diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 85847a7908842..c69513496c5b2 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -9,11 +9,13 @@ import static org.hibernate.cfg.AvailableSettings.USE_SECOND_LEVEL_CACHE; import java.io.IOException; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; @@ -423,6 +425,7 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder BuildProducer beanContainerListener) throws Exception { feature.produce(new FeatureBuildItem(Feature.HIBERNATE_ORM)); + validateHibernatePropertiesNotUsed(); final boolean enableORM = hasEntities(jpaModel); final boolean hibernateReactivePresent = capabilities.isPresent(Capability.HIBERNATE_REACTIVE); @@ -472,6 +475,22 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder proxyDefinitions.getProxies()))); } + private void validateHibernatePropertiesNotUsed() { + try { + final Enumeration resources = Thread.currentThread().getContextClassLoader().getResources( + "hibernate.properties"); + if (resources.hasMoreElements()) { + final URL url = resources.nextElement(); + throw new IllegalStateException( + "The Hibernate ORM configuration in Quarkus does not support sourcing configuration properties from resources named `hibernate.properties`," + + " and this is now expressly prohibited as such a file could lead to unpredictable semantics. Please remove it from `" + + url.toExternalForm() + '`'); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @BuildStep void handleNativeImageImportSql(BuildProducer resources, List descriptors, diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java new file mode 100644 index 0000000000000..bbc40d8c0b08a --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java @@ -0,0 +1,42 @@ +package io.quarkus.hibernate.orm.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; + +/** + * Since we decided that we're no longer supporting to read an hibernate.properties resource, + * let's also test that this is made explicit. + * N.B. while we no longer parse the file during boot, there are other components in Hibernate ORM + * that look for it so this would lead to inconsistencies. + */ +public class NoHibernatePropertiesTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .assertException(t -> { + assertThat(t) + .isInstanceOf(IllegalStateException.class) + .hasMessageContainingAll( + "The Hibernate ORM configuration in Quarkus does not support sourcing configuration properties from resources named `hibernate.properties`"); + }) + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(MyEntity.class) + .addAsResource(new StringAsset(""), "hibernate.properties") + .addAsResource("application.properties")) + .overrideConfigKey("quarkus.datasource.devservices", "false"); + + @Test + public void testInvalidConfiguration() { + // deployment exception should happen first + Assertions.fail(); + } + +} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/QuarkusEnvironment.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/QuarkusEnvironment.java deleted file mode 100644 index de411d39ef1f2..0000000000000 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/QuarkusEnvironment.java +++ /dev/null @@ -1,66 +0,0 @@ -package io.quarkus.hibernate.orm.runtime.boot; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Collections; -import java.util.Map; -import java.util.Properties; - -import org.hibernate.cfg.Environment; -import org.hibernate.internal.CoreMessageLogger; -import org.jboss.logging.Logger; - -/** - * A lighter alternative to org.hibernate.cfg.Environment: we don't need it to look for certain environment variables, - * and don't want it to copy all of {@link System#getProperties()} into the ORM configuration. - * The only legacy behaviour we allow is to load the "/hibernate.properties" resource. - */ -public class QuarkusEnvironment { - - private static final Map GLOBAL_INITIAL_PROPERTIES; - - static { - //Skip logging this as the original Environment initialization is also being triggered; - //avoiding that will need ORM6. - //Version.logVersion(); - - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader != null) { - InputStream stream = classLoader.getResourceAsStream("/hibernate.properties"); - if (stream != null) { - final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, Environment.class.getName()); - LOG.warnf( - "The resource hibernate.properties was found in the root of your application. This configuration source is deprecated and will be removed in a future version of Quarkus, " - + - "as it's not compatible with Live Coding, multiple Persistence Units and many more cool features we have planned. Please refrain from " - + - "using it to configure your ORM and use the available configurations in application.properties (check the guide in https://quarkus.io/guides/hibernate-orm#quarkus-hibernate-orm_configuration). Should no configuration exist for your use case please report in: https://github.com/quarkusio/quarkus/issues/. Thank you! "); - final Properties p = new Properties(); - try { - p.load(stream); - } catch (Exception e) { - LOG.unableToLoadProperties(); - } finally { - try { - stream.close(); - } catch (IOException ioe) { - LOG.unableToCloseStreamError(ioe); - } - } - GLOBAL_INITIAL_PROPERTIES = Collections.unmodifiableMap(p); - } else { - GLOBAL_INITIAL_PROPERTIES = Collections.EMPTY_MAP; - } - } else { - GLOBAL_INITIAL_PROPERTIES = Collections.EMPTY_MAP; - } - } - - /** - * @return - * @see Environment#getProperties() - */ - public static Map getInitialProperties() { - return GLOBAL_INITIAL_PROPERTIES; - } -} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/recording/RecordableBootstrap.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/recording/RecordableBootstrap.java index 84ad02b35ef90..e7f2970f75176 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/recording/RecordableBootstrap.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/recording/RecordableBootstrap.java @@ -19,7 +19,6 @@ import org.hibernate.service.internal.ProvidedService; import org.hibernate.service.spi.ServiceContributor; -import io.quarkus.hibernate.orm.runtime.boot.QuarkusEnvironment; import io.quarkus.hibernate.orm.runtime.service.InitialInitiatorListProvider; /** @@ -42,13 +41,7 @@ public final class RecordableBootstrap extends StandardServiceRegistryBuilder { public RecordableBootstrap(BootstrapServiceRegistry bootstrapServiceRegistry, InitialInitiatorListProvider initialInitiatorsProvider) { - this(bootstrapServiceRegistry, initialProperties(), LoadedConfig.baseline(), initialInitiatorsProvider); - } - - private static Map initialProperties() { - HashMap map = new HashMap(); - map.putAll(QuarkusEnvironment.getInitialProperties()); - return map; + this(bootstrapServiceRegistry, new HashMap(), LoadedConfig.baseline(), initialInitiatorsProvider); } private RecordableBootstrap(BootstrapServiceRegistry bootstrapServiceRegistry, Map properties,