1 22 package org.jboss.ejb; 23 24 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedHashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Stack ; 33 34 import org.jboss.logging.Logger; 35 36 47 public final class AllowedOperationsAssociation implements AllowedOperationsFlags 48 { 49 private static final Logger log = Logger.getLogger(AllowedOperationsAssociation.class); 51 52 54 public static final HashMap methodMap = new LinkedHashMap (); 55 static 56 { 57 methodMap.put(new Integer (IN_INTERCEPTOR_METHOD), "IN_INTERCEPTOR_METHOD"); 58 methodMap.put(new Integer (IN_EJB_ACTIVATE), "IN_EJB_ACTIVATE"); 59 methodMap.put(new Integer (IN_EJB_PASSIVATE), "IN_EJB_PASSIVATE"); 60 methodMap.put(new Integer (IN_EJB_REMOVE), "IN_EJB_REMOVE"); 61 methodMap.put(new Integer (IN_EJB_CREATE), "IN_EJB_CREATE"); 62 methodMap.put(new Integer (IN_EJB_POST_CREATE), "IN_EJB_POST_CREATE"); 63 methodMap.put(new Integer (IN_EJB_FIND), "IN_EJB_FIND"); 64 methodMap.put(new Integer (IN_EJB_HOME), "IN_EJB_HOME"); 65 methodMap.put(new Integer (IN_EJB_TIMEOUT), "IN_EJB_TIMEOUT"); 66 methodMap.put(new Integer (IN_EJB_LOAD), "IN_EJB_LOAD"); 67 methodMap.put(new Integer (IN_EJB_STORE), "IN_EJB_STORE"); 68 methodMap.put(new Integer (IN_SET_ENTITY_CONTEXT), "IN_SET_ENTITY_CONTEXT"); 69 methodMap.put(new Integer (IN_UNSET_ENTITY_CONTEXT), "IN_UNSET_ENTITY_CONTEXT"); 70 methodMap.put(new Integer (IN_SET_SESSION_CONTEXT), "IN_SET_SESSION_CONTEXT"); 71 methodMap.put(new Integer (IN_SET_MESSAGE_DRIVEN_CONTEXT), "IN_SET_MESSAGE_DRIVEN_CONTEXT"); 72 methodMap.put(new Integer (IN_AFTER_BEGIN), "IN_AFTER_BEGIN"); 73 methodMap.put(new Integer (IN_BEFORE_COMPLETION), "IN_BEFORE_COMPLETION"); 74 methodMap.put(new Integer (IN_AFTER_COMPLETION), "IN_AFTER_COMPLETION"); 75 methodMap.put(new Integer (IN_BUSINESS_METHOD), "IN_BUSINESS_METHOD"); 76 methodMap.put(new Integer (IN_SERVICE_ENDPOINT_METHOD), "IN_SERVICE_ENDPOINT_METHOD"); 77 } 78 79 82 private static ThreadLocal threadLocal = new ThreadLocal () { 83 protected Object initialValue() 84 { 85 return new Stack (); 86 } 87 }; 88 89 91 96 public static void pushInMethodFlag(int inMethodFlag) 97 { 98 Stack inMethodStack = (Stack )threadLocal.get(); 99 inMethodStack.push(new Integer (inMethodFlag)); 100 } 101 102 105 public static void popInMethodFlag() 106 { 107 Stack inMethodStack = (Stack )threadLocal.get(); 108 inMethodStack.pop(); 109 } 110 111 114 public static int peekInMethodFlag() 115 { 116 Stack inMethodStack = (Stack )threadLocal.get(); 117 if (inMethodStack.isEmpty() == false) 118 return ((Integer )inMethodStack.peek()).intValue(); 119 else 120 return -1; 121 } 122 123 127 public static String peekInMethodFlagAsString() 128 { 129 int currentMethodFlag = peekInMethodFlag(); 130 131 return (String )methodMap.get(new Integer (currentMethodFlag)); 132 } 133 134 138 public static void assertAllowedIn(String ctxMethod, int flags) 139 { 140 Stack inMethodStack = (Stack )threadLocal.get(); 141 142 if (inMethodStack.empty()) 144 { 145 throw new IllegalStateException ("Cannot obtain inMethodFlag for: " + ctxMethod); 146 } 147 148 if (inMethodStack.empty() == false) 151 { 152 Integer inMethodFlag = ((Integer ) inMethodStack.peek()); 155 if ((inMethodFlag.intValue() & flags) == 0 && inMethodFlag.intValue() != IN_INTERCEPTOR_METHOD) 156 { 157 String message = ctxMethod + " should not be access from this bean method: " + methodMap.get(inMethodFlag); 158 IllegalStateException ex = new IllegalStateException (message); 159 log.error(message + ", allowed is " + getAllowedMethodList(flags), ex); 160 throw ex; 161 } 162 } 163 } 164 165 168 private static List getAllowedMethodList(int flags) 169 { 170 ArrayList allowed = new ArrayList (); 171 Iterator it = methodMap.entrySet().iterator(); 172 while (it.hasNext()) 173 { 174 Map.Entry entry = (Map.Entry ) it.next(); 175 Integer flag = (Integer ) entry.getKey(); 176 if ((flag.intValue() & flags) > 0) 177 allowed.add(entry.getValue()); 178 } 179 return allowed; 180 } 181 } 182 | Popular Tags |