use of javax.enterprise.inject.spi.Interceptor in project wildfly by wildfly.
the class Jsr299BindingsCreateInterceptor method processInvocation.
@Override
public Object processInvocation(InterceptorContext interceptorContext) throws Exception {
BeanManagerImpl beanManager = this.beanManager;
if (beanManager == null) {
//cache the BM lookup, as it is quite slow
beanManager = this.beanManager = this.weldContainer.getValue().getBeanManager(beanArchiveId);
}
//this is not always called with the deployments TCCL set
//which causes weld to blow up
SessionBean<Object> bean = null;
if (ejbName != null) {
EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(this.ejbName);
if (descriptor != null) {
bean = beanManager.getBean(descriptor);
}
}
InterceptorBindings interceptorBindings = this.interceptorBindings.getValue();
final ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class);
InterceptorInstances existing = interceptorSupport.getInterceptorInstances(componentInstance);
if (existing == null) {
CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean);
HashMap<String, SerializableContextualInstance<Interceptor<Object>, Object>> interceptorInstances = new HashMap<String, SerializableContextualInstance<Interceptor<Object>, Object>>();
if (interceptorBindings != null) {
for (Interceptor<?> interceptor : interceptorBindings.getAllInterceptors()) {
addInterceptorInstance((Interceptor<Object>) interceptor, beanManager, interceptorInstances, creationalContext);
}
}
interceptorSupport.setInterceptorInstances(componentInstance, new WeldInterceptorInstances(creationalContext, interceptorInstances));
}
return interceptorContext.proceed();
}
use of javax.enterprise.inject.spi.Interceptor in project tomee by apache.
the class BeanContext method mergeOWBAndOpenEJBInfo.
public void mergeOWBAndOpenEJBInfo() {
final CdiEjbBean cdiEjbBean = get(CdiEjbBean.class);
if (cdiEjbBean == null) {
return;
}
final InjectionTargetImpl<?> injectionTarget = InjectionTargetImpl.class.cast(get(CdiEjbBean.class).getInjectionTarget());
final InterceptorResolutionService.BeanInterceptorInfo info = injectionTarget.getInterceptorInfo();
if (info == null) {
return;
}
final Collection<Interceptor<?>> postConstructInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "postConstructInterceptors"));
final Collection<Interceptor<?>> preDestroyInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "preDestroyInterceptors"));
if (postConstructInterceptors != null) {
for (final Interceptor<?> pc : postConstructInterceptors) {
if (isEjbInterceptor(pc)) {
continue;
}
final InterceptorData interceptorData = createInterceptorData(pc);
instanceScopedInterceptors.add(interceptorData);
cdiInterceptors.add(interceptorData);
}
}
if (preDestroyInterceptors != null) {
for (final Interceptor<?> pd : preDestroyInterceptors) {
if (isEjbInterceptor(pd)) {
continue;
}
if (postConstructInterceptors.contains(pd)) {
continue;
}
final InterceptorData interceptorData = createInterceptorData(pd);
instanceScopedInterceptors.add(interceptorData);
cdiInterceptors.add(interceptorData);
}
}
for (final Map.Entry<Method, InterceptorResolutionService.BusinessMethodInterceptorInfo> entry : info.getBusinessMethodsInfo().entrySet()) {
final Interceptor<?>[] interceptors = entry.getValue().getCdiInterceptors();
if (interceptors == null) {
continue;
}
for (final Interceptor<?> i : interceptors) {
// already at class level, since we merge "hooks" in InterceptorData no need to add it again
if (postConstructInterceptors.contains(i) || preDestroyInterceptors.contains(i)) {
continue;
}
final InterceptorData data = createInterceptorData(i);
addCdiMethodInterceptor(entry.getKey(), data);
}
entry.getValue().setEjbInterceptors(new ArrayList<Interceptor<?>>());
entry.getValue().setCdiInterceptors(new ArrayList<Interceptor<?>>());
}
// handled by OpenEJB now so clean up all duplication from OWB
if (info.getSelfInterceptorBean() != null) {
try {
final Field field = InterceptorResolutionService.BeanInterceptorInfo.class.getDeclaredField("selfInterceptorBean");
field.setAccessible(true);
field.set(info, null);
} catch (final Exception e) {
// no-op
}
}
Map.class.cast(Reflections.get(injectionTarget, "methodInterceptors")).clear();
clear(Collection.class.cast(postConstructInterceptors));
clear(Collection.class.cast(preDestroyInterceptors));
clear(Collection.class.cast(Reflections.get(injectionTarget, "postConstructMethods")));
clear(Collection.class.cast(Reflections.get(injectionTarget, "preDestroyMethods")));
clear(Collection.class.cast(Reflections.get(info, "ejbInterceptors")));
clear(Collection.class.cast(Reflections.get(info, "cdiInterceptors")));
// OWB doesn't compute AROUND_INVOKE so let's do it
final Method timeout = getEjbTimeout();
if (timeout != null) {
final AnnotatedType annotatedType = cdiEjbBean.getAnnotatedType();
final AnnotationManager annotationManager = getWebBeansContext().getAnnotationManager();
final Collection<Annotation> annotations = new HashSet<>();
annotations.addAll(annotationManager.getInterceptorAnnotations(annotatedType.getAnnotations()));
final Set<AnnotatedMethod<?>> methods = annotatedType.getMethods();
for (final AnnotatedMethod<?> m : methods) {
if (timeout.equals(m.getJavaMember())) {
annotations.addAll(annotationManager.getInterceptorAnnotations(m.getAnnotations()));
break;
}
}
if (!annotations.isEmpty()) {
for (final Interceptor<?> timeoutInterceptor : getWebBeansContext().getBeanManagerImpl().resolveInterceptors(InterceptionType.AROUND_TIMEOUT, AnnotationUtil.asArray(annotations))) {
if (isEjbInterceptor(timeoutInterceptor)) {
continue;
}
final InterceptorData data = createInterceptorData(timeoutInterceptor);
addCdiMethodInterceptor(timeout, data);
}
}
}
}
Aggregations