Skip to content

Commit

Permalink
Implement intercept() on Interceptor, correct handling of meta-annota…
Browse files Browse the repository at this point in the history
…tions (on both classes and interceptors), adding support for stereotypes + test.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3854 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
mbogoevici committed Oct 5, 2009
1 parent b636e33 commit b30a7b3
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 26 deletions.
14 changes: 3 additions & 11 deletions impl/src/main/java/org/jboss/webbeans/bean/InterceptorImpl.java
Expand Up @@ -19,6 +19,7 @@

import org.jboss.interceptor.model.InterceptorClassMetadata;
import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.introspector.WBClass;
import org.jboss.webbeans.metadata.cache.MetaAnnotationStore;
Expand All @@ -43,16 +44,7 @@ protected InterceptorImpl(WBClass<T> type, BeanManagerImpl manager)
{
super(type, new StringBuilder().append(Interceptor.class.getSimpleName()).append(BEAN_ID_SEPARATOR).append(type.getName()).toString(), manager);
this.interceptorClassMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(type.getJavaClass());
this.interceptorBindingTypes = new HashSet<Annotation>();
for (Annotation annotation: getAnnotatedItem().getAnnotations())
{
if (manager.isInterceptorBindingType(annotation.annotationType()))
{
interceptorBindingTypes.add(annotation);
interceptorBindingTypes.addAll(getManager().getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
}
}

this.interceptorBindingTypes = flattenInterceptorBindings(manager, getAnnotatedItem().getAnnotations());
}

public static <T> InterceptorImpl<T> of(WBClass<T> type, BeanManagerImpl manager)
Expand All @@ -69,7 +61,7 @@ public Object intercept(InterceptionType type, T instance, InvocationContext ctx
{
try
{
return ctx.proceed();
return new DirectClassInterceptionHandler(instance, getType()).invoke(ctx.getTarget(), org.jboss.interceptor.model.InterceptionType.valueOf(type.name()), ctx);
} catch (Exception e)
{
throw new RuntimeException(e);
Expand Down
40 changes: 25 additions & 15 deletions impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

import org.jboss.interceptor.model.InterceptionModelBuilder;
import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
Expand Down Expand Up @@ -226,14 +227,10 @@ private void initInterceptors()
{
InterceptionModelBuilder<Class<?>, Interceptor> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Interceptor.class);

List<Annotation> classBindingAnnotations = new ArrayList<Annotation>();

for (Annotation annotation : getType().getAnnotations())
Set<Annotation> classBindingAnnotations = flattenInterceptorBindings(manager, annotatedItem.getAnnotations());
for (Class<? extends Annotation> annotation: getStereotypes())
{
if (manager.isInterceptorBindingType(annotation.annotationType()))
{
classBindingAnnotations.add(annotation);
}
classBindingAnnotations.addAll(flattenInterceptorBindings(manager, manager.getStereotypeDefinition(annotation)));
}

builder.interceptPostConstruct().with(manager.resolveInterceptors(InterceptionType.POST_CONSTRUCT, classBindingAnnotations.toArray(new Annotation[0])).toArray(new Interceptor<?>[]{}));
Expand All @@ -243,14 +240,7 @@ private void initInterceptors()
for (WBMethod<?, ?> method : businessMethods)
{
List<Annotation> methodBindingAnnotations = new ArrayList<Annotation>(classBindingAnnotations);
for (Annotation annotation : method.getAnnotations())
{
if (manager.isInterceptorBindingType(annotation.annotationType()))
{
methodBindingAnnotations.add(annotation);
methodBindingAnnotations.addAll(manager.getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
}
}
methodBindingAnnotations.addAll(flattenInterceptorBindings(manager, method.getAnnotations()));
List<Interceptor<?>> methodBoundInterceptors = manager.resolveInterceptors(InterceptionType.AROUND_INVOKE, methodBindingAnnotations.toArray(new Annotation[]{}));
builder.interceptAroundInvoke(((AnnotatedMethod)method).getJavaMember()).with(methodBoundInterceptors.toArray(new Interceptor[]{}));
}
Expand Down Expand Up @@ -446,4 +436,24 @@ public InterceptionHandler createFor(final Interceptor interceptor)
return instance;
}

/**
* Extracts the complete set of interception bindings from a given set of annotations.
*
* @param manager
* @param annotations
* @return
*/
protected static Set<Annotation> flattenInterceptorBindings(BeanManagerImpl manager, Set<Annotation> annotations)
{
Set<Annotation> foundInterceptionBindingTypes = new HashSet<Annotation>();
for (Annotation annotation: annotations)
{
if (manager.isInterceptorBindingType(annotation.annotationType()))
{
foundInterceptionBindingTypes.add(annotation);
foundInterceptionBindingTypes.addAll(manager.getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
}
}
return foundInterceptionBindingTypes;
}
}
@@ -0,0 +1,45 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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.jboss.webbeans.test.unit.interceptor.simple;

import javax.annotation.PostConstruct;

/**
* @author <a href="mailto:mariusb@redhat.com">Marius Bogoevici</a>
*/
@SimpleInterceptorStereotype
public class SimpleBeanWithStereotype
{

public static boolean businessMethodInvoked = false;

public static boolean postConstructCalled = false;

public String doSomething()
{
businessMethodInvoked = true;
return "Hello!";
}

@PostConstruct
public void doPostConstruct()
{
postConstructCalled = true;
}

}
@@ -0,0 +1,35 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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.jboss.webbeans.test.unit.interceptor.simple;

import javax.enterprise.inject.stereotype.Stereotype;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

/**
* @author <a href="mailto:mariusb@redhat.com">Marius Bogoevici</a>
*/
@Stereotype
@SecondaryInterceptionBinding
@Target(TYPE)
@Retention(RUNTIME)
public @interface SimpleInterceptorStereotype
{
}
Expand Up @@ -61,6 +61,22 @@ public void testSimpleInterceptor()
assert !SimpleInterceptor.preDestroyCalled;
assert TwoBindingsInterceptor.aroundInvokeCalled;
assert SimpleBeanImpl.postConstructCalled;
}

@Test
public void testSimpleInterceptorWithStereotype()
{
Bean bean = getCurrentManager().getBeans(SimpleBeanWithStereotype.class).iterator().next();
CreationalContext creationalContext = getCurrentManager().createCreationalContext(bean);
SimpleBeanWithStereotype simpleBean = (SimpleBeanWithStereotype)bean.create(creationalContext);
String result = simpleBean.doSomething();
assert "Hello!".equals(result);
bean.destroy(simpleBean, creationalContext);
assert SimpleInterceptor.aroundInvokeCalled;
assert SimpleInterceptor.postConstructCalled;
assert SimpleInterceptor.preDestroyCalled;
assert TwoBindingsInterceptor.aroundInvokeCalled;
assert SimpleBeanWithStereotype.postConstructCalled;

}
}

0 comments on commit b30a7b3

Please sign in to comment.