1 2 24 package com.sun.enterprise.util; 25 26 import java.util.*; 27 import java.lang.reflect.*; 28 import com.sun.enterprise.deployment.*; 29 30 37 public class BeanMethodCalculator { 38 39 40 public static Vector getPossibleCmpCmrFields(ClassLoader cl, 41 String className) 42 throws Exception { 43 44 Vector fieldDescriptors = new Vector(); 45 Class theClass = cl.loadClass(className); 46 47 Method[] methods = theClass.getMethods(); 49 50 for(int mIndex = 0; mIndex < methods.length; mIndex++) { 55 Method next = methods[mIndex]; 56 String nextName = next.getName(); 57 int nextModifiers = next.getModifiers(); 58 if( Modifier.isAbstract(nextModifiers) ) { 59 if( nextName.startsWith("get") && 60 nextName.length() > 3 ) { 61 String field = 62 nextName.substring(3,4).toLowerCase() + 63 nextName.substring(4); 64 fieldDescriptors.add(new FieldDescriptor(field)); 65 } 66 } 67 } 68 return fieldDescriptors; 69 } 70 71 public static Vector getMethodsFor(EjbDescriptor ejbDescriptor, ClassLoader classLoader) 72 throws ClassNotFoundException 73 { 74 Vector methods = new Vector(); 75 76 if (ejbDescriptor.isRemoteInterfacesSupported()) { 77 addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getHomeClassName())); 78 addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getRemoteClassName())); 79 } 80 81 if (ejbDescriptor.isRemoteBusinessInterfacesSupported()) { 82 for(String intf : ejbDescriptor.getRemoteBusinessClassNames()) { 83 addAllInterfaceMethodsIn(methods, classLoader.loadClass(intf)); 84 } 85 } 86 87 if (ejbDescriptor.isLocalInterfacesSupported()) { 88 addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getLocalHomeClassName())); 89 addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getLocalClassName())); 90 } 91 92 if (ejbDescriptor.isLocalBusinessInterfacesSupported()) { 93 for(String intf : ejbDescriptor.getLocalBusinessClassNames()) { 94 addAllInterfaceMethodsIn(methods, classLoader.loadClass(intf)); 95 } 96 } 97 98 if (ejbDescriptor.hasWebServiceEndpointInterface()) { 99 addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getWebServiceEndpointInterfaceName())); 100 101 } 102 return methods; 103 } 104 105 private static void addAllInterfaceMethodsIn(Collection methods, Class c) { 106 107 methods.addAll(Arrays.asList(c.getMethods())); 108 } 109 110 114 public static Collection getTransactionalMethodsFor(EjbDescriptor ejbDescriptor, ClassLoader loader) 115 throws ClassNotFoundException , NoSuchMethodException 116 { 117 boolean statefulSessionBean = false; 120 121 Vector methods = new Vector(); 122 if (ejbDescriptor instanceof EjbSessionDescriptor) { 123 statefulSessionBean = 124 ((EjbSessionDescriptor) ejbDescriptor).isStateful(); 125 126 if (ejbDescriptor.isRemoteInterfacesSupported()) { 128 Collection disallowedMethods = extractDisallowedMethodsFor(javax.ejb.EJBObject .class, sessionBeanMethodsDisallowed); 129 Collection potentials = getTransactionMethodsFor(loader, ejbDescriptor.getRemoteClassName() , disallowedMethods); 130 transformAndAdd(potentials, MethodDescriptor.EJB_REMOTE, methods); 131 } 132 133 if( ejbDescriptor.isRemoteBusinessInterfacesSupported() ) { 134 135 for(String intfName : 136 ejbDescriptor.getRemoteBusinessClassNames() ) { 137 138 Class businessIntf = loader.loadClass(intfName); 139 Method[] busIntfMethods = businessIntf.getMethods(); 140 for (Method next : busIntfMethods ) { 141 methods.add(new MethodDescriptor 142 (next, MethodDescriptor.EJB_REMOTE)); 143 } 144 } 145 } 146 147 if (ejbDescriptor.isLocalInterfacesSupported()) { 148 Collection disallowedMethods = extractDisallowedMethodsFor(javax.ejb.EJBLocalObject .class, sessionLocalBeanMethodsDisallowed); 149 Collection potentials = getTransactionMethodsFor(loader, ejbDescriptor.getLocalClassName() , disallowedMethods); 150 transformAndAdd(potentials, MethodDescriptor.EJB_LOCAL, methods); 151 152 } 153 154 if( ejbDescriptor.isLocalBusinessInterfacesSupported() ) { 155 156 for(String intfName : 157 ejbDescriptor.getLocalBusinessClassNames() ) { 158 159 Class businessIntf = loader.loadClass(intfName); 160 Method[] busIntfMethods = businessIntf.getMethods(); 161 for (Method next : busIntfMethods ) { 162 methods.add(new MethodDescriptor 163 (next, MethodDescriptor.EJB_LOCAL)); 164 } 165 } 166 } 167 168 if (ejbDescriptor.hasWebServiceEndpointInterface()) { 169 Class webServiceClass = loader.loadClass 170 (ejbDescriptor.getWebServiceEndpointInterfaceName()); 171 172 Method[] webMethods = webServiceClass.getMethods(); 173 for (int i=0;i<webMethods.length;i++) { 174 methods.add(new MethodDescriptor(webMethods[i], 175 MethodDescriptor.EJB_WEB_SERVICE)); 176 177 } 178 } 179 180 } else { 181 String homeIntf = ejbDescriptor.getHomeClassName(); 183 if (homeIntf!=null) { 184 185 Class home = loader.loadClass(homeIntf); 186 Collection potentials = getTransactionMethodsFor(javax.ejb.EJBHome .class, home); 187 transformAndAdd(potentials, MethodDescriptor.EJB_HOME, methods); 188 189 String remoteIntf = ejbDescriptor.getRemoteClassName(); 190 Class remote = loader.loadClass(remoteIntf); 191 potentials = getTransactionMethodsFor(javax.ejb.EJBObject .class, remote); 192 transformAndAdd(potentials, MethodDescriptor.EJB_REMOTE, methods); 193 } 194 195 String localHomeIntf = ejbDescriptor.getLocalHomeClassName(); 197 if (localHomeIntf!=null) { 198 Class home = loader.loadClass(localHomeIntf); 199 Collection potentials = getTransactionMethodsFor(javax.ejb.EJBLocalHome .class, home); 200 transformAndAdd(potentials, MethodDescriptor.EJB_LOCALHOME, methods); 201 202 String remoteIntf = ejbDescriptor.getLocalClassName(); 203 Class remote = loader.loadClass(remoteIntf); 204 potentials = getTransactionMethodsFor(javax.ejb.EJBLocalObject .class, remote); 205 transformAndAdd(potentials, MethodDescriptor.EJB_LOCAL, methods); 206 } 207 } 208 209 if( !statefulSessionBean ) { 210 Class ejbClass = loader.loadClass(ejbDescriptor.getEjbClassName()); 211 if( ejbDescriptor.isTimedObject() ) { 212 methods.add(ejbDescriptor.getEjbTimeoutMethod()); 213 } 214 } 215 216 return methods; 217 } 218 219 private static Collection getTransactionMethodsFor(ClassLoader loader, String interfaceName, Collection disallowedMethods) 220 throws ClassNotFoundException 221 { 222 Class clazz = loader.loadClass(interfaceName); 223 return getTransactionMethodsFor(clazz, disallowedMethods); 224 } 225 226 private static Collection getTransactionMethodsFor(Class interfaceImpl, Collection disallowedMethods) { 227 Vector v = new Vector(Arrays.asList(interfaceImpl.getMethods())); 228 v.removeAll(disallowedMethods); 229 return v; 230 } 231 232 private static Collection getTransactionMethodsFor(Class interfaceType, Class interfaceImpl) { 233 Collection disallowedTransactionMethods = getDisallowedTransactionMethodsFor(interfaceType); 234 return getTransactionMethodsFor(interfaceImpl, disallowedTransactionMethods); 235 } 236 237 private static Collection getDisallowedTransactionMethodsFor(Class interfaceType) { 238 return extractDisallowedMethodsFor(interfaceType, getDisallowedMethodsNamesFor(interfaceType)); 239 } 240 241 private static Collection extractDisallowedMethodsFor(Class interfaceType, String [] methodNames) { 243 244 Vector v = new Vector(); 245 if (methodNames.length==0) 247 return v; 248 249 Method methods[] = interfaceType.getMethods(); 250 251 for (int i=0; i<methods.length; i++) { 252 if (methodNames[0].equals("*")) 254 v.addElement(methods[i]); 255 else if (Arrays.binarySearch(methodNames, methods[i].getName())>=0) 256 v.addElement(methods[i]); 257 } 258 return v; 259 } 260 261 269 private static void transformAndAdd(Collection methods, String methodIntf, Vector globalList) { 270 271 for (Iterator itr = methods.iterator();itr.hasNext();) { 272 Method m = (Method) itr.next(); 273 MethodDescriptor md = new MethodDescriptor(m, methodIntf); 274 globalList.add(md); 275 } 276 } 277 278 281 private static String [] getDisallowedMethodsNamesFor(Class interfaceType) { 282 return (String []) getDisallowedMethodsNames().get(interfaceType); 283 } 284 285 290 protected static Map getDisallowedMethodsNames() { 291 if (disallowedMethodsPerInterface==null) { 292 disallowedMethodsPerInterface = new Hashtable(); 293 disallowedMethodsPerInterface.put(javax.ejb.EJBHome .class, entityBeanHomeMethodsDisallowed); 294 disallowedMethodsPerInterface.put(javax.ejb.EJBObject .class, entityBeanRemoteMethodsDisallowed); 295 disallowedMethodsPerInterface.put(javax.ejb.EJBLocalHome .class, entityBeanLocalHomeMethodsDisallowed); 296 disallowedMethodsPerInterface.put(javax.ejb.EJBLocalObject .class, entityBeanLocalInterfaceMethodsDisallowed); 297 } 298 return disallowedMethodsPerInterface; 299 } 300 301 private final static String entityBeanHomeMethodsDisallowed[] = { 302 "getEJBMetaData", "getHomeHandle" 303 }; 304 private final static String entityBeanRemoteMethodsDisallowed[] = { 305 "getEJBHome", "getHandle", "getPrimaryKey", "isIdentical" 306 }; 307 private final static String entityBeanLocalHomeMethodsDisallowed[] = {}; 308 private final static String entityBeanLocalInterfaceMethodsDisallowed[] = { 309 "getEJBLocalHome", "getPrimaryKey", "isIdentical" 310 }; 311 312 private final static String sessionBeanMethodsDisallowed[] = { 313 "*" 314 }; 315 316 private final static String sessionLocalBeanMethodsDisallowed[] = { 317 "*" 318 }; 319 320 private static Map disallowedMethodsPerInterface; 321 322 } 323 | Popular Tags |