Repository: tomee Updated Branches: refs/heads/master 50339ce99 -> 064aec704
TOMEE-1567 supporting persistence unit overriding from app.properties Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/064aec70 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/064aec70 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/064aec70 Branch: refs/heads/master Commit: 064aec70409993f2f300acb1aa58ae6fad7f3b85 Parents: 50339ce Author: Romain Manni-Bucau <rmannibu...@apache.org> Authored: Wed Apr 29 16:28:13 2015 +0200 Committer: Romain Manni-Bucau <rmannibu...@apache.org> Committed: Wed Apr 29 16:28:13 2015 +0200 ---------------------------------------------------------------------- .../apache/openejb/config/AppInfoBuilder.java | 45 ++++++++------ .../config/AppPersistenceUnitOverrideTest.java | 62 ++++++++++++++++++++ 2 files changed, 90 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/064aec70/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java b/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java index a8f3d6e..dcbb451 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java @@ -84,7 +84,6 @@ import org.apache.openejb.util.Logger; import org.apache.openejb.util.Messages; import org.apache.openejb.util.References; -import javax.xml.bind.JAXBException; import java.io.File; import java.io.IOException; import java.net.URL; @@ -97,6 +96,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import javax.xml.bind.JAXBException; import static org.apache.openejb.util.URLs.toFile; @@ -680,7 +680,7 @@ class AppInfoBuilder { // Handle Properties info.properties.putAll(persistenceUnit.getProperties()); - PersistenceProviderProperties.apply(appModule.getClassLoader(), info); + PersistenceProviderProperties.apply(appModule, info); // Persistence Unit Root Url @@ -747,10 +747,11 @@ class AppInfoBuilder { } /** - * @param classLoader the temp classloader, take care to only use getResource here + * @param appModule the app module with its config and its temp classloader, take care to only use getResource here * @param info the persistence unit info */ - private static void apply(final ClassLoader classLoader, final PersistenceUnitInfo info) { + private static void apply(final AppModule appModule, final PersistenceUnitInfo info) { + final ClassLoader classLoader = appModule.getClassLoader(); overrideFromSystemProp(info); // The result is that OpenEJB-specific configuration can be avoided when @@ -766,7 +767,7 @@ class AppInfoBuilder { if ("org.hibernate.ejb.HibernatePersistence".equals(info.provider) || "org.hibernate.jpa.HibernatePersistenceProvider".equals(info.provider)) { // Apply the overrides that apply to all persistence units of this provider - override(info, "hibernate"); + override(appModule.getProperties(), info, "hibernate"); String className = info.properties.getProperty(HIBERNATE_JTA_PLATFORM); if (className == null) { @@ -812,7 +813,7 @@ class AppInfoBuilder { "oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider".equals(info.provider)) { // Apply the overrides that apply to all persistence units of this provider - override(info, "toplink"); + override(appModule.getProperties(), info, "toplink"); final String lookupProperty = "toplink.target-server"; final String openejbLookupClass = MakeTxLookup.TOPLINK_FACTORY; @@ -831,7 +832,7 @@ class AppInfoBuilder { } else if ("org.eclipse.persistence.jpa.PersistenceProvider".equals(info.provider) || "org.eclipse.persistence.jpa.osgi.PersistenceProvider".equals(info.provider)) { // Apply the overrides that apply to all persistence units of this provider - override(info, "eclipselink"); + override(appModule.getProperties(), info, "eclipselink"); final String className = info.properties.getProperty(ECLIPSELINK_TARGET_SERVER); @@ -858,7 +859,7 @@ class AppInfoBuilder { } else if (info.provider == null || "org.apache.openjpa.persistence.PersistenceProviderImpl".equals(info.provider)) { // Apply the overrides that apply to all persistence units of this provider - override(info, "openjpa"); + override(appModule.getProperties(), info, "openjpa"); final String existing = info.properties.getProperty(OPENJPA_RUNTIME_UNENHANCED_CLASSES); @@ -904,7 +905,7 @@ class AppInfoBuilder { } // Apply the overrides that apply to just this persistence unit - override(info); + override(appModule.getProperties(), info); } private static void overrideFromSystemProp(final PersistenceUnitInfo info) { @@ -925,20 +926,30 @@ class AppInfoBuilder { } } - private static void override(final PersistenceUnitInfo info) { - override(info, info.name); + private static void override(final Properties appProperties, final PersistenceUnitInfo info) { + override(appProperties, info, info.name); } - private static void override(final PersistenceUnitInfo info, final String prefix) { - - final Properties overrides = ConfigurationFactory.getSystemProperties(prefix, "PersistenceUnit"); + private static void override(final Properties appProperties, final PersistenceUnitInfo info, final String prefix) { + final Properties propertiesToCheckForOverridings = new Properties(); + propertiesToCheckForOverridings.putAll(appProperties); + propertiesToCheckForOverridings.putAll(System.getProperties()); + propertiesToCheckForOverridings.putAll(SystemInstance.get().getProperties()); + final Properties overrides = ConfigurationFactory.getOverrides(propertiesToCheckForOverridings, prefix, "PersistenceUnit"); for (final Map.Entry<Object, Object> entry : overrides.entrySet()) { final String property = (String) (prefix.equalsIgnoreCase(info.name) ? entry.getKey() : prefix + "." + entry.getKey()); - final String value = (String) entry.getValue(); + String value = (String) entry.getValue(); - if ("openjpa.Log".equals(property) && info.properties.containsKey("openjpa.Log")) { // we set a default - continue; + if ("openjpa.Log".equals(property) && "org.apache.openejb.openjpa.JULOpenJPALogFactory".equals(value)) { // we set a default + if (info.properties.containsKey("openjpa.Log")) { + continue; + } + if (appProperties.containsKey("openjpa.Log")) { + value = appProperties.getProperty(property, value); + } else { + continue; + } } if (info.properties.containsKey(property)) { http://git-wip-us.apache.org/repos/asf/tomee/blob/064aec70/container/openejb-core/src/test/java/org/apache/openejb/config/AppPersistenceUnitOverrideTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/AppPersistenceUnitOverrideTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/AppPersistenceUnitOverrideTest.java new file mode 100644 index 0000000..1bd1651 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/config/AppPersistenceUnitOverrideTest.java @@ -0,0 +1,62 @@ +/** + * 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.openejb.config; + +import org.apache.openejb.api.configuration.PersistenceUnitDefinition; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.testing.ApplicationConfiguration; +import org.apache.openejb.testing.Classes; +import org.apache.openejb.testing.SimpleLog; +import org.apache.openejb.testng.PropertiesBuilder; +import org.apache.openjpa.lib.log.Log; +import org.apache.openjpa.lib.log.LogFactory; +import org.apache.openjpa.lib.log.NoneLogFactory; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.Properties; + +import static org.junit.Assert.assertFalse; + +@RunWith(ApplicationComposer.class) +@PersistenceUnitDefinition +@Classes +@SimpleLog +public class AppPersistenceUnitOverrideTest { + @ApplicationConfiguration + public Properties properties() { + MyLogFactory.CHANNELS.clear(); + return new PropertiesBuilder().p("openjpa.Log", MyLogFactory.class.getName()).build(); + } + + @Test + public void run() { + assertFalse(MyLogFactory.CHANNELS.isEmpty()); + } + + public static class MyLogFactory implements LogFactory { + public static final Collection<String> CHANNELS = new LinkedList<>(); + + @Override + public Log getLog(final String channel) { + CHANNELS.add(channel); + return new NoneLogFactory.NoneLog(); + } + } +}