Skip to content

Commit

Permalink
Scan EJB3-style interceptors for EJBs as well (don't handle them), so…
Browse files Browse the repository at this point in the history
… that we can validate passivation inconsistencies.

Make sure that interceptor bindings are recognized according to the spec wrt @target (only METHOD and TYPE), meta-binding rules are observed.
  • Loading branch information
mbogoevici committed Oct 19, 2009
1 parent e0b80d4 commit f07d531
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 63 deletions.
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/Validator.java
Expand Up @@ -122,7 +122,7 @@ private void validateRIBean(RIBean<?> bean, BeanManagerImpl beanManager, Collect
validateCdiBoundInterceptors(beanManager, classBean);
}
// validate EJB-defined interceptors
if (classBean instanceof ManagedBean && ((ManagedBean)classBean).hasDirectlyDefinedInterceptors())
if (((AbstractClassBean<?>) bean).hasDirectlyDefinedInterceptors())
{
validateDirectlyDefinedInterceptorClasses(beanManager, classBean);
}
Expand Down
70 changes: 65 additions & 5 deletions impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
Expand Up @@ -40,6 +40,9 @@
import javax.inject.Scope;

import org.jboss.interceptor.model.InterceptionModelBuilder;
import org.jboss.interceptor.model.InterceptionModel;
import org.jboss.interceptor.model.InterceptorClassMetadataImpl;
import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.DefinitionException;
import org.jboss.weld.DeploymentException;
Expand All @@ -57,6 +60,7 @@
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Strings;
import org.jboss.weld.util.Reflections;

/**
* An abstract bean representation common for class-based beans
Expand Down Expand Up @@ -125,7 +129,10 @@ public void initialize(BeanDeployerEnvironment environment)
checkType();
initProxyClassForDecoratedBean();
if (isInterceptionCandidate())
initInterceptors();
{
initCdiBoundInterceptors();
initDirectlyDefinedInterceptors();
}
}

protected void checkType()
Expand Down Expand Up @@ -394,9 +401,7 @@ protected static Set<Annotation> flattenInterceptorBindings(BeanManagerImpl mana
return foundInterceptionBindingTypes;
}



protected void initInterceptors()
protected void initCdiBoundInterceptors()
{
if (manager.getCdiInterceptorsRegistry().getInterceptionModel(getType()) == null)
{
Expand Down Expand Up @@ -440,7 +445,12 @@ protected void initInterceptors()
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(toSerializableContextualArray(methodBoundInterceptors));
}
}
manager.getCdiInterceptorsRegistry().registerInterceptionModel(getType(), builder.build());
InterceptionModel<Class<?>,SerializableContextual<Interceptor<?>,?>> serializableContextualInterceptionModel = builder.build();
// if there is at least one applicable interceptor, register it
if (serializableContextualInterceptionModel.getAllInterceptors().size() > 0)
{
manager.getCdiInterceptorsRegistry().registerInterceptionModel(getType(), serializableContextualInterceptionModel);
}
}
}

Expand Down Expand Up @@ -511,5 +521,55 @@ public boolean hasCdiBoundInterceptors()
return false;
}

public boolean hasDirectlyDefinedInterceptors()
{
if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()) != null)
return manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size() > 0;
else
return false;
}

protected void initDirectlyDefinedInterceptors()
{
if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()) == null && InterceptionUtils.supportsEjb3InterceptorDeclaration())
{
InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Class.class);

Class<?>[] classDeclaredInterceptors = null;
if (getAnnotatedItem().isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
Annotation interceptorsAnnotation = getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
classDeclaredInterceptors = Reflections.extractValues(interceptorsAnnotation);
}

if (classDeclaredInterceptors != null)
{
builder.interceptAll().with(classDeclaredInterceptors);
}

List<WeldMethod<?, ?>> businessMethods = Beans.getInterceptableBusinessMethods(getAnnotatedItem());
for (WeldMethod<?, ?> method : businessMethods)
{
boolean excludeClassInterceptors = method.isAnnotationPresent(InterceptionUtils.getExcludeClassInterceptorsAnnotationClass());
Class<?>[] methodDeclaredInterceptors = null;
if (method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
methodDeclaredInterceptors = Reflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
}
if (excludeClassInterceptors)
{
builder.ignoreGlobalInterceptors(((AnnotatedMethod)method).getJavaMember());
}
if (methodDeclaredInterceptors != null)
{
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(methodDeclaredInterceptors);
}
}
InterceptionModel<Class<?>, Class<?>> interceptionModel = builder.build();
if (interceptionModel.getAllInterceptors().size() > 0 || new InterceptorClassMetadataImpl(getType()).isInterceptor())
manager.getClassDeclaredInterceptorsRegistry().registerInterceptionModel(getType(), builder.build());
}
}


}
55 changes: 1 addition & 54 deletions impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -186,7 +186,7 @@ public void initialize(BeanDeployerEnvironment environment)
initEEInjectionPoints();
if (isInterceptionCandidate())
{
initClassInterceptors();
initDirectlyDefinedInterceptors();
}
setInjectionTarget(new InjectionTarget<T>()
{
Expand Down Expand Up @@ -425,14 +425,6 @@ protected boolean isInterceptionCandidate()
return !Beans.isInterceptor(getAnnotatedItem()) && !Beans.isDecorator(getAnnotatedItem());
}

public boolean hasDirectlyDefinedInterceptors()
{
if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()) != null)
return manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size() > 0;
else
return false;
}

protected T applyInterceptors(T instance, final CreationalContext<T> creationalContext)
{
try
Expand All @@ -459,49 +451,4 @@ protected T applyInterceptors(T instance, final CreationalContext<T> creationalC
return instance;
}

protected void initClassInterceptors()
{
if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()) == null && InterceptionUtils.supportsEjb3InterceptorDeclaration())
{
InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Class.class);

Class<?>[] classDeclaredInterceptors = null;
if (getAnnotatedItem().isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
Annotation interceptorsAnnotation = getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
classDeclaredInterceptors = Reflections.extractValues(interceptorsAnnotation);
}

if (classDeclaredInterceptors != null)
{
builder.interceptPostConstruct().with(classDeclaredInterceptors);
builder.interceptPreDestroy().with(classDeclaredInterceptors);
builder.interceptPrePassivate().with(classDeclaredInterceptors);
builder.interceptPostActivate().with(classDeclaredInterceptors);
}

List<WeldMethod<?, ?>> businessMethods = Beans.getInterceptableBusinessMethods(getAnnotatedItem());
for (WeldMethod<?, ?> method : businessMethods)
{
boolean excludeClassInterceptors = method.isAnnotationPresent(InterceptionUtils.getExcludeClassInterceptorsAnnotationClass());
Class<?>[] methodDeclaredInterceptors = null;
if (method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
{
methodDeclaredInterceptors = Reflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
}
if (!excludeClassInterceptors && classDeclaredInterceptors != null)
{
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(classDeclaredInterceptors);
}
if (methodDeclaredInterceptors != null)
{
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(methodDeclaredInterceptors);
}
}
InterceptionModel<Class<?>, Class<?>> interceptionModel = builder.build();
if (interceptionModel.getAllInterceptors().size() > 0 || new InterceptorClassMetadataImpl(getType()).isInterceptor())
manager.getClassDeclaredInterceptorsRegistry().registerInterceptionModel(getType(), builder.build());
}
}

}
Expand Up @@ -22,10 +22,16 @@
import org.jboss.weld.log.Logging;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.collections.Arrays2;
import org.jboss.weld.util.Reflections;
import org.jboss.weld.DefinitionException;

import javax.interceptor.InterceptorBinding;
import javax.enterprise.inject.Nonbinding;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

Expand All @@ -43,9 +49,14 @@ public class InterceptorBindingModel<T extends Annotation> extends AnnotationMod
public InterceptorBindingModel(Class<T> type, ClassTransformer transformer)
{
super(type, transformer);
initNonBindingTypes();
initInterceptionBindingTypes();
this.metaAnnotations = getAnnotatedAnnotation().getAnnotations();
if (isValid())
{
initNonBindingTypes();
initInterceptionBindingTypes();
checkArrayAndAnnotationValuedMembers();
checkMetaAnnotations();
this.metaAnnotations = getAnnotatedAnnotation().getAnnotations();
}
}

protected Set<Class<? extends Annotation>> getMetaAnnotationTypes()
Expand All @@ -68,6 +79,54 @@ protected void initInterceptionBindingTypes()
inheritedInterceptionBindingTypes = getAnnotatedAnnotation().getMetaAnnotations(InterceptorBinding.class);
}

@Override
protected void initValid()
{
super.initValid();
if (!getAnnotatedAnnotation().isAnnotationPresent(Target.class))
{
this.valid = false;
log.debug("#0 is missing @Target", getAnnotatedAnnotation());
}
else
{
ElementType[] targetElementTypes = getAnnotatedAnnotation().getAnnotation(Target.class).value();
if (!Arrays2.unorderedEquals(targetElementTypes, ElementType.TYPE, ElementType.METHOD)
&& !Arrays2.unorderedEquals(targetElementTypes, ElementType.TYPE))
{
this.valid = false;
log.debug("#0 is not declared @Target(TYPE, METHOD) or @Target(TYPE)");
}
}
}

private void checkMetaAnnotations()
{
if (Arrays2.containsAll(getAnnotatedAnnotation().getAnnotation(Target.class).value(), ElementType.METHOD))
{
for (Annotation inheritedBinding: getInheritedInterceptionBindingTypes())
{
if (!Arrays2.containsAll(inheritedBinding.annotationType().getAnnotation(Target.class).value(), ElementType.METHOD))
{
this.valid = false;
log.debug("#0 is declared @Target(TYPE, METHOD), but inherits #1, which is declared @Target(TYPE)",
getAnnotatedAnnotation(), inheritedBinding);
}
}
}
}

private void checkArrayAndAnnotationValuedMembers()
{
for (WeldMethod<?, ?> annotatedMethod : getAnnotatedAnnotation().getMembers())
{
if ((Reflections.isArrayType(annotatedMethod.getJavaClass()) || Annotation.class.isAssignableFrom(annotatedMethod.getJavaClass())) && !nonBindingTypes.contains(annotatedMethod))
{
throw new DefinitionException("Member of array type or annotation type must be annotated @NonBinding " + annotatedMethod);
}
}
}

public Set<Annotation> getInheritedInterceptionBindingTypes()
{
return inheritedInterceptionBindingTypes;
Expand Down

0 comments on commit f07d531

Please sign in to comment.