PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/component/allowedmethods/AllowedMethodsInformation.java

#
Java | 175 lines | 107 code | 39 blank | 29 comment | 21 complexity | 059f4ca5f047c57d1db52543d4c73689 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. package org.jboss.as.ejb3.component.allowedmethods;
  2. import org.jboss.as.ee.component.Component;
  3. import org.jboss.as.ee.component.interceptors.InvocationType;
  4. import org.jboss.as.ejb3.EjbMessages;
  5. import org.jboss.as.ejb3.component.EJBComponent;
  6. import org.jboss.as.ejb3.component.stateful.CurrentSynchronizationCallback;
  7. import org.jboss.as.ejb3.context.CurrentInvocationContext;
  8. import org.jboss.invocation.InterceptorContext;
  9. import java.util.Collections;
  10. import java.util.HashSet;
  11. import java.util.Set;
  12. /**
  13. * This class and its subclasses can be used to determine if a given method
  14. * is allowed to be invoked.
  15. *
  16. * @see CurrentInvocationContext
  17. * @see CurrentSynchronizationCallback
  18. *
  19. * @author Stuart Douglas
  20. */
  21. public class AllowedMethodsInformation {
  22. public static final AllowedMethodsInformation INSTANCE = new AllowedMethodsInformation();
  23. private final Set<DeniedMethodKey> denied;
  24. private final Set<DeniedSyncMethodKey> deniedSyncMethods;
  25. protected AllowedMethodsInformation() {
  26. final Set<DeniedMethodKey> denied = new HashSet<DeniedMethodKey>();
  27. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.TIMER_SERVICE_METHOD);
  28. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.TIMER_SERVICE_METHOD);
  29. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.GET_PRIMARY_KEY);
  30. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.GET_TIMER_SERVICE);
  31. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.IS_CALLER_IN_ROLE);
  32. add(denied, InvocationType.SET_ENTITY_CONTEXT, MethodType.GET_CALLER_PRINCIPLE);
  33. add(denied, InvocationType.HOME_METHOD, MethodType.TIMER_SERVICE_METHOD);
  34. add(denied, InvocationType.HOME_METHOD, MethodType.GET_PRIMARY_KEY);
  35. add(denied, InvocationType.ENTITY_EJB_CREATE, MethodType.TIMER_SERVICE_METHOD);
  36. add(denied, InvocationType.ENTITY_EJB_CREATE, MethodType.GET_PRIMARY_KEY);
  37. setup(denied);
  38. this.denied = Collections.unmodifiableSet(denied);
  39. final Set<DeniedSyncMethodKey> deniedSync = new HashSet<DeniedSyncMethodKey>();
  40. add(deniedSync, CurrentSynchronizationCallback.CallbackType.AFTER_COMPLETION, MethodType.TIMER_SERVICE_METHOD);
  41. add(deniedSync, CurrentSynchronizationCallback.CallbackType.AFTER_COMPLETION, MethodType.GET_ROLLBACK_ONLY);
  42. add(deniedSync, CurrentSynchronizationCallback.CallbackType.AFTER_COMPLETION, MethodType.SET_ROLLBACK_ONLY);
  43. this.deniedSyncMethods = Collections.unmodifiableSet(deniedSync);
  44. }
  45. protected void setup(Set<DeniedMethodKey> denied) {
  46. }
  47. protected static void add(Set<DeniedMethodKey> otherDenied, InvocationType setEntityContext, MethodType timerServiceMethod) {
  48. otherDenied.add(new DeniedMethodKey(setEntityContext, timerServiceMethod));
  49. }
  50. protected static void add(Set<DeniedSyncMethodKey> otherDenied, CurrentSynchronizationCallback.CallbackType callbackType, MethodType timerServiceMethod) {
  51. otherDenied.add(new DeniedSyncMethodKey(callbackType, timerServiceMethod));
  52. }
  53. /**
  54. * Checks that the current method
  55. */
  56. public static void checkAllowed(final MethodType methodType) {
  57. final InterceptorContext context = CurrentInvocationContext.get();
  58. if (context == null) {
  59. return;
  60. }
  61. final Component component = context.getPrivateData(Component.class);
  62. if (!(component instanceof EJBComponent)) {
  63. return;
  64. }
  65. final InvocationType invocationType = context.getPrivateData(InvocationType.class);
  66. ((EJBComponent) component).getAllowedMethodsInformation().realCheckPermission(methodType, invocationType);
  67. }
  68. /**
  69. * transaction sync is not affected by the current invocation, as multiple ejb methods may be invoked from afterCompletion
  70. */
  71. private void checkTransactionSync(MethodType methodType) {
  72. //first we have to check the synchronization status
  73. //as the sync is not affected by the current invocation
  74. final CurrentSynchronizationCallback.CallbackType currentSync = CurrentSynchronizationCallback.get();
  75. if (currentSync != null) {
  76. if (deniedSyncMethods.contains(new DeniedSyncMethodKey(currentSync, methodType))) {
  77. throwException(methodType, currentSync);
  78. }
  79. }
  80. }
  81. protected void realCheckPermission(MethodType methodType, InvocationType invocationType) {
  82. checkTransactionSync(methodType);
  83. if (invocationType != null) {
  84. if (denied.contains(new DeniedMethodKey(invocationType, methodType))) {
  85. throwException(methodType, invocationType);
  86. }
  87. }
  88. }
  89. /**
  90. * throw an exception when a method cannot be invoked
  91. *
  92. * @param methodType the method
  93. * @param invocationType the type of invocation that caused it to be disabled
  94. */
  95. protected void throwException(MethodType methodType, InvocationType invocationType) {
  96. throw EjbMessages.MESSAGES.cannotCallMethod(methodType.getLabel(), invocationType.getLabel());
  97. }
  98. /**
  99. * throw an exception when a method cannot be invoked
  100. *
  101. * @param methodType the method
  102. * @param callback the type of invocation that caused it to be disabled
  103. */
  104. protected void throwException(MethodType methodType, CurrentSynchronizationCallback.CallbackType callback) {
  105. throw EjbMessages.MESSAGES.cannotCallMethod(methodType.getLabel(), callback.name());
  106. }
  107. private static class DeniedSyncMethodKey {
  108. private final CurrentSynchronizationCallback.CallbackType callbackType;
  109. private final MethodType methodType;
  110. public DeniedSyncMethodKey(CurrentSynchronizationCallback.CallbackType callbackType, MethodType methodType) {
  111. this.callbackType = callbackType;
  112. this.methodType = methodType;
  113. }
  114. public CurrentSynchronizationCallback.CallbackType getCallbackType() {
  115. return callbackType;
  116. }
  117. public MethodType getMethodType() {
  118. return methodType;
  119. }
  120. @Override
  121. public boolean equals(Object o) {
  122. if (this == o) return true;
  123. if (o == null || getClass() != o.getClass()) return false;
  124. DeniedSyncMethodKey that = (DeniedSyncMethodKey) o;
  125. if (callbackType != that.callbackType) return false;
  126. if (methodType != that.methodType) return false;
  127. return true;
  128. }
  129. @Override
  130. public int hashCode() {
  131. int result = callbackType != null ? callbackType.hashCode() : 0;
  132. result = 31 * result + (methodType != null ? methodType.hashCode() : 0);
  133. return result;
  134. }
  135. }
  136. }