Author: dblevins
Date: Mon Aug 20 21:53:07 2012
New Revision: 1375261

URL: http://svn.apache.org/viewvc?rev=1375261&view=rev
Log:
TOMEE-392 - EJB properties overriding from system.properties, 
application.properties or module.properties

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/BeanProperties.java
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/BeanPropertiesTest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/BeanProperties.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/BeanProperties.java?rev=1375261&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/BeanProperties.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/BeanProperties.java
 Mon Aug 20 21:53:07 2012
@@ -0,0 +1,98 @@
+/*
+ * 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.OpenEJBException;
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.oejb3.EjbDeployment;
+import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.SuperProperties;
+
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BeanProperties implements DynamicDeployer {
+
+    private static final Logger log = 
Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG, ModuleProperties.class);
+
+    @Override
+    public AppModule deploy(AppModule appModule) throws OpenEJBException {
+
+        final Properties base = new Properties();
+        base.putAll(SystemInstance.get().getProperties());
+        base.putAll(appModule.getProperties());
+
+        for (EjbModule module : appModule.getEjbModules()) {
+            final Properties overrides = new 
SuperProperties().caseInsensitive(true);
+            overrides.putAll(base);
+            overrides.putAll(module.getProperties());
+
+            if (module.getOpenejbJar() == null) {
+                module.setOpenejbJar(new OpenejbJar());
+            }
+
+            final OpenejbJar openejbJar = module.getOpenejbJar();
+
+            final Map<String, EjbDeployment> deploymentMap = 
openejbJar.getDeploymentsByEjbName();
+            for (EnterpriseBean bean : 
module.getEjbJar().getEnterpriseBeans()) {
+                final SuperProperties properties = new 
SuperProperties().caseInsensitive(true);
+
+                final EjbDeployment deployment = 
deploymentMap.get(bean.getEjbName());
+                if (deployment != null) {
+                    properties.putAll(deployment.getProperties());
+                    deployment.getProperties().clear();
+                }
+
+                final String id = bean.getEjbName() + ".";
+
+                for (Map.Entry<Object, Object> entry : overrides.entrySet()) {
+                    final String key = entry.getKey().toString();
+
+                    if (key.startsWith(id)) {
+                        final String property = key.substring(id.length());
+
+                        if (properties.containsKey(property)) {
+                            log.debug("Overriding ejb " + bean.getEjbName() + 
" property " + property + "=" + entry.getValue());
+                        } else {
+                            log.debug("Adding ejb " + bean.getEjbName() + " 
property " + property + "=" + entry.getValue());
+                        }
+
+                        properties.put(property, entry.getValue());
+                    }
+                }
+
+                if (properties.size() > 0) {
+                    if (deployment == null) {
+                        final EjbDeployment ejbDeployment = 
openejbJar.addEjbDeployment(bean);
+                        ejbDeployment.getProperties().putAll(properties);
+                    } else {
+                        deployment.getProperties().putAll(properties);
+                    }
+                }
+            }
+        }
+
+        return appModule;
+    }
+
+}

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1375261&r1=1375260&r2=1375261&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 Mon Aug 20 21:53:07 2012
@@ -174,6 +174,8 @@ public class ConfigurationFactory implem
 
         chain.add(new AnnotationDeployer());
 
+        chain.add(new BeanProperties());
+
         chain.add(new ProxyBeanClassUpdate());
 
         chain.add(new GeneratedClientModules.Prune());

Added: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/BeanPropertiesTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/BeanPropertiesTest.java?rev=1375261&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/BeanPropertiesTest.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/BeanPropertiesTest.java
 Mon Aug 20 21:53:07 2012
@@ -0,0 +1,280 @@
+/*
+ * 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 junit.framework.TestCase;
+import org.apache.openejb.AppContext;
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.ModuleContext;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.assembler.classic.SecurityServiceInfo;
+import org.apache.openejb.assembler.classic.TransactionServiceInfo;
+import org.apache.openejb.loader.Options;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+import org.apache.openejb.util.Archives;
+
+import javax.ejb.Singleton;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BeanPropertiesTest extends TestCase {
+
+    public void testFile() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            final Map<String, String> map = new HashMap<String, String>();
+            map.put("META-INF/openejb-jar.xml",
+                    "<openejb-jar>\n" +
+                            "  <ejb-deployment ejb-name=\"WidgetBean\">\n" +
+                            "    <properties>\n" +
+                            "      color=orange\n" +
+                            "    </properties>\n" +
+                            "  </ejb-deployment>\n" +
+                            "</openejb-jar>");
+
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    public void testOverrideAdd() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            SystemInstance.get().getProperties().put("WidgetBean.color", 
"orange");
+
+            final Map<String, String> map = new HashMap<String, String>();
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    public void testOverrideReplace() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            SystemInstance.get().getProperties().put("WidgetBean.color", 
"orange");
+
+            final Map<String, String> map = new HashMap<String, String>();
+            map.put("META-INF/openejb-jar.xml",
+                    "<openejb-jar>\n" +
+                            "  <ejb-deployment ejb-name=\"WidgetBean\">\n" +
+                            "    <properties>\n" +
+                            "      color=white\n" +
+                            "    </properties>\n" +
+                            "  </ejb-deployment>\n" +
+                            "</openejb-jar>");
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    public void testOverrideFromModuleProperties() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            final Map<String, String> map = new HashMap<String, String>();
+            map.put("META-INF/openejb-jar.xml",
+                    "<openejb-jar>\n" +
+                            "  <ejb-deployment ejb-name=\"WidgetBean\">\n" +
+                            "    <properties>\n" +
+                            "      color=white\n" +
+                            "    </properties>\n" +
+                            "  </ejb-deployment>\n" +
+                            "</openejb-jar>");
+            map.put("META-INF/module.properties", "WidgetBean.color=orange");
+
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    public void testOverrideFromApplicationProperties() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            final Map<String, String> map = new HashMap<String, String>();
+            map.put("META-INF/openejb-jar.xml",
+                    "<openejb-jar>\n" +
+                            "  <ejb-deployment ejb-name=\"WidgetBean\">\n" +
+                            "    <properties>\n" +
+                            "      color=white\n" +
+                            "    </properties>\n" +
+                            "  </ejb-deployment>\n" +
+                            "</openejb-jar>");
+            map.put("META-INF/application.properties", 
"WidgetBean.color=orange");
+
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    public void testOverrideFromFullDottedPath() throws Exception {
+        final ConfigurationFactory config = new ConfigurationFactory();
+        final Assembler assembler = new Assembler();
+
+        { // setup the system
+            
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+            
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+        }
+
+        {
+            final Map<String, String> map = new HashMap<String, String>();
+            map.put("META-INF/ejb-jar.xml", "<ejb-jar id=\"Foo\"/>");
+            map.put("META-INF/openejb-jar.xml",
+                    "<openejb-jar>\n" +
+                            "  <ejb-deployment ejb-name=\"WidgetBean\">\n" +
+                            "    <properties>\n" +
+                            "      color=white\n" +
+                            "    </properties>\n" +
+                            "  </ejb-deployment>\n" +
+                            "</openejb-jar>");
+
+            final File app = Archives.fileArchive(map, WidgetBean.class);
+
+            // Foo is both the app-name and module-name
+            SystemInstance.get().setProperty("Foo.Foo.WidgetBean.color", 
"orange");
+
+            assembler.createApplication(config.configureApplication(app));
+        }
+
+        final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
+
+        assertContexts(containerSystem);
+    }
+
+    private void assertContexts(ContainerSystem containerSystem) {
+        final BeanContext beanContext = 
containerSystem.getBeanContext("WidgetBean");
+        final ModuleContext moduleContext = beanContext.getModuleContext();
+        final AppContext appContext = moduleContext.getAppContext();
+
+        { // Assert as Properties
+
+            // BeanContext should have color property
+            assertProperty(beanContext.getProperties(), "color", "orange");
+
+            // ModuleContext and AppContext should not
+            assertNoProperty(moduleContext.getProperties(), "color");
+            assertNoProperty(appContext.getProperties(), "color");
+
+            // Try all the above again with mixed case
+            assertProperty(beanContext.getProperties(), "coLOr", "orange");
+            assertNoProperty(moduleContext.getProperties(), "coLOr");
+            assertNoProperty(appContext.getProperties(), "coLOr");
+        }
+
+        { // Assert as Options
+
+            // ModuleContext should have color option
+            assertOption(beanContext.getOptions(), "color", "orange");
+
+            // AppContext and ModuleContext should remain unpolluted
+            assertNoOption(moduleContext.getOptions(), "color");
+            assertNoOption(appContext.getOptions(), "color");
+
+            // Try all the above again using mixed case
+            assertOption(beanContext.getOptions(), "coLoR", "orange");
+            assertNoOption(moduleContext.getOptions(), "coLoR");
+            assertNoOption(appContext.getOptions(), "coLoR");
+        }
+    }
+
+    private void assertOption(Options options, final String key, final String 
value) {
+        assertEquals(value, options.get(key, key + " (not set)"));
+    }
+
+    private void assertNoOption(Options options, final String key) {
+        final String defaultValue = key + " (not set)";
+        assertEquals(defaultValue, options.get(key, defaultValue));
+    }
+
+    private void assertProperty(Properties properties, final String key, final 
String value) {
+        assertTrue(properties.containsKey(key));
+        assertEquals(value, properties.getProperty(key));
+    }
+
+    private void assertNoProperty(Properties properties, final String key) {
+        assertFalse(properties.containsKey(key));
+    }
+
+    @Singleton
+    public static class WidgetBean {
+
+    }
+}


Reply via email to