You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/04/10 09:13:14 UTC

tomee git commit: adding Lazy support for resources to allow to create resources later at runtime - useful for default resources typically

Repository: tomee
Updated Branches:
  refs/heads/master ce7d7480f -> 2b385bb58


adding Lazy support for resources to allow to create resources later at runtime - useful for default resources typically


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

Branch: refs/heads/master
Commit: 2b385bb589f19cc2c15974c7d556fb3379f3a9d2
Parents: ce7d748
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Fri Apr 10 09:10:41 2015 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Fri Apr 10 09:10:41 2015 +0200

----------------------------------------------------------------------
 .../openejb/assembler/classic/Assembler.java    | 84 +++++++++++++++-----
 .../META-INF/org.apache.openejb/service-jar.xml |  9 ++-
 .../assembler/classic/LazyResourceTest.java     | 63 +++++++++++++++
 3 files changed, 133 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/2b385bb5/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 b25e70a..d80e53f 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
@@ -82,6 +82,7 @@ import org.apache.openejb.core.ivm.naming.ContextualJndiReference;
 import org.apache.openejb.core.ivm.naming.IvmContext;
 import org.apache.openejb.core.ivm.naming.IvmJndiFactory;
 import org.apache.openejb.core.ivm.naming.JndiUrlReference;
+import org.apache.openejb.core.ivm.naming.LazyObjectReference;
 import org.apache.openejb.core.ivm.naming.Reference;
 import org.apache.openejb.core.security.SecurityContextHandler;
 import org.apache.openejb.core.timer.EjbTimerServiceImpl;
