Author: dblevins Date: Sat Mar 29 04:44:19 2014 New Revision: 1582955 URL: http://svn.apache.org/r1582955 Log: Move injection logic to central place -- should reuse a little more here
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java (with props) Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/OpenEjbContainerTest.java Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java?rev=1582955&view=auto ============================================================================== --- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java (added) +++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java Sat Mar 29 04:44:19 2014 @@ -0,0 +1,143 @@ +/* + * 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.core.Operation; +import org.apache.openejb.core.ThreadContext; +import org.apache.openejb.loader.SystemInstance; +import org.apache.openejb.spi.ContainerSystem; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; +import org.apache.webbeans.inject.OWBInjector; + +/** + * Extracted from the OpenEjbContainer class + */ +public class Injector { + + private static Logger logger = null; // initialized lazily to get the logging config from properties + + public static <T> T inject(final T object) { + + assert object != null; + + final Class<?> clazz = object.getClass(); + + final BeanContext context = resolve(clazz); + + 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 (final OpenEJBException e) { + throw new InjectionException(clazz.getName(), e); + } + } else if (!isAnnotatedLocalClient(clazz)) { // nothing to do + throw new NoInjectionMetaDataException(clazz.getName()); + } + + // 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 (final OpenEJBException e) { + throw new NoInjectionMetaDataException("Injection failed", e); + } + } + + private static <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(context.getWebBeansContext().getBeanManagerImpl(), object, null); + } catch (final Throwable t) { + logger().warning("an error occured while injecting the class '" + object.getClass().getName() + "': " + t.getMessage()); + } finally { + if (context != null) { + ThreadContext.exit(oldContext); + } + } + } + + private static 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 static BeanContext resolve(Class<?> clazz) { + + final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class); + + while (clazz != null && clazz != Object.class) { + + { + final BeanContext context = containerSystem.getBeanContext(clazz.getName()); + + if (context != null) { + return context; + } + } + + for (final BeanContext context : containerSystem.deployments()) { + + if (clazz == context.getBeanClass()) { + return context; + } + + } + + clazz = clazz.getSuperclass(); + } + + return null; + } + + private static Logger logger() { // don't trigger init too eagerly to be sure to be configured + if (logger == null) { + logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, OpenEjbContainer.class); + } + return logger; + } + + public static class NoInjectionMetaDataException extends IllegalStateException { + 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); + } + } + + public static class InjectionException extends IllegalStateException { + public InjectionException(final String message, final Throwable cause) { + super(message, cause); + } + } +} Propchange: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/Injector.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java?rev=1582955&r1=1582954&r2=1582955&view=diff ============================================================================== --- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java (original) +++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java Sat Mar 29 04:44:19 2014 @@ -16,7 +16,6 @@ */ 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; @@ -27,10 +26,8 @@ import org.apache.openejb.config.EjbModu import org.apache.openejb.config.NewLoaderLogic; import org.apache.openejb.config.PersistenceModule; import org.apache.openejb.config.ValidationFailedException; -import org.apache.openejb.core.Operation; import org.apache.openejb.core.ParentClassLoaderFinder; import org.apache.openejb.core.ProvidedClassLoaderFinder; -import org.apache.openejb.core.ThreadContext; import org.apache.openejb.jee.Application; import org.apache.openejb.jee.Beans; import org.apache.openejb.jee.Connector; @@ -44,7 +41,6 @@ import org.apache.openejb.jee.oejb3.EjbD import org.apache.openejb.jee.oejb3.OpenejbJar; import org.apache.openejb.loader.Options; import org.apache.openejb.loader.SystemInstance; -import org.apache.openejb.spi.ContainerSystem; import org.apache.openejb.util.Exceptions; import org.apache.openejb.util.Join; import org.apache.openejb.util.JuliLogStreamFactory; @@ -53,7 +49,6 @@ import org.apache.openejb.util.Logger; import org.apache.openejb.util.OptionsLog; import org.apache.openejb.util.ServiceManagerProxy; import org.apache.webbeans.config.WebBeansContext; -import org.apache.webbeans.inject.OWBInjector; import org.apache.webbeans.web.lifecycle.test.MockHttpSession; import org.apache.webbeans.web.lifecycle.test.MockServletContext; import org.apache.xbean.naming.context.ContextFlyweight; @@ -181,92 +176,6 @@ public final class OpenEjbContainer exte return globalJndiContext; } - public <T> T inject(final T object) { - - assert object != null; - - final Class<?> clazz = object.getClass(); - - final BeanContext context = resolve(clazz); - - 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 (final OpenEJBException e) { - throw new InjectionException(clazz.getName(), e); - } - } else if (!isAnnotatedLocalClient(clazz)) { // nothing to do - throw new NoInjectionMetaDataException(clazz.getName()); - } - - // 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 (final OpenEJBException e) { - throw new NoInjectionMetaDataException("Injection failed", e); - } - } - - 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 (final Throwable t) { - logger().warning("an error occured while injecting the class '" + object.getClass().getName() + "': " + t.getMessage()); - } finally { - if (context != null) { - ThreadContext.exit(oldContext); - } - } - } - - 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) { - - final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class); - - while (clazz != null && clazz != Object.class) { - - { - final BeanContext context = containerSystem.getBeanContext(clazz.getName()); - - if (context != null) { - return context; - } - } - - for (final BeanContext context : containerSystem.deployments()) { - - if (clazz == context.getBeanClass()) { - return context; - } - - } - - clazz = clazz.getSuperclass(); - } - - return null; - } - private void startNetworkServices() { if (!options.get(OPENEJB_EMBEDDED_REMOTABLE, false)) { @@ -288,23 +197,6 @@ public final class OpenEjbContainer exte return logger; } - - public static class NoInjectionMetaDataException extends IllegalStateException { - 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); - } - } - - public static class InjectionException extends IllegalStateException { - public InjectionException(final String message, final Throwable cause) { - super(message, cause); - } - } - public static class Provider implements EJBContainerProvider { public static final String OPENEJB_ADDITIONNAL_CALLERS_KEY = "openejb.additionnal.callers"; @@ -705,7 +597,7 @@ public final class OpenEjbContainer exte @Override public void bind(final Name name, final Object obj) throws NamingException { if (name.size() == 1 && "inject".equals(name.get(0))) { - inject(obj); + Injector.inject(obj); } else { super.bind(name, obj); } @@ -714,7 +606,7 @@ public final class OpenEjbContainer exte @Override public void bind(final String name, final Object obj) throws NamingException { if (name != null && "inject".equals(name)) { - inject(obj); + Injector.inject(obj); } else { super.bind(name, obj); } Modified: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/OpenEjbContainerTest.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/OpenEjbContainerTest.java?rev=1582955&r1=1582954&r2=1582955&view=diff ============================================================================== --- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/OpenEjbContainerTest.java (original) +++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/OpenEjbContainerTest.java Sat Mar 29 04:44:19 2014 @@ -44,7 +44,7 @@ public class OpenEjbContainerTest extend OpenEjbContainer openEjbContainer = (OpenEjbContainer) EJBContainer.createEJBContainer(map); - openEjbContainer.inject(this); + Injector.inject(this); assertNotNull(widget); @@ -78,7 +78,7 @@ public class OpenEjbContainerTest extend OpenEjbContainer openEjbContainer = (OpenEjbContainer) EJBContainer.createEJBContainer(map); try { - openEjbContainer.inject(this); + Injector.inject(this); assertNotNull(widget);