You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ge...@apache.org on 2010/10/11 16:11:39 UTC
svn commit: r1021350 - in /openwebbeans/trunk:
webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/
webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/
webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/ w...
Author: gerdogdu
Date: Mon Oct 11 14:11:39 2010
New Revision: 1021350
URL: http://svn.apache.org/viewvc?rev=1021350&view=rev
Log:
[OWB-470] https://issues.apache.org/jira/browse/OWB-470
Added:
openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java (with props)
Modified:
openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/BaseEjbBean.java
openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
Modified: openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/BaseEjbBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/BaseEjbBean.java?rev=1021350&r1=1021349&r2=1021350&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/BaseEjbBean.java (original)
+++ openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/component/BaseEjbBean.java Mon Oct 11 14:11:39 2010
@@ -63,15 +63,6 @@ public abstract class BaseEjbBean<T> ext
}
- /**
- * {@inheritDoc}
- */
- @Override
- public void injectFields(T instance, CreationalContext<T> creationalContext)
- {
- //No-operations
- }
-
/* (non-Javadoc)
* @see org.apache.webbeans.component.AbstractBean#isPassivationCapable()
*/
Modified: openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java?rev=1021350&r1=1021349&r2=1021350&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java (original)
+++ openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java Mon Oct 11 14:11:39 2010
@@ -635,6 +635,16 @@ public class OpenWebBeansEjbInterceptor
lifecycleCommon(context, InterceptorType.POST_ACTIVATE);
}
+
+ public static CreationalContext<?> getThreadCreationalContext()
+ {
+ return threadLocalCreationalContext.get();
+ }
+
+ public static BaseEjbBean<?> getEjbBean()
+ {
+ return threadLocal.get();
+ }
private void writeObject(ObjectOutputStream out) throws IOException
{
Added: openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java?rev=1021350&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java (added)
+++ openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java Mon Oct 11 14:11:39 2010
@@ -0,0 +1,202 @@
+/*
+ * 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.webbeans.ejb.common.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.enterprise.context.spi.Contextual;
+import javax.enterprise.context.spi.CreationalContext;
+
+import org.apache.webbeans.container.BeanManagerImpl;
+import org.apache.webbeans.context.creational.CreationalContextFactory;
+import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.ejb.common.component.BaseEjbBean;
+import org.apache.webbeans.ejb.common.interceptor.OpenWebBeansEjbInterceptor;
+import org.apache.webbeans.inject.AbstractInjectable;
+import org.apache.webbeans.inject.OWBInjector;
+
+@SuppressWarnings("unchecked")
+public class CdiDefaultEjbInjector
+{
+ /**Ejb instances per creational context*/
+ private final ConcurrentMap<Object, List<CreationalContextImpl<Object>>> injectedNonContextuals =
+ new ConcurrentHashMap<Object, List<CreationalContextImpl<Object>>>();
+
+ /**Contextual ejb instance per key*/
+ private final ConcurrentMap<Object, CreationalContextImpl<Object>> injectedContextuals =
+ new ConcurrentHashMap<Object, CreationalContextImpl<Object>>();
+
+ private final BeanManagerImpl beanManager;
+
+ /**
+ * Default constructor.
+ */
+ public CdiDefaultEjbInjector()
+ {
+ super();
+ this.beanManager = BeanManagerImpl.getManager();
+ }
+
+ /**
+ * Inject dependencies of given ejb instance.
+ * @param ejbInstance ejb instance
+ * @throws Exception if any exception occurs
+ */
+ public void injectDependenciesOfEjbInstance(Object ejbInstance, Object mapKey) throws Exception
+ {
+ BaseEjbBean<Object> ejbBean = (BaseEjbBean<Object>)OpenWebBeansEjbInterceptor.getEjbBean();
+ if(ejbBean != null)
+ {
+ CreationalContextImpl<Object> contextualCc = (CreationalContextImpl<Object>)OpenWebBeansEjbInterceptor.getThreadCreationalContext();
+ injectDependenciesOfContextualEjb(ejbInstance,ejbBean, contextualCc);
+ this.injectedContextuals.putIfAbsent(mapKey, contextualCc);
+ }
+ else
+ {
+ injectDependenciesOfNonContextualEjb(ejbInstance, mapKey);
+ }
+ }
+
+ /**
+ * Inject dependencies of given interceptor instance.
+ * @param ejbInstance ejb instance
+ * @param interceptorInstance interceptor instance
+ * @throws Exception if any exception occurs
+ */
+ public void injectDependenciesOfEjbInterceptor(Object ejbInstance, Object interceptorInstance, Object mapKey) throws Exception
+ {
+ OWBInjector owbInjector = new OWBInjector();
+ CreationalContextImpl<Object> cc = null;
+ //Look for contextual ejb or not
+ if(this.injectedContextuals.containsKey(mapKey))
+ {
+ cc = this.injectedContextuals.get(mapKey);
+
+ //Add all dependencies of interceptor to ejb bean
+ owbInjector.inject(interceptorInstance,cc);
+ }
+ else
+ {
+ cc = (CreationalContextImpl<Object>)this.beanManager.createCreationalContext(null);
+ owbInjector.inject(interceptorInstance,cc);
+ this.injectedNonContextuals.get(mapKey).add(cc);
+ }
+ }
+
+
+ /**
+ * Release context for noncontextual ejb instance.
+ * @param destroyInstance ejb instance
+ * under destroy
+ * @throws Exception if any exception occurs
+ */
+ public void releaseNonContextualDependents(Object mapKey) throws Exception
+ {
+ if(this.injectedContextuals.containsKey(mapKey))
+ {
+ this.injectedContextuals.remove(mapKey);
+ }
+ else
+ {
+ List<CreationalContextImpl<Object>> ccList = this.injectedNonContextuals.remove(mapKey);
+ if(ccList != null)
+ {
+ for(CreationalContextImpl<Object> cc : ccList)
+ {
+ cc.release();
+ }
+ }
+ }
+ }
+
+ /**
+ * Inject dependencies of contextual ejb bean.
+ * @param instance ejb instance
+ * @param bean ejb bean
+ * @param ctx creational context
+ * @throws Exception if exception occurs
+ */
+ private void injectDependenciesOfContextualEjb(Object instance, BaseEjbBean<Object> bean, CreationalContext<Object> ctx) throws Exception
+ {
+ if(!(ctx instanceof CreationalContextImpl))
+ {
+ ctx = CreationalContextFactory.getInstance().wrappedCreationalContext(ctx, bean);
+ }
+
+ Object oldInstanceUnderInjection = AbstractInjectable.instanceUnderInjection.get();
+ boolean isInjectionToAnotherBean = false;
+ try
+ {
+ Contextual<?> contextual = null;
+ if(ctx instanceof CreationalContextImpl)
+ {
+ contextual = ((CreationalContextImpl<Object>)ctx).getBean();
+ isInjectionToAnotherBean = contextual == bean ? false : true;
+ }
+
+ if(!isInjectionToAnotherBean)
+ {
+ AbstractInjectable.instanceUnderInjection.set(instance);
+ }
+
+ //Injection of @Inject
+ bean.injectSuperFields(instance, ctx);
+ bean.injectSuperMethods(instance, ctx);
+ bean.injectFields(instance, ctx);
+ bean.injectMethods(instance, ctx);
+ }
+ finally
+ {
+ if(oldInstanceUnderInjection != null)
+ {
+ AbstractInjectable.instanceUnderInjection.set(oldInstanceUnderInjection);
+ }
+ else
+ {
+ AbstractInjectable.instanceUnderInjection.set(null);
+ AbstractInjectable.instanceUnderInjection.remove();
+ }
+ }
+ }
+
+ /**
+ * Inject dependencies of non-contextual instance.
+ * @param ejbInstance non contextual instance
+ * @throws Exception if any exception occurs
+ */
+ private void injectDependenciesOfNonContextualEjb(Object instance, Object mapKey) throws Exception
+ {
+ OWBInjector owbInjector = new OWBInjector();
+ CreationalContextImpl<Object> cc = (CreationalContextImpl<Object>)this.beanManager.createCreationalContext(null);
+ owbInjector.inject(instance,cc);
+
+ List<CreationalContextImpl<Object>> ccList = this.injectedNonContextuals.get(mapKey);
+ if(ccList == null)
+ {
+ ccList = new ArrayList<CreationalContextImpl<Object>>();
+ }
+
+ ccList.add(cc);
+ this.injectedNonContextuals.putIfAbsent(mapKey, ccList);
+ }
+
+}
Propchange: openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/util/CdiDefaultEjbInjector.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java?rev=1021350&r1=1021349&r2=1021350&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/OWBInjector.java Mon Oct 11 14:11:39 2010
@@ -22,17 +22,26 @@ import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Event;
import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
+import javax.inject.Provider;
+import org.apache.webbeans.component.EventBean;
import org.apache.webbeans.component.InjectionPointBean;
import org.apache.webbeans.component.InjectionTargetWrapper;
+import org.apache.webbeans.component.InstanceBean;
import org.apache.webbeans.container.BeanManagerImpl;
import org.apache.webbeans.container.InjectionResolver;
import org.apache.webbeans.context.creational.CreationalContextImpl;
@@ -53,46 +62,41 @@ import org.apache.webbeans.util.WebBeans
*/
public final class OWBInjector implements Serializable
{
+ //Serial id
private static final long serialVersionUID = 1L;
+ /**Creational context to hold dependent instances*/
private CreationalContextImpl<?> ownerCreationalContext = null;
+ /**Underlying javaee instance*/
private Object javaEEInstance;
+ /**
+ * Creates a new instance
+ */
public OWBInjector()
{
-
- }
-
- @SuppressWarnings("unchecked")
- public void destroy()
- {
- BeanManagerImpl beanManager = BeanManagerImpl.getManager();
-
- //Look for custom InjectionTarget
- InjectionTargetWrapper<Object> wrapper = beanManager.getInjectionTargetWrapper((Class<Object>)javaEEInstance.getClass());
- if(wrapper != null)
- {
- wrapper.dispose(javaEEInstance);
- this.javaEEInstance = null;
- this.ownerCreationalContext = null;
- }
-
- else
- {
- if(this.ownerCreationalContext != null)
- {
- this.ownerCreationalContext.release();
- this.ownerCreationalContext = null;
- }
- }
+ //No operation
}
+ /**
+ * Inject dependencies of given instance.
+ * @param javaEeComponentInstance instance
+ * @return this injector
+ * @throws Exception if exception occurs
+ */
public OWBInjector inject(Object javaEeComponentInstance) throws Exception
{
return inject(javaEeComponentInstance,null);
}
+ /**
+ * Inject dependencies of given instance.
+ * @param javaEeComponentInstance instance
+ * @param creationalContext context
+ * @return this injector
+ * @throws Exception if exception occurs
+ */
@SuppressWarnings("unchecked")
public OWBInjector inject(Object javaEeComponentInstance, CreationalContext<?> creationalContext) throws Exception
{
@@ -121,45 +125,26 @@ public final class OWBInjector implement
{
for(InjectionPoint injectionPoint : injectionPoints)
{
- boolean injectionPointBeanSet = false;
- try
+ if(injectionPoint.getMember() instanceof Method)
{
- if(injectionPoint.getMember() instanceof Field)
- {
- //Injected contextual beam
- Bean<?> injectedBean = (Bean<?>)InjectionResolver.getInstance().getInjectionPointBean(injectionPoint);
-
- if(WebBeansUtil.isDependent(injectedBean))
- {
- if(!InjectionPoint.class.isAssignableFrom(ClassUtil.getClass(injectionPoint.getType())))
- {
- injectionPointBeanSet = true;
- InjectionPointBean.local.set(injectionPoint);
- }
- }
- }
+ Method method = (Method)injectionPoint.getMember();
- Object object = beanManager.getInjectableReference(injectionPoint, ownerCreationalContext);
+ //Get injected method arguments
+ List<Object> parameters = getInjectedMethodParameterReferences(injectionPoint, beanManager, injectionPoints);
+
+ //Set method
+ ClassUtil.callInstanceMethod(method, javaEeComponentInstance, parameters.toArray(new Object[parameters.size()]));
- if(injectionPoint.getMember() instanceof Method)
- {
- Method method = (Method)injectionPoint.getMember();
- ClassUtil.callInstanceMethod(method, javaEeComponentInstance, new Object[]{object});
-
- }
- else if(injectionPoint.getMember() instanceof Field)
- {
- Field field = (Field)injectionPoint.getMember();
- ClassUtil.setField(javaEeComponentInstance, field, object);
- }
}
- finally
+ else if(injectionPoint.getMember() instanceof Field)
{
- if(injectionPointBeanSet)
- {
- InjectionPointBean.local.remove();
- }
- }
+ //Get injected object ref
+ Object object = getInjectedObjectReference(injectionPoint, beanManager);
+
+ //Set field
+ Field field = (Field)injectionPoint.getMember();
+ ClassUtil.setField(javaEeComponentInstance, field, object);
+ }
}
return this;
@@ -175,6 +160,175 @@ public final class OWBInjector implement
return null;
}
+ /**
+ * Release dependents.
+ */
+ @SuppressWarnings("unchecked")
+ public void destroy()
+ {
+ BeanManagerImpl beanManager = BeanManagerImpl.getManager();
+
+ //Look for custom InjectionTarget
+ InjectionTargetWrapper<Object> wrapper = beanManager.getInjectionTargetWrapper((Class<Object>)javaEEInstance.getClass());
+ if(wrapper != null)
+ {
+ wrapper.dispose(javaEEInstance);
+ this.javaEEInstance = null;
+ this.ownerCreationalContext = null;
+ }
+
+ else
+ {
+ if(this.ownerCreationalContext != null)
+ {
+ this.ownerCreationalContext.release();
+ this.ownerCreationalContext = null;
+ }
+ }
+ }
+
+ /**
+ * Gets injected object reference.
+ * @param injectionPoint injection point of javaee instance
+ * @param beanManager bean manager implementation
+ * @return injected reference
+ */
+ private Object getInjectedObjectReference(InjectionPoint injectionPoint, BeanManagerImpl beanManager)
+ {
+ Object object = null;
+
+ //Injected contextual beam
+ Bean<?> injectedBean = (Bean<?>)InjectionResolver.getInstance().getInjectionPointBean(injectionPoint);
+
+ if(isInstanceProviderInjection(injectionPoint))
+ {
+ InstanceBean.local.set(injectionPoint);
+ }
+
+ else if(isEventProviderInjection(injectionPoint))
+ {
+ EventBean.local.set(injectionPoint);
+ }
+
+ else if(WebBeansUtil.isDependent(injectedBean))
+ {
+ if(!InjectionPoint.class.isAssignableFrom(ClassUtil.getClass(injectionPoint.getType())))
+ {
+ InjectionPointBean.local.set(injectionPoint);
+ }
+ }
+
+ object = beanManager.getInjectableReference(injectionPoint, ownerCreationalContext);
+
+ return object;
+ }
+
+ /**
+ * Gets initializer method parameters.
+ * @param injectionPoint javaee component
+ * injection point
+ * @param beanManager bean manager
+ * @param injectionPoints all injection points
+ * @return injected method injected arguments
+ */
+ private List<Object> getInjectedMethodParameterReferences(InjectionPoint injectionPoint, BeanManagerImpl beanManager, Set<InjectionPoint> injectionPoints)
+ {
+ Method method = (Method)injectionPoint.getMember();
+ List<InjectionPoint> injectedPoints = getInjectedPoints(method, injectionPoints);
+ List<Object> list = new ArrayList<Object>();
+ for(int i=0;i<injectedPoints.size();i++)
+ {
+ for(InjectionPoint point : injectedPoints)
+ {
+ AnnotatedParameter<?> parameter = (AnnotatedParameter<?>)point.getAnnotated();
+ if(parameter.getPosition() == i)
+ {
+ Object instance = getInjectedObjectReference(injectionPoint, beanManager);
+ list.add(instance);
+ break;
+ }
+ }
+ }
+
+ return list;
+ }
+
+ /**
+ * Gets injection point of given methods.
+ * @param method injection point member
+ * @param injectionPoints all injection points
+ * @return method injection points
+ */
+ private List<InjectionPoint> getInjectedPoints(Method method, Set<InjectionPoint> injectionPoints)
+ {
+ List<InjectionPoint> points = new ArrayList<InjectionPoint>();
+
+ for(InjectionPoint ip : injectionPoints)
+ {
+ if(ip.getMember().equals(method))
+ {
+ points.add(ip);
+ }
+ }
+
+ return points;
+
+ }
+
+ /**
+ * Returns true if injection point is instance injection point
+ * false otherwise.
+ * @param injectionPoint injection point
+ * @return true if injection point is instance injection point
+ */
+ private boolean isInstanceProviderInjection(InjectionPoint injectionPoint)
+ {
+ Type type = injectionPoint.getType();
+
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType pt = (ParameterizedType) type;
+ Class<?> clazz = (Class<?>) pt.getRawType();
+
+ if(Provider.class.isAssignableFrom(clazz))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns true if injection point is event injection point
+ * false otherwise.
+ * @param injectionPoint injection point
+ * @return true if injection point is event injection point
+ */
+ private boolean isEventProviderInjection(InjectionPoint injectionPoint)
+ {
+ Type type = injectionPoint.getType();
+
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType pt = (ParameterizedType) type;
+ Class<?> clazz = (Class<?>) pt.getRawType();
+
+ if(clazz.isAssignableFrom(Event.class))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+
+ /**
+ * JavaEE components can not inject {@link InjectionPoint}.
+ * @param clazz javaee component class info
+ * @throws exception if condition is not applied
+ */
public static void checkInjectionPointForInjectInjectionPoint(Class<?> clazz)
{
Asserts.nullCheckForClass(clazz);
@@ -195,6 +349,11 @@ public final class OWBInjector implement
}
}
+ /**
+ * Returns trur for serializable types.
+ * @param clazz class info
+ * @return true if class is serializable
+ */
public static boolean checkInjectionPointForInterceptorPassivation(Class<?> clazz)
{
Asserts.nullCheckForClass(clazz);