/weld/src/main/java/org/jboss/as/weld/ejb/Jsr299BindingsInterceptor.java

https://github.com/dpospisil/jboss-as · Java · 120 lines · 80 code · 14 blank · 26 comment · 10 complexity · 2443b22fe51d7540ae230cae435370fe MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
  4. * contributors by the @authors tag. See the copyright.txt in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jboss.as.weld.ejb;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.enterprise.inject.spi.InterceptionType;
  21. import javax.enterprise.inject.spi.Interceptor;
  22. import javax.interceptor.InvocationContext;
  23. import org.jboss.as.ee.component.ComponentInstance;
  24. import org.jboss.as.ejb3.component.stateful.SerializedCdiInterceptorsKey;
  25. import org.jboss.invocation.ImmediateInterceptorFactory;
  26. import org.jboss.invocation.InterceptorContext;
  27. import org.jboss.invocation.InterceptorFactory;
  28. import org.jboss.msc.service.ServiceBuilder;
  29. import org.jboss.msc.service.ServiceName;
  30. import org.jboss.msc.value.InjectedValue;
  31. import org.jboss.weld.ejb.spi.InterceptorBindings;
  32. /**
  33. * Interceptor for applying the JSR-299 specific interceptor bindings.
  34. * <p/>
  35. * It is a separate interceptor, as it needs to be applied after all
  36. * the other existing interceptors.
  37. *
  38. * @author Marius Bogoevici
  39. * @author Stuart Douglas
  40. */
  41. public class Jsr299BindingsInterceptor implements org.jboss.invocation.Interceptor {
  42. private final InterceptionType interceptionType;
  43. private final InjectedValue<InterceptorBindings> interceptorBindings = new InjectedValue<InterceptorBindings>();
  44. private Jsr299BindingsInterceptor(InterceptionType interceptionType) {
  45. this.interceptionType = interceptionType;
  46. }
  47. public static InterceptorFactory factory(final InterceptionType interceptionType, final ServiceBuilder<?> builder, final ServiceName interceptorBindingServiceName) {
  48. Jsr299BindingsInterceptor interceptor = new Jsr299BindingsInterceptor(interceptionType);
  49. builder.addDependency(interceptorBindingServiceName, InterceptorBindings.class, interceptor.interceptorBindings);
  50. return new ImmediateInterceptorFactory(interceptor);
  51. }
  52. protected Object delegateInterception(InvocationContext invocationContext, InterceptionType interceptionType, List<Interceptor<?>> currentInterceptors, WeldInterceptorInstances interceptorInstances)
  53. throws Exception {
  54. List<Object> currentInterceptorInstances = new ArrayList<Object>();
  55. for (Interceptor<?> interceptor : currentInterceptors) {
  56. currentInterceptorInstances.add(interceptorInstances.getInterceptorInstances().get(interceptor.getBeanClass().getName()).getInstance());
  57. }
  58. if (currentInterceptorInstances.size() > 0) {
  59. return new DelegatingInterceptorInvocationContext(invocationContext, currentInterceptors, currentInterceptorInstances, interceptionType).proceed();
  60. } else {
  61. return invocationContext.proceed();
  62. }
  63. }
  64. private Object doMethodInterception(InvocationContext invocationContext, InterceptionType interceptionType, WeldInterceptorInstances interceptorInstances, InterceptorBindings interceptorBindings)
  65. throws Exception {
  66. if (interceptorBindings != null) {
  67. List<Interceptor<?>> currentInterceptors = interceptorBindings.getMethodInterceptors(interceptionType, invocationContext.getMethod());
  68. return delegateInterception(invocationContext, interceptionType, currentInterceptors, interceptorInstances);
  69. } else {
  70. return invocationContext.proceed();
  71. }
  72. }
  73. @Override
  74. public Object processInvocation(final InterceptorContext context) throws Exception {
  75. final ComponentInstance componentInstance = context.getPrivateData(ComponentInstance.class);
  76. final WeldInterceptorInstances interceptorInstances = (WeldInterceptorInstances) componentInstance.getInstanceData(SerializedCdiInterceptorsKey.class);
  77. final InterceptorBindings interceptorBindings = this.interceptorBindings.getValue();
  78. switch (interceptionType) {
  79. case AROUND_INVOKE:
  80. return doMethodInterception(context.getInvocationContext(), InterceptionType.AROUND_INVOKE, interceptorInstances, interceptorBindings);
  81. case AROUND_TIMEOUT:
  82. return doMethodInterception(context.getInvocationContext(), InterceptionType.AROUND_TIMEOUT, interceptorInstances, interceptorBindings);
  83. case PRE_DESTROY:
  84. try {
  85. return doLifecycleInterception(context, interceptorInstances, interceptorBindings);
  86. } finally {
  87. interceptorInstances.getCreationalContext().release();
  88. }
  89. case POST_CONSTRUCT:
  90. return doLifecycleInterception(context, interceptorInstances, interceptorBindings);
  91. case AROUND_CONSTRUCT:
  92. return doLifecycleInterception(context, interceptorInstances, interceptorBindings);
  93. default:
  94. //should never happen
  95. return context.proceed();
  96. }
  97. }
  98. private Object doLifecycleInterception(final InterceptorContext context, WeldInterceptorInstances interceptorInstances, final InterceptorBindings interceptorBindings) throws Exception {
  99. if (interceptorBindings == null) {
  100. return context.proceed();
  101. } else {
  102. List<Interceptor<?>> currentInterceptors = interceptorBindings.getLifecycleInterceptors(interceptionType);
  103. return delegateInterception(context.getInvocationContext(), interceptionType, currentInterceptors, interceptorInstances);
  104. }
  105. }
  106. }