You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2013/01/15 01:12:32 UTC
svn commit: r1433228 - in
/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans:
component/ component/creation/ intercept/ portable/
Author: struberg
Date: Tue Jan 15 00:12:32 2013
New Revision: 1433228
URL: http://svn.apache.org/viewvc?rev=1433228&view=rev
Log:
OWB-344 add PostConstruct and PreDestroy lifecycle support to InjectionTargetImpl
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjectionTargetBeanBuilder.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ManagedBeanBuilder.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/LifecycleInterceptorInvocationContext.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractEjbInjectionTarget.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ExtensionProducer.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java Tue Jan 15 00:12:32 2013
@@ -153,7 +153,18 @@ public abstract class AbstractOwbBean<T>
creationalContext = webBeansContext.getCreationalContextFactory().wrappedCreationalContext(
creationalContext, this);
}
-
+
+ if (this instanceof InjectionTargetBean)
+ {
+ //X TODO this is a workaround until the producer solution is finally fixed
+ InjectionTargetBean<T> it = ((InjectionTargetBean<T>) this);
+ instance = it.getInjectionTarget().produce(creationalContext);
+ it.getInjectionTarget().inject(instance, creationalContext);
+ it.getInjectionTarget().postConstruct(instance);
+
+ return instance;
+ }
+
//If wrapper not null
if(producer != null)
{
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjectionTargetBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjectionTargetBeanBuilder.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjectionTargetBeanBuilder.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjectionTargetBeanBuilder.java Tue Jan 15 00:12:32 2013
@@ -677,10 +677,12 @@ public abstract class AbstractInjectionT
protected InjectionTarget<T> buildInjectionTarget(AnnotatedType<T> annotatedType,
Set<InjectionPoint> points,
- WebBeansContext webBeansContext)
+ WebBeansContext webBeansContext,
+ List<AnnotatedMethod<?>> postConstructMethods,
+ List<AnnotatedMethod<?>> preDestroyMethods)
{
//X TODO set interceptor information
- return new InjectionTargetImpl<T>(annotatedType, points, webBeansContext);
+ return new InjectionTargetImpl<T>(annotatedType, points, webBeansContext, postConstructMethods, preDestroyMethods);
}
protected abstract I createBean(Set<Type> types,
@@ -706,12 +708,23 @@ public abstract class AbstractInjectionT
I bean = createBean(types, qualifiers, scope, name, nullable, beanClass, stereotypes, alternative, enabled);
//X TODO hack to set the InjectionTarget
- InjectionTarget<T> injectionTarget = buildInjectionTarget(bean.getAnnotatedType(), bean.getInjectionPoints(), webBeansContext);
+ InjectionTarget<T> injectionTarget
+ = buildInjectionTarget(bean.getAnnotatedType(), bean.getInjectionPoints(), webBeansContext, getPostConstructMethods(), getPreDestroyMethods());
bean.setInjectionTarget(injectionTarget);
return bean;
}
+ protected List<AnnotatedMethod<?>> getPostConstructMethods()
+ {
+ return null;
+ }
+
+ protected List<AnnotatedMethod<?>> getPreDestroyMethods()
+ {
+ return null;
+ }
+
public boolean isEnabled()
{
return enabled;
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ManagedBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ManagedBeanBuilder.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ManagedBeanBuilder.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ManagedBeanBuilder.java Tue Jan 15 00:12:32 2013
@@ -21,10 +21,14 @@ package org.apache.webbeans.component.cr
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.List;
import java.util.Set;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.enterprise.inject.spi.AnnotatedConstructor;
+import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.InjectionPoint;
@@ -118,6 +122,63 @@ public class ManagedBeanBuilder<T, M ext
return getBean();
}
+ @Override
+ protected List<AnnotatedMethod<?>> getPostConstructMethods()
+ {
+ List<AnnotatedMethod<?>> postConstructMethods = new ArrayList<AnnotatedMethod<?>>();
+
+ AnnotatedType<T> annotatedType = getAnnotated();
+ List<Class> classes = getReverseClassHierarchy();
+ for (Class clazz : classes)
+ {
+ for (AnnotatedMethod annotatedMethod : annotatedType.getMethods())
+ {
+ if (annotatedMethod.getDeclaringType().getJavaClass() != clazz)
+ {
+ continue;
+ }
+
+ if (annotatedMethod.isAnnotationPresent(PostConstruct.class))
+ {
+ //X TODO check criterias!
+
+ postConstructMethods.add(annotatedMethod);
+ }
+ }
+ }
+
+ return postConstructMethods;
+ }
+
+ @Override
+ protected List<AnnotatedMethod<?>> getPreDestroyMethods()
+ {
+ List<AnnotatedMethod<?>> preDestroyMethods = new ArrayList<AnnotatedMethod<?>>();
+
+ AnnotatedType<T> annotatedType = getAnnotated();
+ List<Class> classes = getReverseClassHierarchy();
+ for (Class clazz : classes)
+ {
+ for (AnnotatedMethod annotatedMethod : annotatedType.getMethods())
+ {
+ if (annotatedMethod.getDeclaringType().getJavaClass() != clazz)
+ {
+ continue;
+ }
+
+ if (annotatedMethod.isAnnotationPresent(PreDestroy.class))
+ {
+ //X TODO check criterias!
+
+ // reverse invocation order for PreDestroy methods!
+ preDestroyMethods.add(0, annotatedMethod);
+ }
+ }
+ }
+
+ return preDestroyMethods;
+ }
+
/**
* @deprecated replaced via the various {@link InterceptorBeanBuilder}s
*/
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java Tue Jan 15 00:12:32 2013
@@ -134,6 +134,10 @@ public class InterceptorResolutionServic
{
businessMethodInterceptorInfos.put(annotatedMethod.getJavaMember(), methodInterceptorInfo);
}
+ else
+ {
+ //X TODO pick up non-business interceptors
+ }
}
return new BeanInterceptorInfo(decorators, allUsedCdiInterceptors, businessMethodInterceptorInfos);
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/LifecycleInterceptorInvocationContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/LifecycleInterceptorInvocationContext.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/LifecycleInterceptorInvocationContext.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/LifecycleInterceptorInvocationContext.java Tue Jan 15 00:12:32 2013
@@ -18,16 +18,96 @@
*/
package org.apache.webbeans.intercept;
+import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.InterceptionType;
+import javax.enterprise.inject.spi.Interceptor;
+import javax.interceptor.InvocationContext;
import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
/**
* InvocationContext for lifecycle methods like @PostConstruct, etc.
*/
-public class LifecycleInterceptorInvocationContext<T> extends AbstractInvocationContext<T>
+public class LifecycleInterceptorInvocationContext<T> implements InvocationContext
{
- public LifecycleInterceptorInvocationContext(T target, Method method, Object[] parameters)
+ private T target;
+ private InterceptionType type;
+ private List<Interceptor<?>> interceptors;
+ private Map<Interceptor<?>, ?> instances;
+ private Map<String, Object> contextData;
+ private int interceptorIndex = 0;
+ private List<AnnotatedMethod<?>> lifecycleMethods;
+
+ public LifecycleInterceptorInvocationContext(T target, InterceptionType type, List<Interceptor<?>> interceptors, Map<Interceptor<?>, ?> instances,
+ List<AnnotatedMethod<?>> lifecycleMethods)
+ {
+ this.target = target;
+ this.type = type;
+ this.interceptors = interceptors;
+ this.instances = instances;
+ this.lifecycleMethods = lifecycleMethods;
+ }
+
+ public T getTarget()
+ {
+ return target;
+ }
+
+ public Map<String, Object> getContextData()
+ {
+ return contextData;
+ }
+
+ public void setContextData(Map<String, Object> contextData)
+ {
+ this.contextData = contextData;
+ }
+
+ @Override
+ public Object proceed() throws Exception
{
- super(target, method, parameters);
+ if (interceptors != null && interceptorIndex < interceptors.size())
+ {
+ Interceptor interceptor = interceptors.get(interceptorIndex++);
+ return interceptor.intercept(type, instances.get(interceptor), this);
+ }
+ else
+ {
+ for (AnnotatedMethod<?> lifecycleMethod : lifecycleMethods)
+ { Method m = lifecycleMethod.getJavaMember();
+ if (!m.isAccessible())
+ {
+ m.setAccessible(true);
+ }
+ m.invoke(getTarget());
+ }
+
+ return null;
+ }
}
+
+ @Override
+ public Method getMethod()
+ {
+ return null;
+ }
+
+ @Override
+ public Object[] getParameters()
+ {
+ return new Object[0];
+ }
+
+ @Override
+ public void setParameters(Object[] parameters)
+ {
+ }
+
+ @Override
+ public Object getTimer()
+ {
+ return null;
+ }
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractEjbInjectionTarget.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractEjbInjectionTarget.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractEjbInjectionTarget.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractEjbInjectionTarget.java Tue Jan 15 00:12:32 2013
@@ -33,7 +33,7 @@ public abstract class AbstractEjbInjecti
Set<InjectionPoint> points,
WebBeansContext webBeansContext)
{
- super(annotatedType, points, webBeansContext);
+ super(annotatedType, points, webBeansContext, null, null);
}
public abstract T produce(CreationalContext<T> creationalContext);
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ExtensionProducer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ExtensionProducer.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ExtensionProducer.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/ExtensionProducer.java Tue Jan 15 00:12:32 2013
@@ -36,7 +36,7 @@ public class ExtensionProducer<T> extend
Set<InjectionPoint> points,
WebBeansContext webBeansContext)
{
- super(annotatedType, points, webBeansContext);
+ super(annotatedType, points, webBeansContext, null, null);
}
@Override
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java?rev=1433228&r1=1433227&r2=1433228&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java Tue Jan 15 00:12:32 2013
@@ -25,6 +25,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
@@ -38,14 +39,18 @@ import javax.enterprise.inject.spi.Annot
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.InterceptionType;
import javax.inject.Inject;
+import javax.interceptor.InvocationContext;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.context.creational.CreationalContextImpl;
import org.apache.webbeans.inject.InjectableConstructor;
import org.apache.webbeans.inject.InjectableField;
import org.apache.webbeans.inject.InjectableMethod;
+import org.apache.webbeans.intercept.LifecycleInterceptorInvocationContext;
import org.apache.webbeans.util.Asserts;
+import org.apache.webbeans.util.ExceptionUtil;
public class InjectionTargetImpl<T> extends AbstractProducer<T> implements InjectionTarget<T>
@@ -61,7 +66,7 @@ public class InjectionTargetImpl<T> exte
* This methods must have the signature <code>void METHOD();</code>
* They are ordered as <b>superclass first</b>.
*/
- private AnnotatedMethod<T>[] postConstructMethods;
+ private List<AnnotatedMethod<?>> postConstructMethods;
/**
* If the InjectionTarget has a @PreDestroy method, <code>null</code> if not.
@@ -69,15 +74,18 @@ public class InjectionTargetImpl<T> exte
* This methods must have the signature <code>void METHOD();</code>
* They are ordered as <b>subclass first</b>.
*/
- private AnnotatedMethod<T>[] preDestroyMethods;
+ private List<AnnotatedMethod<?>> preDestroyMethods;
- public InjectionTargetImpl(AnnotatedType<T> annotatedType, Set<InjectionPoint> points, WebBeansContext webBeansContext)
+ public InjectionTargetImpl(AnnotatedType<T> annotatedType, Set<InjectionPoint> points, WebBeansContext webBeansContext,
+ List<AnnotatedMethod<?>> postConstructMethods, List<AnnotatedMethod<?>> preDestroyMethods)
{
super(points);
Asserts.assertNotNull(annotatedType);
Asserts.assertNotNull(webBeansContext);
type = annotatedType;
context = webBeansContext;
+ this.postConstructMethods = postConstructMethods;
+ this.preDestroyMethods = preDestroyMethods;
}
@Override
@@ -161,11 +169,39 @@ public class InjectionTargetImpl<T> exte
@Override
public void postConstruct(T instance)
{
+ if (postConstructMethods == null)
+ {
+ return;
+ }
+
+ InvocationContext ic = new LifecycleInterceptorInvocationContext<T>(instance, InterceptionType.POST_CONSTRUCT, null, null, postConstructMethods);
+ try
+ {
+ ic.proceed();
+ }
+ catch (Exception e)
+ {
+ ExceptionUtil.throwAsRuntimeException(e);
+ }
}
@Override
public void preDestroy(T instance)
{
+ if (preDestroyMethods == null)
+ {
+ return;
+ }
+
+ InvocationContext ic = new LifecycleInterceptorInvocationContext<T>(instance, InterceptionType.PRE_DESTROY, null, null, preDestroyMethods);
+ try
+ {
+ ic.proceed();
+ }
+ catch (Exception e)
+ {
+ ExceptionUtil.throwAsRuntimeException(e);
+ }
}
private AnnotatedConstructor<T> getConstructor()