tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-1574 TOMEE-1573 @Module Resources for app composer + containers in resources.xml
Date Mon, 04 May 2015 17:53:12 GMT
Repository: tomee
Updated Branches:
  refs/heads/master bf1b6e58a -> d8b80bf10


TOMEE-1574 TOMEE-1573 @Module Resources for app composer + containers in resources.xml


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/d8b80bf1
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d8b80bf1
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d8b80bf1

Branch: refs/heads/master
Commit: d8b80bf10965ba7f3437231973ed5ecd53c412de
Parents: bf1b6e5
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Mon May 4 19:53:04 2015 +0200
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Mon May 4 19:53:04 2015 +0200

----------------------------------------------------------------------
 .../openejb/assembler/classic/AppInfo.java      |  1 +
 .../openejb/assembler/classic/Assembler.java    |  5 ++
 .../apache/openejb/config/AppInfoBuilder.java   | 14 ++++
 .../org/apache/openejb/config/AppModule.java    |  6 ++
 .../org/apache/openejb/config/AutoConfig.java   | 58 +++++++++++++--
 .../apache/openejb/config/DeploymentLoader.java |  1 +
 .../java/org/apache/openejb/config/Module.java  |  1 +
 .../openejb/testing/ApplicationComposers.java   |  8 +-
 .../apache/openejb/config/AppContainerTest.java | 77 ++++++++++++++++++++
 9 files changed, 164 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/AppInfo.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/AppInfo.java
b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/AppInfo.java
index 1212c8e..e72dd59 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/AppInfo.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/AppInfo.java
@@ -45,6 +45,7 @@ public class AppInfo extends InfoObject {
     public List<ServiceInfo> services = new ArrayList<ServiceInfo>();
     public final List<String> libs = new ArrayList<String>();
     public final Set<String> watchedResources = new TreeSet<String>();
+    public final Set<String> containerIds = new TreeSet<String>();
     public final Set<String> resourceIds = new TreeSet<String>();
     public final Set<String> resourceAliases = new TreeSet<String>();
     public final JndiEncInfo globalJndiEnc = new JndiEncInfo();

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
index 359d9bc..b6cdaf9 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
@@ -2266,6 +2266,9 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
                 }
             }
 
+            for (final String id : appInfo.containerIds) {
+                removeContainer(id);
+            }
             containerSystem.removeAppContext(appInfo.appId);
 
             if (!appInfo.properties.containsKey("tomee.destroying")) { // destroy tomee classloader
after resources cleanup
@@ -2465,6 +2468,8 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
 
         final Object service = serviceRecipe.create();
 
+        serviceRecipe.getUnsetProperties().remove("id"); // we forced it
+        serviceRecipe.getUnsetProperties().remove("securityService"); // we forced it
         logUnusedProperties(serviceRecipe, serviceInfo);
 
         final Class interfce = serviceInterfaces.get(serviceInfo.service);

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/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 dcbb451..cc61204 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
@@ -162,6 +162,7 @@ class AppInfoBuilder {
                 containerIds.add(containerInfo.id);
             }
         }
+        containerIds.addAll(appInfo.containerIds);
 
         //
         //  EJB Jars
