KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > BeanMethodCalculator


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24 package com.sun.enterprise.util;
25
26 import java.util.*;
27 import java.lang.reflect.*;
28 import com.sun.enterprise.deployment.*;
29
30 /**
31  * Utility class to calculate the list of methods required to have transaction
32  * attributes
33  *
34  * @author Jerome Dochez
35  * @version
36  */

37 public class BeanMethodCalculator {
38     
39     
40     public static Vector getPossibleCmpCmrFields(ClassLoader JavaDoc cl,
41                                                  String JavaDoc className)
42         throws Exception JavaDoc {
43
44         Vector fieldDescriptors = new Vector();
45         Class JavaDoc theClass = cl.loadClass(className);
46
47         // Start with all *public* methods
48
Method[] methods = theClass.getMethods();
49
50         // Find all accessors that could be cmp fields. This list
51
// will contain all cmr field accessors as well, since there
52
// is no good way to distinguish between the two purely based
53
// on method signature.
54
for(int mIndex = 0; mIndex < methods.length; mIndex++) {
55             Method next = methods[mIndex];
56             String JavaDoc nextName = next.getName();
57             int nextModifiers = next.getModifiers();
58             if( Modifier.isAbstract(nextModifiers) ) {
59                 if( nextName.startsWith("get") &&
60                     nextName.length() > 3 ) {
61                     String JavaDoc 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 JavaDoc classLoader)
72             throws ClassNotFoundException JavaDoc
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 JavaDoc 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 JavaDoc 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 JavaDoc c) {
106         
107         methods.addAll(Arrays.asList(c.getMethods()));
108     }
109     
110     /**
111      * @return a collection of MethodDescriptor for all the methods of my
112      * ejb which are elligible to have a particular transaction setting.
113      */

114     public static Collection getTransactionalMethodsFor(EjbDescriptor ejbDescriptor, ClassLoader JavaDoc loader)
115         throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
116     {
117         // only set if desc is a stateful session bean. NOTE that
118
// !statefulSessionBean does not imply stateless session bean
119
boolean statefulSessionBean = false;
120
121         Vector methods = new Vector();
122         if (ejbDescriptor instanceof EjbSessionDescriptor) {
123             statefulSessionBean =
124                 ((EjbSessionDescriptor) ejbDescriptor).isStateful();
125             
126         // Session Beans
127
if (ejbDescriptor.isRemoteInterfacesSupported()) {
128                 Collection disallowedMethods = extractDisallowedMethodsFor(javax.ejb.EJBObject JavaDoc.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 JavaDoc intfName :
136                         ejbDescriptor.getRemoteBusinessClassNames() ) {
137
138                     Class JavaDoc 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 JavaDoc.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 JavaDoc intfName :
157                         ejbDescriptor.getLocalBusinessClassNames() ) {
158
159                     Class JavaDoc 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 JavaDoc 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             // entity beans local interfaces
182
String JavaDoc homeIntf = ejbDescriptor.getHomeClassName();
183             if (homeIntf!=null) {
184  
185                 Class JavaDoc home = loader.loadClass(homeIntf);
186                 Collection potentials = getTransactionMethodsFor(javax.ejb.EJBHome JavaDoc.class, home);
187                 transformAndAdd(potentials, MethodDescriptor.EJB_HOME, methods);
188                 
189                 String JavaDoc remoteIntf = ejbDescriptor.getRemoteClassName();
190                 Class JavaDoc remote = loader.loadClass(remoteIntf);
191                 potentials = getTransactionMethodsFor(javax.ejb.EJBObject JavaDoc.class, remote);
192                 transformAndAdd(potentials, MethodDescriptor.EJB_REMOTE, methods);
193             }
194             
195             // enity beans remote interfaces
196
String JavaDoc localHomeIntf = ejbDescriptor.getLocalHomeClassName();
197             if (localHomeIntf!=null) {
198                 Class JavaDoc home = loader.loadClass(localHomeIntf);
199                 Collection potentials = getTransactionMethodsFor(javax.ejb.EJBLocalHome JavaDoc.class, home);
200                 transformAndAdd(potentials, MethodDescriptor.EJB_LOCALHOME, methods);
201                 
202                 String JavaDoc remoteIntf = ejbDescriptor.getLocalClassName();
203                 Class JavaDoc remote = loader.loadClass(remoteIntf);
204                 potentials = getTransactionMethodsFor(javax.ejb.EJBLocalObject JavaDoc.class, remote);
205                 transformAndAdd(potentials, MethodDescriptor.EJB_LOCAL, methods);
206             }
207         }
208
209         if( !statefulSessionBean ) {
210             Class JavaDoc 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 JavaDoc loader, String JavaDoc interfaceName, Collection disallowedMethods)
220         throws ClassNotFoundException JavaDoc
221      {
222          Class JavaDoc clazz = loader.loadClass(interfaceName);
223          return getTransactionMethodsFor(clazz, disallowedMethods);
224      }
225      
226      private static Collection getTransactionMethodsFor(Class JavaDoc 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 JavaDoc interfaceType, Class JavaDoc interfaceImpl) {
233          Collection disallowedTransactionMethods = getDisallowedTransactionMethodsFor(interfaceType);
234          return getTransactionMethodsFor(interfaceImpl, disallowedTransactionMethods);
235      }
236      
237      private static Collection getDisallowedTransactionMethodsFor(Class JavaDoc interfaceType) {
238          return extractDisallowedMethodsFor(interfaceType, getDisallowedMethodsNamesFor(interfaceType));
239      }
240      
241      // from EJB 2.0 spec section 17.4.1
242
private static Collection extractDisallowedMethodsFor(Class JavaDoc interfaceType, String JavaDoc[] methodNames) {
243          
244          Vector v = new Vector();
245          // no disallowed methods for this interface
246
if (methodNames.length==0)
247              return v;
248          
249          Method methods[] = interfaceType.getMethods();
250          
251          for (int i=0; i<methods.length; i++) {
252             // all methods of the interface are disallowed
253
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      /**
262       * utiliy method to transform our collection of Method objects into
263       * MethodDescriptor objects and add them to our global list of
264       * elligible methods
265       * @param the collection of acceptable method objects
266       * @param the method-intf identifier for those methods
267       * @param the global list of MethodDescriptors objects
268       */

269      private static void transformAndAdd(Collection methods, String JavaDoc 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      /**
279       * @return the list of disallowed methods for a particular interface
280       */

281      private static String JavaDoc[] getDisallowedMethodsNamesFor(Class JavaDoc interfaceType) {
282          return (String JavaDoc[]) getDisallowedMethodsNames().get(interfaceType);
283      }
284      
285      /**
286       * @return a Map of disallowed methods per interface type. The key to the
287       * map is the interface type (e.g. EJBHome, EJBObject), the value
288       * is an array of methods names disallowed to have transaction attributes
289       */

290      protected static Map getDisallowedMethodsNames() {
291          if (disallowedMethodsPerInterface==null) {
292             disallowedMethodsPerInterface = new Hashtable();
293             disallowedMethodsPerInterface.put(javax.ejb.EJBHome JavaDoc.class, entityBeanHomeMethodsDisallowed);
294             disallowedMethodsPerInterface.put(javax.ejb.EJBObject JavaDoc.class, entityBeanRemoteMethodsDisallowed);
295             disallowedMethodsPerInterface.put(javax.ejb.EJBLocalHome JavaDoc.class, entityBeanLocalHomeMethodsDisallowed);
296             disallowedMethodsPerInterface.put(javax.ejb.EJBLocalObject JavaDoc.class, entityBeanLocalInterfaceMethodsDisallowed);
297          }
298          return disallowedMethodsPerInterface;
299      }
300      
301      private final static String JavaDoc entityBeanHomeMethodsDisallowed[] = {
302          "getEJBMetaData", "getHomeHandle"
303      };
304      private final static String JavaDoc entityBeanRemoteMethodsDisallowed[] = {
305          "getEJBHome", "getHandle", "getPrimaryKey", "isIdentical"
306      };
307      private final static String JavaDoc entityBeanLocalHomeMethodsDisallowed[] = {};
308      private final static String JavaDoc entityBeanLocalInterfaceMethodsDisallowed[] = {
309         "getEJBLocalHome", "getPrimaryKey", "isIdentical"
310      };
311      
312      private final static String JavaDoc sessionBeanMethodsDisallowed[] = {
313          "*"
314      };
315      
316      private final static String JavaDoc sessionLocalBeanMethodsDisallowed[] = {
317          "*"
318      };
319      
320      private static Map disallowedMethodsPerInterface;
321      
322 }
323
Popular Tags