}
interceptorWithLifecycleCallbacks.addAll(description.getClassInterceptors());
for (final InterceptorDescription interceptorDescription : description.getAllInterceptors()) {
final String interceptorClassName = interceptorDescription.getInterceptorClassName();
final ClassIndex interceptorClass;
try {
interceptorClass = classIndex.classIndex(interceptorClassName);
} catch (ClassNotFoundException e) {
throw MESSAGES.cannotLoadInterceptor(e, interceptorClassName);
}
final InterceptorEnvironment interceptorEnvironment = moduleDescription.getInterceptorEnvironment().get(interceptorClassName);
if (interceptorEnvironment != null) {
//if the interceptor has environment config we merge it into the components environment
description.getBindingConfigurations().addAll(interceptorEnvironment.getBindingConfigurations());
for (final ResourceInjectionConfiguration injection : interceptorEnvironment.getResourceInjections()) {
description.addResourceInjection(injection);
}
}
//we store the interceptor instance under the class key
final Object contextKey = interceptorClass.getModuleClass();
configuration.getInterceptorContextKeys().add(contextKey);
final ClassReflectionIndex<?> interceptorIndex = deploymentReflectionIndex.getClassIndex(interceptorClass.getModuleClass());
final Constructor<?> constructor = interceptorIndex.getConstructor(EMPTY_CLASS_ARRAY);
if (constructor == null) {
throw MESSAGES.defaultConstructorNotFoundOnComponent(interceptorClassName, configuration.getComponentClass());
}
instantiators.addFirst(new ManagedReferenceInterceptorFactory(new ValueManagedReferenceFactory(new ConstructedValue(constructor, Collections.<Value<?>>emptyList())), contextKey));
destructors.addLast(new ManagedReferenceReleaseInterceptorFactory(contextKey));
final boolean interceptorHasLifecycleCallbacks = interceptorWithLifecycleCallbacks.contains(interceptorDescription);
new ClassDescriptionTraversal(interceptorClass.getModuleClass(), applicationClasses) {
@Override
public void handle(final Class<?> clazz, EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
mergeInjectionsForClass(clazz, classDescription, moduleDescription, description, configuration, context, injectors, contextKey, uninjectors, metadataComplete);
final InterceptorClassDescription interceptorConfig;
if (classDescription != null && !metadataComplete) {
interceptorConfig = InterceptorClassDescription.merge(classDescription.getInterceptorClassDescription(), moduleDescription.getInterceptorClassOverride(clazz.getName()));
} else {
interceptorConfig = InterceptorClassDescription.merge(null, moduleDescription.getInterceptorClassOverride(clazz.getName()));
}
// Only class level interceptors are processed for postconstruct/predestroy methods.
// Method level interceptors aren't supposed to be processed for postconstruct/predestroy lifecycle
// methods, as per interceptors spec
if (interceptorHasLifecycleCallbacks) {
final MethodIdentifier postConstructMethodIdentifier = interceptorConfig.getPostConstruct();
handleInterceptorClass(clazz, postConstructMethodIdentifier, userPostConstructByInterceptorClass, true, true);
final MethodIdentifier preDestroyMethodIdentifier = interceptorConfig.getPreDestroy();
handleInterceptorClass(clazz, preDestroyMethodIdentifier, userPreDestroyByInterceptorClass, true, true);
}
final MethodIdentifier aroundInvokeMethodIdentifier = interceptorConfig.getAroundInvoke();
handleInterceptorClass(clazz, aroundInvokeMethodIdentifier, userAroundInvokesByInterceptorClass, false, false);
if (description.isTimerServiceApplicable()) {
final MethodIdentifier aroundTimeoutMethodIdentifier = interceptorConfig.getAroundTimeout();
handleInterceptorClass(clazz, aroundTimeoutMethodIdentifier, userAroundTimeoutsByInterceptorClass, false, false);
}
if (description.isPassivationApplicable()) {
handleInterceptorClass(clazz, interceptorConfig.getPrePassivate(), userPrePassivatesByInterceptorClass, false, false);
handleInterceptorClass(clazz, interceptorConfig.getPostActivate(), userPostActivatesByInterceptorClass, false, false);
}
}
private void handleInterceptorClass(final Class<?> clazz, final MethodIdentifier methodIdentifier, final Map<String, List<InterceptorFactory>> classMap, final boolean changeMethod, final boolean lifecycleMethod) throws DeploymentUnitProcessingException {
if (methodIdentifier != null) {
final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, methodIdentifier);
if (isNotOverriden(clazz, method, interceptorClass.getModuleClass(), deploymentReflectionIndex)) {
final InterceptorFactory interceptorFactory = new ManagedReferenceLifecycleMethodInterceptorFactory(contextKey, method, changeMethod, lifecycleMethod);
List<InterceptorFactory> factories = classMap.get(interceptorClassName);
if (factories == null) {
classMap.put(interceptorClassName, factories = new ArrayList<InterceptorFactory>());
}
factories.add(interceptorFactory);
}
}
}
}.run();
}
final List<InterceptorFactory> userPostConstruct = new ArrayList<InterceptorFactory>();
final List<InterceptorFactory> userPreDestroy = new ArrayList<InterceptorFactory>();
final List<InterceptorFactory> userPrePassivate = new ArrayList<InterceptorFactory>();
final List<InterceptorFactory> userPostActivate = new ArrayList<InterceptorFactory>();
//now add the lifecycle interceptors in the correct order
for (final InterceptorDescription interceptorClass : interceptorWithLifecycleCallbacks) {
if (userPostConstructByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPostConstruct.addAll(userPostConstructByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
if (userPreDestroyByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPreDestroy.addAll(userPreDestroyByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
if (description.isPassivationApplicable()) {
if (userPrePassivatesByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPrePassivate.addAll(userPrePassivatesByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
if (userPostActivatesByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPostActivate.addAll(userPostActivatesByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
}
}