@@ -176,6 +177,14 @@ class AppInfoBuilder {
 
                 for (final EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
                     final EjbDeployment d = deploymentsByEjbName.get(bean.ejbName);
+                    if (d.getContainerId() != null && !containerIds.contains(d.getContainerId()))
{
+                        for (final String cId : appInfo.containerIds) {
+                            if (cId.endsWith("/" + d.getContainerId())) {
+                                d.setContainerId(cId);
+                                break;
+                            }
+                        }
+                    }
 
                     /*
                      * JRG - there's probably a better way of handling this, but this code
handles the case when:
@@ -348,6 +357,11 @@ class AppInfoBuilder {
                 info.resourceAliases.addAll(def.getAliases());
             }
         }
+        for (final Container def : module.getContainers()) {
+            if (!def.getProperties().containsKey("ApplicationWide")) {
+                info.containerIds.add(def.getId());
+            }
+        }
     }
 
     private void buildClientModules(final AppModule appModule, final AppInfo appInfo, final
JndiEncInfoBuilder jndiEncInfoBuilder) throws OpenEJBException {

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java
index ea7b841..42bbfb5 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java
@@ -17,6 +17,7 @@
 
 package org.apache.openejb.config;
 
+import org.apache.openejb.config.sys.Container;
 import org.apache.openejb.config.sys.Resource;
 import org.apache.openejb.config.sys.Service;
 import org.apache.openejb.core.ParentClassLoaderFinder;
@@ -61,6 +62,7 @@ public class AppModule implements DeploymentModule {
     private final Map<String, TransactionType> txTypeByUnit = new HashMap<String,
TransactionType>();
     // TODO We could turn this into the Resources JAXB object and support containers and
other things as well
     private final Collection<Resource> resources = new HashSet<Resource>();
+    private final Collection<Container> containers = new HashSet<Container>();
     private final Collection<Service> services = new HashSet<Service>();
     private final ClassLoader classLoader;
     private EntityMappings cmpMappings;
@@ -364,6 +366,10 @@ public class AppModule implements DeploymentModule {
         return resources;
     }
 
+    public Collection<Container> getContainers() {
+        return containers;
+    }
+
     public Collection<Service> getServices() {
         return services;
     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
index 6661eb2..90883db 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
@@ -21,6 +21,7 @@ import org.apache.openejb.JndiConstants;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.assembler.classic.ContainerInfo;
 import org.apache.openejb.assembler.classic.ResourceInfo;
+import org.apache.openejb.config.sys.Container;
 import org.apache.openejb.config.sys.Resource;
 import org.apache.openejb.jee.ActivationConfig;
 import org.apache.openejb.jee.ActivationConfigProperty;
@@ -70,6 +71,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -109,6 +111,8 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
     public static final String ORIGIN_FLAG = "Origin";
     public static final String ORIGINAL_ID = "OriginalId";
 
+    private static final AppResources EMPTY_APP_RESOURCES = new AppResources();
+
     public static Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG,
AutoConfig.class);
 
     private static final int MAX_IMPLICIT_POOL_SIZE = 5;
@@ -184,6 +188,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
         appResources.dump();
 
         processApplicationResources(appModule);
+        processApplicationContainers(appModule, appResources);
 
         for (final EjbModule ejbModule : appModule.getEjbModules()) {
             processActivationConfig(ejbModule);
@@ -893,6 +898,25 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
         }
     }
 
+    private void processApplicationContainers(final AppModule module, final AppResources
appResources) throws OpenEJBException {
+        if (module.getContainers().isEmpty()) {
+            return;
+        }
+
+        final String prefix = module.getModuleId() + "/";
+        for (final Container container : module.getContainers()) {
+            if (container.getId() == null) {
+                throw new IllegalStateException("a container can't get a null id: " + container.getType()
+ " from " + module.getModuleId());
+            }
+            if (!container.getId().startsWith(prefix)) {
+                container.setId(prefix + container.getId());
+            }
+            final ContainerInfo containerInfo = configFactory.createContainerInfo(container);
+            configFactory.install(containerInfo);
+            appResources.addContainer(containerInfo);
+        }
+    }
+
     private void processApplicationResources(final AppModule module) throws OpenEJBException
{
         final Collection<Resource> resources = module.getResources();
 
@@ -1944,7 +1968,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
         }
 
         if (appResources == null) {
-            appResources = new AppResources();
+            appResources = EMPTY_APP_RESOURCES;
         }
 
         // skip references such as URL which are automatically handled by the server
@@ -2146,7 +2170,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
             return null;
         }
         if (appResources == null) {
-            appResources = new AppResources();
+            appResources = EMPTY_APP_RESOURCES;
         }
 
         // skip references such as URLs which are automatically handled by the server
@@ -2198,7 +2222,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
     }
 
     private String getUsableContainer(final Class<? extends ContainerInfo> containerInfoType,
final Object bean, final AppResources appResources) {
-        if (bean instanceof MessageDrivenBean) {
+        if (MessageDrivenBean.class.isInstance(bean)) {
             final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
             final String messagingType = messageDrivenBean.getMessagingType();
             final List<String> containerIds = appResources.containerIdsByType.get(messagingType);
@@ -2207,10 +2231,22 @@ public class AutoConfig implements DynamicDeployer, JndiConstants
{
             }
         }
 
-        for (final ContainerInfo containerInfo : configFactory.getContainerInfos()) {
+        String containerInfo = matchContainer(containerInfoType, bean, appResources.getContainerInfos());
+        if (containerInfo == null) { // avoid to build configFactory.getContainerInfos()
if not needed
+            containerInfo = matchContainer(containerInfoType, bean, configFactory.getContainerInfos());
+        }
+        if (containerInfo != null) {
+            return containerInfo;
+        }
+
+        return null;
+    }
+
+    private String matchContainer(final Class<? extends ContainerInfo> containerInfoType,
final Object bean, final Collection<ContainerInfo> list) {
+        for (final ContainerInfo containerInfo : list) {
             if (containerInfo.getClass().equals(containerInfoType)) {
                 // MDBs must match message listener interface type
-                if (bean instanceof MessageDrivenBean) {
+                if (MessageDrivenBean.class.isInstance(bean)) {
                     final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
                     final String messagingType = messageDrivenBean.getMessagingType();
                     if (containerInfo.properties.get("MessageListenerInterface").equals(messagingType))
{
@@ -2221,7 +2257,6 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
                 }
             }
         }
-
         return null;
     }
 
@@ -2234,6 +2269,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
         private final Map<String, List<String>> resourceIdsByType = new TreeMap<String,
List<String>>();
         private final Map<String, List<String>> resourceEnvIdsByType = new TreeMap<String,
List<String>>();
         private final Map<String, List<String>> containerIdsByType = new TreeMap<String,
List<String>>();
+        private final Collection<ContainerInfo> containerInfos = new HashSet<>();
 
         public void dump() {
             if (!logger.isDebugEnabled()) {
@@ -2397,6 +2433,16 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
             //            }
         }
 
+        public Collection<ContainerInfo> getContainerInfos() {
+            return containerInfos;
+        }
+
+        // needs to be called after merge otherwise we get wrong/missing data
+        private void addContainer(final ContainerInfo container) {
+            containerInfos.add(container);
+            // no need to enrich containerIdsByType here, TODO: see if we can remove containerIdsByType
+        }
+
         public List<String> getResourceIds(final String type) {
             if (type == null) {
                 final List<String> allResourceIds = new ArrayList<String>();

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
index 5ff4034..1242ae2 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
@@ -546,6 +546,7 @@ public class DeploymentLoader implements DeploymentFilterable {
 
                     @Override
                     public void initResources(final Resources resources) {
+                        appModule.getContainers().addAll(resources.getContainer());
                         appModule.getResources().addAll(resources.getResource());
                         appModule.getServices().addAll(resources.getService());
                     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/config/Module.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/Module.java b/container/openejb-core/src/main/java/org/apache/openejb/config/Module.java
index 30667f3..bdf1dad 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/Module.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/Module.java
@@ -100,6 +100,7 @@ public class Module {
         this.appModule = appModule;
         if (resources != null) {
             this.appModule.getResources().addAll(resources.getResource());
+            this.appModule.getContainers().addAll(resources.getContainer());
             this.appModule.getServices().addAll(resources.getService());
         }
     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
index 08c2c5e..62d9821 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
@@ -44,6 +44,7 @@ import org.apache.openejb.config.WebModule;
 import org.apache.openejb.config.sys.JSonConfigReader;
 import org.apache.openejb.config.sys.JaxbOpenejb;
 import org.apache.openejb.config.sys.Openejb;
+import org.apache.openejb.config.sys.Resources;
 import org.apache.openejb.core.LocalInitialContextFactory;
 import org.apache.openejb.core.Operation;
 import org.apache.openejb.core.ParentClassLoaderFinder;
@@ -146,7 +147,8 @@ public class ApplicationComposers {
         WebApp.class, EjbJar.class, EnterpriseBean.class,
         Persistence.class, PersistenceUnit.class,
         Connector.class, Beans.class,
-        Class[].class, Class.class
+        Class[].class, Class.class,
+        Resources.class
     };
 
     static {
@@ -594,6 +596,10 @@ public class ApplicationComposers {
                     ejbModule.setFinder(new AnnotationFinder((Archive) obj).link());
                     ejbModule.setBeans(new Beans());
                     appModule.getEjbModules().add(ejbModule);
+                } else if (obj instanceof Resources) {
+                    final Resources asResources = Resources.class.cast(obj);
+                    appModule.getResources().addAll(asResources.getResource());
+                    appModule.getContainers().addAll(asResources.getContainer());
                 } else if (obj instanceof AppModule) {
                     // we can probably go further here
                     final AppModule module = (AppModule) obj;

http://git-wip-us.apache.org/repos/asf/tomee/blob/d8b80bf1/container/openejb-core/src/test/java/org/apache/openejb/config/AppContainerTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/AppContainerTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/config/AppContainerTest.java
new file mode 100644
index 0000000..baad710
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/config/AppContainerTest.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.openejb.config;
+
+import org.apache.openejb.InterfaceType;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.config.sys.Container;
+import org.apache.openejb.config.sys.Resources;
+import org.apache.openejb.core.singleton.SingletonContainer;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.SimpleLog;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.lang.reflect.Method;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+
+import static org.junit.Assert.assertEquals;
+
+@SimpleLog
+@Classes(AppContainerTest.Singleton1.class)
+@RunWith(ApplicationComposer.class)
+public class AppContainerTest {
+    @org.apache.openejb.testing.Module
+    public Resources resources() {
+        final Resources resources = new Resources();
+        final Container container = new Container();
+        container.setId("theMine");
+        container.setType("SINGLETON");
+        container.setClassName("org.apache.openejb.config.AppContainerTest$MySingletonContainer");
+        resources.getContainer().add(container);
+        return resources;
+    }
+
+    @Singleton
+    public static class Singleton1 {
+        public String ok() {
+            throw new UnsupportedOperationException("my container mocks it");
+        }
+    }
+
+    @EJB
+    private Singleton1 s1;
+
+    @Test
+    public void run() {
+        assertEquals("yeah!", s1.ok());
+    }
+
+    public static class MySingletonContainer extends SingletonContainer {
+        public MySingletonContainer() throws OpenEJBException {
+            super("mine", null);
+        }
+
+        @Override
+        public Object invoke(final Object deployID, final InterfaceType type, final Class
callInterface,
+                             final Method callMethod, final Object[] args, final Object primKey)
throws OpenEJBException {
+            return callMethod.getDeclaringClass() == Singleton1.class ? "yeah!" : super.invoke(deployID,
type, callInterface, callMethod, args, primKey);
+        }
+    }
+}


Mime
View raw message