Skip to content

Commit

Permalink
Support @Interceptors-bound interceptors on managed beans and EJBs wi…
Browse files Browse the repository at this point in the history
…th @InterceptionBinding.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3865 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
mbogoevici committed Oct 7, 2009
1 parent 9840855 commit fcb1dd7
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 22 deletions.
10 changes: 8 additions & 2 deletions impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -264,7 +264,8 @@ public String toString()
/**
* Interception model
*/
private transient final InterceptorRegistry<Class<?>, Interceptor> boundInterceptorsRegistry = new InterceptorRegistry<Class<?>, Interceptor>();
private transient final InterceptorRegistry<Class<?>, Interceptor<?>> boundInterceptorsRegistry = new InterceptorRegistry<Class<?>, Interceptor<?>>();
private transient final InterceptorRegistry<Class<?>, Class<?>> declaredInterceptorsRegistry = new InterceptorRegistry<Class<?>, Class<?>>();

/**
* Create a new, root, manager
Expand Down Expand Up @@ -1413,8 +1414,13 @@ public void cleanup()
this.currentInjectionPoint.remove();
}

public InterceptorRegistry<Class<?>, Interceptor> getBoundInterceptorsRegistry()
public InterceptorRegistry<Class<?>, Interceptor<?>> getBoundInterceptorsRegistry()
{
return boundInterceptorsRegistry;
}

public InterceptorRegistry<Class<?>, Class<?>> getDeclaredInterceptorsRegistry()
{
return declaredInterceptorsRegistry;
}
}
Expand Up @@ -444,7 +444,7 @@ protected void initInterceptors()
{
if (manager.getBoundInterceptorsRegistry().getInterceptionModel(getType()) == null)
{
InterceptionModelBuilder<Class<?>, Interceptor> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Interceptor.class);
InterceptionModelBuilder<Class<?>, Interceptor<?>> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Interceptor.class);

Set<Annotation> classBindingAnnotations = flattenInterceptorBindings(manager, getAnnotatedItem().getAnnotations());
for (Class<? extends Annotation> annotation : getStereotypes())
Expand Down
97 changes: 80 additions & 17 deletions impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java
Expand Up @@ -17,22 +17,28 @@
package org.jboss.webbeans.bean;

import java.util.Set;
import java.util.List;
import java.util.ArrayList;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.Interceptor;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.interceptor.Interceptors;
import javax.interceptor.ExcludeClassInterceptors;

import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
import org.jboss.interceptor.proxy.InterceptionHandler;
import org.jboss.interceptor.proxy.InterceptionHandlerFactory;
import org.jboss.interceptor.proxy.InterceptorProxyCreatorImpl;
import org.jboss.interceptor.proxy.InterceptionHandlerFactory;
import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.interceptor.model.InterceptionModelBuilder;
import org.jboss.interceptor.registry.InterceptorRegistry;
import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.DefinitionException;
import org.jboss.webbeans.DeploymentException;
import org.jboss.webbeans.bean.interceptor.InterceptorInterceptionHandlerFactory;
import org.jboss.webbeans.bean.interceptor.ClassInterceptionHandlerFactory;
import org.jboss.webbeans.bootstrap.BeanDeployerEnvironment;
import org.jboss.webbeans.injection.ConstructorInjectionPoint;
import org.jboss.webbeans.injection.InjectionContextImpl;
Expand Down Expand Up @@ -69,7 +75,6 @@ public class ManagedBean<T> extends AbstractClassBean<T>

private ManagedBean<?> specializedBean;


/**
* Creates a simple, annotation defined Web Bean
*
Expand Down Expand Up @@ -115,7 +120,7 @@ public T create(CreationalContext<T> creationalContext)
{
instance = applyDecorators(instance, creationalContext, originalInjectionPoint);
}
if (isInterceptionCandidate() && hasInterceptors())
if (isInterceptionCandidate() && (hasBoundInterceptors() || hasDeclaredInterceptors()))
{
instance = applyInterceptors(instance, creationalContext);
InterceptionUtils.executePostConstruct(instance);
Expand Down Expand Up @@ -176,7 +181,7 @@ public void destroy(T instance, CreationalContext<T> creationalContext)
{
try
{
if (!isInterceptionCandidate() || !hasInterceptors())
if (!isInterceptionCandidate() || !hasBoundInterceptors())
preDestroy(instance);
else
{
Expand Down Expand Up @@ -204,6 +209,8 @@ public void initialize(BeanDeployerEnvironment environment)
initPostConstruct();
initPreDestroy();
initEEInjectionPoints();
if (isInterceptionCandidate())
initDeclaredInterceptors();
}
}

Expand Down Expand Up @@ -378,24 +385,40 @@ protected boolean isInterceptionCandidate()
return !Beans.isInterceptor(getAnnotatedItem()) && !Beans.isDecorator(getAnnotatedItem());
}

private boolean hasInterceptors()
private boolean hasBoundInterceptors()
{
return manager.getBoundInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size() > 0;
if (manager.getBoundInterceptorsRegistry().getInterceptionModel(getType()) != null)
return manager.getBoundInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size() > 0;
else
return false;
}

private boolean hasDeclaredInterceptors()
{
if (manager.getDeclaredInterceptorsRegistry().getInterceptionModel(getType()) != null)
return manager.getDeclaredInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size() > 0;
else
return false;
}

protected T applyInterceptors(T instance, final CreationalContext<T> creationalContext)
{
try
{
InterceptionHandlerFactory<Interceptor> factory = new InterceptionHandlerFactory<Interceptor>()
List<InterceptorRegistry<Class<?>, ?>> interceptionRegistries = new ArrayList<InterceptorRegistry<Class<?>,?>>();
List<InterceptionHandlerFactory<?>> interceptionHandlerFactories = new ArrayList<InterceptionHandlerFactory<?>>();
if (hasDeclaredInterceptors())
{
public InterceptionHandler createFor(final Interceptor interceptor)
{
final Object instance = getManager().getReference(interceptor, creationalContext);
return new DirectClassInterceptionHandler<Interceptor>(instance, interceptor.getBeanClass());
}
};
instance = new InterceptorProxyCreatorImpl<Interceptor>(manager.getBoundInterceptorsRegistry(), factory).createProxyFromInstance(instance, getType());
interceptionRegistries.add(manager.getDeclaredInterceptorsRegistry());
interceptionHandlerFactories.add(new ClassInterceptionHandlerFactory(creationalContext, getManager()));
}
if (hasBoundInterceptors())
{
interceptionRegistries.add(manager.getBoundInterceptorsRegistry());
interceptionHandlerFactories.add(new InterceptorInterceptionHandlerFactory(creationalContext, manager));
}
if (interceptionRegistries.size() > 0)
instance = new InterceptorProxyCreatorImpl(interceptionRegistries, interceptionHandlerFactories).createProxyFromInstance(instance, getType());

} catch (Exception e)
{
Expand All @@ -404,4 +427,44 @@ public InterceptionHandler createFor(final Interceptor interceptor)
return instance;
}

protected void initDeclaredInterceptors()
{
if (manager.getDeclaredInterceptorsRegistry().getInterceptionModel(getType()) == null)
{
InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Class.class);

Class<?>[] classDeclaredInterceptors = null;
if (getAnnotatedItem().isAnnotationPresent(Interceptors.class))
{
classDeclaredInterceptors = getType().getAnnotation(Interceptors.class).value();
}

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

List<WBMethod<?, ?>> businessMethods = Beans.getInterceptableBusinessMethods(getAnnotatedItem());
for (WBMethod<?, ?> method : businessMethods)
{
boolean excludeClassInterceptors = method.isAnnotationPresent(ExcludeClassInterceptors.class);
Class<?>[] methodDeclaredInterceptors = null;
if (method.isAnnotationPresent(Interceptors.class))
{
methodDeclaredInterceptors = method.getAnnotation(Interceptors.class).value();
}
if (!excludeClassInterceptors && classDeclaredInterceptors != null)
{
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(classDeclaredInterceptors);
}
if (methodDeclaredInterceptors != null)
{
builder.interceptAroundInvoke(((AnnotatedMethod) method).getJavaMember()).with(methodDeclaredInterceptors);
}
}
manager.getDeclaredInterceptorsRegistry().registerInterceptionModel(getType(), builder.build());
}
}

}
11 changes: 10 additions & 1 deletion impl/src/main/java/org/jboss/webbeans/bean/SessionBean.java
Expand Up @@ -40,6 +40,7 @@
import org.jboss.webbeans.bean.proxy.EnterpriseBeanInstance;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanProxyMethodHandler;
import org.jboss.webbeans.bean.proxy.Marker;
import org.jboss.webbeans.bean.interceptor.InterceptorBindingsAdapter;
import org.jboss.webbeans.bootstrap.BeanDeployerEnvironment;
import org.jboss.webbeans.ejb.InternalEjbDescriptor;
import org.jboss.webbeans.ejb.api.SessionObjectReference;
Expand All @@ -53,6 +54,7 @@
import org.jboss.webbeans.resources.ClassTransformer;
import org.jboss.webbeans.util.Beans;
import org.jboss.webbeans.util.Proxies;
import org.jboss.interceptor.model.InterceptionModel;

/**
* An enterprise bean representation
Expand Down Expand Up @@ -115,6 +117,7 @@ public void initialize(BeanDeployerEnvironment environment)
checkConflictingRoles();
checkObserverMethods();
checkScopeAllowed();
registerInterceptors();
}
}

Expand Down Expand Up @@ -394,6 +397,12 @@ protected boolean isInterceptionCandidate()
{
return true;
}


private void registerInterceptors()
{
InterceptionModel<Class<?>,javax.enterprise.inject.spi.Interceptor<?>> model = manager.getBoundInterceptorsRegistry().getInterceptionModel(ejbDescriptor.getBeanClass());
if (model != null)
getManager().getServices().get(EjbServices.class).registerInterceptors(getEjbDescriptor(), new InterceptorBindingsAdapter(model));
}
}

@@ -0,0 +1,57 @@
/*
* 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.bean.interceptor;

import javax.enterprise.context.spi.CreationalContext;

import org.jboss.interceptor.proxy.InterceptionHandlerFactory;
import org.jboss.interceptor.proxy.InterceptionHandler;
import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
import org.jboss.webbeans.DeploymentException;
import org.jboss.webbeans.BeanManagerImpl;

/**
* @author Marius Bogoevici
*/
public class ClassInterceptionHandlerFactory implements InterceptionHandlerFactory<Class>
{
private final CreationalContext<?> creationalContext;
private BeanManagerImpl manager;

public ClassInterceptionHandlerFactory(CreationalContext<?> creationalContext, BeanManagerImpl manager)
{
this.creationalContext = creationalContext;
this.manager = manager;
}

public InterceptionHandler createFor(Class clazz)
{
try
{
// this is not a managed instance - assume no-argument constructor exists
Object interceptorInstance = clazz.newInstance();
// inject
manager.createInjectionTarget(manager.createAnnotatedType(clazz)).inject(interceptorInstance, creationalContext);
return new DirectClassInterceptionHandler(interceptorInstance, clazz);
}
catch (Exception e)
{
throw new DeploymentException(e);
}
}
}
@@ -0,0 +1,95 @@
/*
* 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.bean.interceptor;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import java.util.Arrays;
import java.lang.reflect.Method;

import javax.enterprise.inject.spi.Interceptor;
import javax.enterprise.inject.spi.InterceptionType;

import org.jboss.webbeans.ejb.spi.InterceptorBindings;
import org.jboss.interceptor.model.InterceptionModel;

/**
* @author Marius Bogoevici
*/
public class InterceptorBindingsAdapter implements InterceptorBindings
{

private InterceptionModel<Class<?>, Interceptor<?>> interceptionModel;

public InterceptorBindingsAdapter(InterceptionModel<Class<?>, Interceptor<?>> interceptionModel)
{
if (interceptionModel == null)
{
throw new IllegalArgumentException("Interception model must not be null");
}
this.interceptionModel = interceptionModel;
}

public Collection<Interceptor<?>> getAllInterceptors()
{
return interceptionModel.getAllInterceptors();
}

public List<Interceptor<?>> getMethodInterceptors(InterceptionType interceptionType, Method method)
{
if (interceptionType == null)
{
throw new IllegalArgumentException("InterceptionType must not be null");
}

if (method == null)
{
throw new IllegalArgumentException("Method must not be null");
}

org.jboss.interceptor.model.InterceptionType internalInterceptionType = org.jboss.interceptor.model.InterceptionType.valueOf(interceptionType.name());

if (internalInterceptionType.isLifecycleCallback())
{
throw new IllegalArgumentException("Interception type must not be lifecycle, but it is " + interceptionType.name());
}

return interceptionModel.getInterceptors(internalInterceptionType, method);

}

public List<Interceptor<?>> getLifecycleInterceptors(InterceptionType interceptionType)
{
if (interceptionType == null)
{
throw new IllegalArgumentException("InterceptionType must not be null");
}

org.jboss.interceptor.model.InterceptionType internalInterceptionType = org.jboss.interceptor.model.InterceptionType.valueOf(interceptionType.name());

if (!internalInterceptionType.isLifecycleCallback())
{
throw new IllegalArgumentException("Interception type must be lifecycle, but it is " + interceptionType.name());
}

return interceptionModel.getInterceptors(internalInterceptionType, null);
}
}

0 comments on commit fcb1dd7

Please sign in to comment.