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 2012/09/21 13:44:25 UTC

svn commit: r1388441 - in /openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb: ClientInjections.java OpenEjbContainer.java core/LocalInitialContext.java

Author: rmannibucau
Date: Fri Sep 21 11:44:24 2012
New Revision: 1388441

URL: http://svn.apache.org/viewvc?rev=1388441&view=rev
Log:
OPENEJB-1901 @LocalClient and EJBContainer

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClientInjections.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContext.java

Added: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClientInjections.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClientInjections.java?rev=1388441&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClientInjections.java (added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClientInjections.java Fri Sep 21 11:44:24 2012
@@ -0,0 +1,70 @@
+/*
+ * 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;
+
+import org.apache.openejb.api.LocalClient;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import java.util.List;
+
+public final class ClientInjections {
+    private ClientInjections() {
+        // no-op
+    }
+
+    public static InjectionProcessor<?> clientInjector(final Object object) throws OpenEJBException {
+        if (object == null) {
+            throw new NullPointerException("Object supplied to 'inject' operation is null");
+        }
+
+        Context clients;
+        try {
+            clients = (Context) SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext()
+                    .lookup("openejb/client/");
+        } catch (NamingException e) {
+            throw new OpenEJBException(object.getClass().getName(), e);
+        }
+
+        Context ctx = null;
+        List<Injection> injections = null;
+
+        Class<?> current = object.getClass();
+        while (current != null && !current.equals(Object.class)) {
+            try {
+                String moduleId = (String) clients.lookup(current.getName());
+                ctx = (Context) clients.lookup(moduleId);
+                injections = (List<Injection>) ctx.lookup("info/injections");
+                break;
+            } catch (NamingException e) {
+                current = current.getSuperclass();
+            }
+        }
+
+        if (injections == null) {
+            throw new OpenEJBException("Unable to find injection meta-data for "
+                    + object.getClass().getName()
+                    + ".  Ensure that class was annotated with @"
+                    + LocalClient.class.getName()+" and was successfully discovered and deployed. "
+                    + " See http://openejb.apache.org/3.0/local-client-injection.html");
+        }
+
+        return new InjectionProcessor(object, injections, ctx);
+    }
+}

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java?rev=1388441&r1=1388440&r2=1388441&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java Fri Sep 21 11:44:24 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.openejb;
 
+import org.apache.openejb.api.LocalClient;
 import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.assembler.classic.Assembler;
 import org.apache.openejb.config.AppModule;
@@ -173,27 +174,54 @@ public class OpenEjbContainer extends EJ
 
         final BeanContext context = resolve(clazz);
 
-        if (context == null) throw new NoInjectionMetaDataException(clazz.getName());
+        if (context != null) { // found the test class directly
+            final InjectionProcessor processor = new InjectionProcessor(object, context.getInjections(), context.getJndiContext());
+            cdiInjections(context, object);
+            try {
+                return (T) processor.createInstance();
+            } catch (OpenEJBException e) {
+                throw new InjectionException(clazz.getName(), e);
+            }
+        } else if (!isAnnotatedLocalClient(clazz)) { // nothing to do
+            throw new NoInjectionMetaDataException(clazz.getName());
+        }
 
-        final InjectionProcessor processor = new InjectionProcessor(object, context.getInjections(), context.getJndiContext());
+        // the test class was not found in beans (OpenEJB ran from parent) but was annotated @LocalClient
+        try {
+            final InjectionProcessor<?> processor = ClientInjections.clientInjector(object);
+            cdiInjections(null, object);
+            return (T) processor.createInstance();
+        } catch (OpenEJBException e) {
+            throw new NoInjectionMetaDataException("Injection failed", e);
+        }
+    }
 
-        final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
-        final ThreadContext oldContext = ThreadContext.enter(callContext);
+    private <T> void cdiInjections(final BeanContext context, final T object) {
+        ThreadContext oldContext = null;
+        if (context != null) {
+            final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
+            oldContext = ThreadContext.enter(callContext);
+        }
         try {
             OWBInjector.inject(webBeanContext.getBeanManagerImpl(), object, null);
         } catch (Throwable t) {
-            logger.warning("an error occured while injecting the class '" + clazz.getName() + "': " + t.getMessage());
-            // TODO handle this differently
-            // this is temporary till the injector can be rewritten
+            logger.warning("an error occured while injecting the class '" + object.getClass().getName() + "': " + t.getMessage());
         } finally {
-            ThreadContext.exit(oldContext);
+            if (oldContext != null) {
+                ThreadContext.exit(oldContext);
+            }
         }
+    }
 
-        try {
-            return (T) processor.createInstance();
-        } catch (OpenEJBException e) {
-            throw new InjectionException(clazz.getName(), e);
+    private boolean isAnnotatedLocalClient(final Class<?> clazz) {
+        Class<?> current = clazz;
+        while (current != null && current != Object.class) {
+            if (current.getAnnotation(LocalClient.class) != null) {
+                return true;
+            }
+            current = current.getSuperclass();
         }
+        return false;
     }
 
     private BeanContext resolve(Class<?> clazz) {
@@ -236,8 +264,12 @@ public class OpenEjbContainer extends EJ
 
 
     public static class NoInjectionMetaDataException extends IllegalStateException {
-        public NoInjectionMetaDataException(String s) {
-            super(String.format("%s : Annotate the class with @%s so it can be discovered in the application scanning process", s, javax.annotation.ManagedBean.class.getName()));
+        public NoInjectionMetaDataException(final String s) {
+            this(s, null);
+        }
+
+        public NoInjectionMetaDataException(final String s, final Exception e) {
+            super(String.format("%s : Annotate the class with @%s so it can be discovered in the application scanning process", s, javax.annotation.ManagedBean.class.getName()), e);
         }
     }
 

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContext.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContext.java?rev=1388441&r1=1388440&r2=1388441&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContext.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContext.java Fri Sep 21 11:44:24 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.openejb.core;
 
+import org.apache.openejb.ClientInjections;
 import org.apache.openejb.Core;
 import org.apache.openejb.OpenEJB;
 import org.apache.openejb.Injection;
@@ -182,31 +183,8 @@ public class LocalInitialContext extends
     }
 
     private void inject(Object obj) throws NamingException {
-        if (obj == null) throw new NullPointerException("Object supplied to 'inject' operation is null");
-        Class clazz = obj.getClass();
-
-        Context clients = (Context) getRoot().lookup("openejb/client/");
-
-        Context context = null;
-        List<Injection> injections = null;
-
-        while (clazz != null && !clazz.equals(Object.class)) {
-            try {
-                String moduleId = (String) clients.lookup(clazz.getName());
-                context = (Context) clients.lookup(moduleId);
-                injections = (List<Injection>) context.lookup("info/injections");
-                break;
-            } catch (NamingException e) {
-                clazz = clazz.getSuperclass();
-            }
-        }
-
-        if (injections == null) throw new NamingException("Unable to find injection meta-data for "+obj.getClass().getName()+".  Ensure that class was annotated with @"+ LocalClient.class.getName()+" and was successfully discovered and deployed.  See http://openejb.apache.org/3.0/local-client-injection.html");
-
         try {
-            InjectionProcessor processor = new InjectionProcessor(obj, injections, context);
-
-            processor.createInstance();
+            ClientInjections.clientInjector(obj).createInstance();
         } catch (OpenEJBException e) {
             throw (NamingException) new NamingException("Injection failed").initCause(e);
         }