DELTASPIKE-1251 pick up EMF config via ConfigResolver. Currently PersistenceConfigurationProvider only looks for files named persistence-persistenceUnitName.properties.
It would be great to also be able to pick up EntityManagerFactory configuration via our standard ConfigResolver system. For a PersistenceUnit named 'MyUnit' we could e.g. pick up persitence-MyUnit.properties plus all config values which start with 'deltaspike.persistence.config.MyUnit.' A config value 'deltaspike.persistence.config.MyUnit.javax.persistence.password=bla' Would end up as javax.persistence.password=bla configuration entry Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/f5ba10bb Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/f5ba10bb Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/f5ba10bb Branch: refs/heads/master Commit: f5ba10bb78465ac044664eba524d1bbc2bedee39 Parents: 906049d Author: Mark Struberg <[email protected]> Authored: Mon May 8 11:30:13 2017 +0200 Committer: Mark Struberg <[email protected]> Committed: Mon May 8 11:30:13 2017 +0200 ---------------------------------------------------------------------- .../PersistenceConfigurationProviderImpl.java | 42 +++++++++++ .../EntityManagerFactoryProducerTest.java | 5 ++ .../PersistenceConfigurationProviderTest.java | 77 ++++++++++++++++++++ .../META-INF/apache-deltaspike.properties | 24 ++++++ .../resources/persistence-MyUnit.properties | 21 ++++++ 5 files changed, 169 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f5ba10bb/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/PersistenceConfigurationProviderImpl.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/PersistenceConfigurationProviderImpl.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/PersistenceConfigurationProviderImpl.java index 9aec0d6..3bcb2ce 100644 --- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/PersistenceConfigurationProviderImpl.java +++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/PersistenceConfigurationProviderImpl.java @@ -22,7 +22,9 @@ import javax.enterprise.context.ApplicationScoped; import java.util.Properties; +import java.util.Set; +import org.apache.deltaspike.core.api.config.ConfigResolver; import org.apache.deltaspike.core.api.config.PropertyLoader; import org.apache.deltaspike.jpa.spi.entitymanager.PersistenceConfigurationProvider; @@ -33,6 +35,14 @@ import org.apache.deltaspike.jpa.spi.entitymanager.PersistenceConfigurationProvi @ApplicationScoped public class PersistenceConfigurationProviderImpl implements PersistenceConfigurationProvider { + /** + * A prefix which will be used for looking up more specific + * information for a persistenceUnit. + * + * @see #addConfigProperties(Properties, String) + */ + private static final String CONFIG_PREFIX = "deltaspike.persistence.config."; + @Override public Properties getEntityManagerFactoryConfiguration(String persistenceUnitName) { @@ -43,6 +53,38 @@ public class PersistenceConfigurationProviderImpl implements PersistenceConfigur unitProperties = new Properties(); } + unitProperties = addConfigProperties(unitProperties, persistenceUnitName); + return unitProperties; } + + + /** + * Load additional configuration from the Configuration system + * and overload the basic settings with that info. + * + * The key is deltaspike.persistence.config.${persistenceUnitName}.${originalKey} + * + * @see #CONFIG_PREFIX + * @since 1.8.0 + */ + protected Properties addConfigProperties(Properties unitProperties, String persistenceUnitName) + { + // we start with a copy of the original properties + Properties mergedConfig = new Properties(); + mergedConfig.putAll(unitProperties); + + Set<String> allConfigKeys = ConfigResolver.getAllProperties().keySet(); + String unitPrefix = CONFIG_PREFIX + persistenceUnitName + "."; + for (String configKey : allConfigKeys) + { + if (configKey.startsWith(unitPrefix)) + { + mergedConfig.put(configKey.substring(unitPrefix.length()), + ConfigResolver.getProjectStageAwarePropertyValue(configKey)); + } + } + + return mergedConfig; + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f5ba10bb/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/EntityManagerFactoryProducerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/EntityManagerFactoryProducerTest.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/EntityManagerFactoryProducerTest.java index 62b8d03..4fd912e 100644 --- a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/EntityManagerFactoryProducerTest.java +++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/EntityManagerFactoryProducerTest.java @@ -22,6 +22,9 @@ import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.spi.PersistenceProviderResolverHolder; +import java.util.Properties; + +import org.apache.deltaspike.jpa.spi.entitymanager.PersistenceConfigurationProvider; import org.apache.deltaspike.test.category.SeCategory; import org.apache.deltaspike.test.jpa.api.shared.TestEntityManager; import org.apache.deltaspike.test.util.ArchiveUtils; @@ -67,6 +70,8 @@ public class EntityManagerFactoryProducerTest @SampleDb private EntityManager entityManager; + private @Inject PersistenceConfigurationProvider persistenceConfigurationProvider; + @Test public void testUnitDefinitionQualifier() throws Exception { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f5ba10bb/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/PersistenceConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/PersistenceConfigurationProviderTest.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/PersistenceConfigurationProviderTest.java new file mode 100644 index 0000000..ff69d5f --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/entitymanager/PersistenceConfigurationProviderTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jpa.api.entitymanager; + +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.spi.PersistenceProviderResolverHolder; +import java.util.Properties; + +import org.apache.deltaspike.jpa.spi.entitymanager.PersistenceConfigurationProvider; +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.test.jpa.api.shared.TestEntityManager; +import org.apache.deltaspike.test.util.ArchiveUtils; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +@Category(SeCategory.class) +public class PersistenceConfigurationProviderTest +{ + + @Deployment + public static WebArchive deploy() + { + // set the dummy PersistenceProviderResolver which creates our DummyEntityManagerFactory + PersistenceProviderResolverHolder.setPersistenceProviderResolver(new TestPersistenceProviderResolver()); + + JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "unitDefinitionTest.jar") + .addPackage(ArchiveUtils.SHARED_PACKAGE) + .addClass(PersistenceConfigurationProviderTest.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive()) + .addAsLibraries(testJar) + .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml"); + } + + + private @Inject PersistenceConfigurationProvider persistenceConfigurationProvider; + + + @Test + public void testPersistenceConfigurationProvider() + { + Properties myUnitConfig = persistenceConfigurationProvider.getEntityManagerFactoryConfiguration("MyUnit"); + Assert.assertEquals(3, myUnitConfig.size()); + Assert.assertEquals("blub", myUnitConfig.get("javax.persistence.jdbc.password")); + Assert.assertEquals("sa", myUnitConfig.get("javax.persistence.jdbc.user")); + Assert.assertEquals("some.jdbc.Driver", myUnitConfig.get("javax.persistence.jdbc.driver")); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f5ba10bb/deltaspike/modules/jpa/impl/src/test/resources/META-INF/apache-deltaspike.properties ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/resources/META-INF/apache-deltaspike.properties b/deltaspike/modules/jpa/impl/src/test/resources/META-INF/apache-deltaspike.properties new file mode 100644 index 0000000..303b456 --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/resources/META-INF/apache-deltaspike.properties @@ -0,0 +1,24 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# overwriting the original value from bla to blub +deltaspike.persistence.config.MyUnit.javax.persistence.jdbc.password=blub + +# and a new config entry +deltaspike.persistence.config.MyUnit.javax.persistence.jdbc.driver=some.jdbc.Driver \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f5ba10bb/deltaspike/modules/jpa/impl/src/test/resources/persistence-MyUnit.properties ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/resources/persistence-MyUnit.properties b/deltaspike/modules/jpa/impl/src/test/resources/persistence-MyUnit.properties new file mode 100644 index 0000000..ffdab84 --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/resources/persistence-MyUnit.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +javax.persistence.jdbc.user=sa +javax.persistence.jdbc.password=bla \ No newline at end of file
