/ejb3/src/main/java/org/jboss/as/ejb3/component/stateless/StatelessSessionComponentInstance.java

https://github.com/luksa/wildfly · Java · 97 lines · 57 code · 10 blank · 30 comment · 7 complexity · 7c2fc832c9388e1022bbc57718cace28 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.ejb3.component.stateless;
  23. import org.jboss.as.ee.component.BasicComponent;
  24. import org.jboss.as.ejb3.component.TimedComponentInstance;
  25. import org.jboss.as.ejb3.component.session.SessionBeanComponentInstance;
  26. import org.jboss.as.naming.ManagedReference;
  27. import org.jboss.invocation.Interceptor;
  28. import org.jboss.invocation.InterceptorContext;
  29. import javax.ejb.Timer;
  30. import java.io.Serializable;
  31. import java.lang.reflect.Method;
  32. import java.util.Map;
  33. import java.util.concurrent.atomic.AtomicReference;
  34. /**
  35. * Author : Jaikiran Pai
  36. */
  37. public class StatelessSessionComponentInstance extends SessionBeanComponentInstance implements TimedComponentInstance {
  38. private static final Object[] EMPTY_OBJECT_ARRAY = {};
  39. private final Map<Method, Interceptor> timeoutInterceptors;
  40. /**
  41. * Construct a new instance.
  42. *
  43. * @param component the component
  44. * @param timeoutInterceptors
  45. */
  46. protected StatelessSessionComponentInstance(final BasicComponent component, final AtomicReference<ManagedReference> instanceReference, final Interceptor preDestroyInterceptor, final Map<Method, Interceptor> methodInterceptors, final Map<Method, Interceptor> timeoutInterceptors) {
  47. super(component, instanceReference, preDestroyInterceptor, methodInterceptors);
  48. this.timeoutInterceptors = timeoutInterceptors;
  49. }
  50. @Override
  51. protected Serializable getId() {
  52. return null;
  53. }
  54. @Override
  55. public void invokeTimeoutMethod(final Method method, final Timer timer) {
  56. final Interceptor interceptor = timeoutInterceptors.get(method);
  57. if (interceptor == null) {
  58. throw new RuntimeException("Unknown timeout method " + method);
  59. }
  60. try {
  61. InterceptorContext context = prepareInterceptorContext();
  62. context.setMethod(method);
  63. context.setTimer(timer);
  64. context.setTarget(getInstance());
  65. final Object[] params;
  66. if (method.getParameterTypes().length == 1) {
  67. params = new Object[1];
  68. params[0] = timer;
  69. } else {
  70. params = EMPTY_OBJECT_ARRAY;
  71. }
  72. context.setParameters(params);
  73. interceptor.processInvocation(context);
  74. } catch (Exception e) {
  75. throw new RuntimeException(e);
  76. }
  77. }
  78. @Override
  79. public void invokeTimeoutMethod(final Timer timer) {
  80. final StatelessSessionComponent component = (StatelessSessionComponent) getComponent();
  81. final Method method = component.getTimeoutMethod();
  82. if (method == null) {
  83. throw new IllegalArgumentException("Component " + component.getComponentName() + " does not have a timeout method");
  84. }
  85. invokeTimeoutMethod(method, timer);
  86. }
  87. }