@@ -221,6 +222,7 @@ import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.concurrent.Callable;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -2443,6 +2445,49 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
     }
 
     public void createResource(final ResourceInfo serviceInfo) throws OpenEJBException {
+        Object service = "true".equalsIgnoreCase(String.valueOf(serviceInfo.properties.remove("Lazy"))) ?
+                newLazyResource(serviceInfo) :
+                doCreateResource(serviceInfo);
+
+        bindResource(serviceInfo.id, service);
+        for (final String alias : serviceInfo.aliases) {
+            bindResource(alias, service);
+        }
+        if (serviceInfo.originAppName != null && !serviceInfo.originAppName.isEmpty() && !"/".equals(serviceInfo.originAppName)
+            && !serviceInfo.id.startsWith("global")) {
+            final String baseJndiName = serviceInfo.id.substring(serviceInfo.originAppName.length() + 1);
+            serviceInfo.aliases.add(baseJndiName);
+            final ContextualJndiReference ref = new ContextualJndiReference(baseJndiName);
+            ref.addPrefix(serviceInfo.originAppName);
+            bindResource(baseJndiName, ref);
+        }
+
+        // Update the config tree
+        config.facilities.resources.add(serviceInfo);
+
+        if (logger.isDebugEnabled()) { // weird to check parent logger but save time and it is almost never activated
+            logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
+        }
+    }
+
+    private LazyResource newLazyResource(final ResourceInfo serviceInfo) {
+        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        return new LazyResource(new Callable<Object>() {
+            @Override
+            public Object call() throws Exception {
+                final Thread t = Thread.currentThread();
+                t.setContextClassLoader(loader);
+                final ClassLoader old = t.getContextClassLoader();
+                try {
+                    return doCreateResource(serviceInfo);
+                } finally {
+                    t.setContextClassLoader(old);
+                }
+            }
+        });
+    }
+
+    private Object doCreateResource(final ResourceInfo serviceInfo) throws OpenEJBException {
         final ObjectRecipe serviceRecipe = createRecipe(serviceInfo);
         final boolean properties = PropertiesFactory.class.getName().equals(serviceInfo.className);
         if ("false".equalsIgnoreCase(serviceInfo.properties.getProperty("SkipImplicitAttributes", "false")) && !properties) {
@@ -2459,7 +2504,7 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
                 final InputStream is = new ByteArrayInputStream(serviceInfo.properties.getProperty("Definition").getBytes());
                 final Properties p = new SuperProperties();
                 IO.readProperties(is, p);
-                for (final Map.Entry<Object, Object> entry : p.entrySet()) {
+                for (final Entry<Object, Object> entry : p.entrySet()) {
                     final String key = entry.getKey().toString();
                     if (!props.containsKey(key)
                         // never override from Definition, just use it to complete the properties set
@@ -2602,7 +2647,7 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
             final Map<String, Object> unsetA = serviceRecipe.getUnsetProperties();
             final Map<String, Object> unsetB = connectionManagerRecipe.getUnsetProperties();
             final Map<String, Object> unset = new HashMap<String, Object>();
-            for (final Map.Entry<String, Object> entry : unsetA.entrySet()) {
+            for (final Entry<String, Object> entry : unsetA.entrySet()) {
                 if (unsetB.containsKey(entry.getKey())) {
                     unset.put(entry.getKey(), entry.getValue());
                 }
@@ -2666,26 +2711,7 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
         } else if (!Properties.class.isInstance(service)) {
             logUnusedProperties(serviceRecipe, serviceInfo);
         }
-
-        bindResource(serviceInfo.id, service);
-        for (final String alias : serviceInfo.aliases) {
-            bindResource(alias, service);
-        }
-        if (serviceInfo.originAppName != null && !serviceInfo.originAppName.isEmpty() && !"/".equals(serviceInfo.originAppName)
-            && !serviceInfo.id.startsWith("global")) {
-            final String baseJndiName = serviceInfo.id.substring(serviceInfo.originAppName.length() + 1);
-            serviceInfo.aliases.add(baseJndiName);
-            final ContextualJndiReference ref = new ContextualJndiReference(baseJndiName);
-            ref.addPrefix(serviceInfo.originAppName);
-            bindResource(baseJndiName, ref);
-        }
-
-        // Update the config tree
-        config.facilities.resources.add(serviceInfo);
-
-        if (logger.isDebugEnabled()) { // weird to check parent logger but save time and it is almost never activated
-            logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
-        }
+        return service;
     }
 
     private void bindResource(final String id, final Object service) throws OpenEJBException {
@@ -3139,4 +3165,18 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
             }
         }
     }
+
+    public static class LazyResource extends LazyObjectReference<Object> {
+        public LazyResource(final Callable<Object> creator) {
+            super(creator);
+        }
+
+        Object writeReplace() throws ObjectStreamException {
+            try {
+                return getObject();
+            } catch (final NamingException e) {
+                return null;
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/2b385bb5/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
index b415e6a..fd8c3ec 100644
--- a/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
+++ b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
@@ -1125,6 +1125,7 @@
     KeepAlive = 5 s
     Queue = 15
     ThreadFactory = org.apache.openejb.threads.impl.ManagedThreadFactoryImpl
+    Lazy = true
   </ServiceProvider>
   <ServiceProvider id="Default Scheduled Executor Service"
                    service="Resource"
@@ -1133,6 +1134,8 @@
                    class-name="org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory">
     Core = 5
     ThreadFactory = org.apache.openejb.threads.impl.ManagedThreadFactoryImpl
+    Lazy = true
+    SkipImplicitAttributes = true
   </ServiceProvider>
   <ServiceProvider id="Default Managed Thread Factory"
                    service="Resource"
@@ -1140,10 +1143,14 @@
                    factory-name="create"
                    class-name="org.apache.openejb.resource.thread.ManagedThreadFactoryImplFactory">
     Prefix = openejb-managed-thread-
+    Lazy = true
+    SkipImplicitAttributes = true
   </ServiceProvider>
   <ServiceProvider id="Default Context Service"
                    service="Resource"
                    types="ContextService, javax.enterprise.concurrent.ContextService"
-                   class-name="org.apache.openejb.threads.impl.ContextServiceImpl" />
+                   class-name="org.apache.openejb.threads.impl.ContextServiceImpl">
+    SkipImplicitAttributes = true
+  </ServiceProvider>
 
 </ServiceJar>

http://git-wip-us.apache.org/repos/asf/tomee/blob/2b385bb5/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyResourceTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyResourceTest.java b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyResourceTest.java
new file mode 100644
index 0000000..537585e
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyResourceTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.assembler.classic;
+
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.ContainerProperties;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.naming.NamingException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@Classes
+@RunWith(ApplicationComposer.class)
+@ContainerProperties({
+        @ContainerProperties.Property(name = "r1", value = "new://Resource?class-name=org.apache.openejb.assembler.classic.LazyResourceTest$MyResource1"),
+        @ContainerProperties.Property(name = "r2", value = "new://Resource?class-name=org.apache.openejb.assembler.classic.LazyResourceTest$MyResource2"),
+        @ContainerProperties.Property(name = "r2.Lazy", value = "true")
+})
+public class LazyResourceTest {
+    @Test
+    public void lazy() throws NamingException {
+        assertEquals(1, MyResource1.count);
+        assertEquals(0, MyResource2.count);
+        assertTrue(MyResource2.class.isInstance(
+                SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("openejb/Resource/r2")));
+        assertEquals(1, MyResource2.count);
+    }
+
+    public static class MyResource1 {
+        public static volatile int count = 0;
+
+        public MyResource1() {
+            count++;
+        }
+    }
+    public static class MyResource2 {
+        public static volatile int count = 0;
+
+        public MyResource2() {
+            count++;
+        }
+    }
+}