FileDocCategorySizeDatePackage
BeanMethodCalculator.javaAPI DocGlassfish v2 API15004Fri May 04 22:32:06 BST 2007com.sun.enterprise.util

BeanMethodCalculator

public class BeanMethodCalculator extends Object
Utility class to calculate the list of methods required to have transaction attributes
author
Jerome Dochez
version

Fields Summary
private static final String[]
entityBeanHomeMethodsDisallowed
private static final String[]
entityBeanRemoteMethodsDisallowed
private static final String[]
entityBeanLocalHomeMethodsDisallowed
private static final String[]
entityBeanLocalInterfaceMethodsDisallowed
private static final String[]
sessionBeanMethodsDisallowed
private static final String[]
sessionLocalBeanMethodsDisallowed
private static Map
disallowedMethodsPerInterface
Constructors Summary
Methods Summary
private static voidaddAllInterfaceMethodsIn(java.util.Collection methods, java.lang.Class c)

        
        methods.addAll(Arrays.asList(c.getMethods()));
    
private static java.util.CollectionextractDisallowedMethodsFor(java.lang.Class interfaceType, java.lang.String[] methodNames)

         
         Vector v = new Vector();
         // no disallowed methods for this interface 
         if (methodNames.length==0)
             return v;
         
         Method methods[] = interfaceType.getMethods();
         
         for (int i=0; i<methods.length; i++) {
            // all methods of the interface are disallowed
            if (methodNames[0].equals("*"))
                 v.addElement(methods[i]);             
            else if (Arrays.binarySearch(methodNames, methods[i].getName())>=0) 
                 v.addElement(methods[i]);
         }
         return v;
     
protected static java.util.MapgetDisallowedMethodsNames()

return
a Map of disallowed methods per interface type. The key to the map is the interface type (e.g. EJBHome, EJBObject), the value is an array of methods names disallowed to have transaction attributes

         if (disallowedMethodsPerInterface==null) {
            disallowedMethodsPerInterface = new Hashtable();
            disallowedMethodsPerInterface.put(javax.ejb.EJBHome.class, entityBeanHomeMethodsDisallowed);
            disallowedMethodsPerInterface.put(javax.ejb.EJBObject.class, entityBeanRemoteMethodsDisallowed);
            disallowedMethodsPerInterface.put(javax.ejb.EJBLocalHome.class, entityBeanLocalHomeMethodsDisallowed);
            disallowedMethodsPerInterface.put(javax.ejb.EJBLocalObject.class, entityBeanLocalInterfaceMethodsDisallowed);
         } 
         return disallowedMethodsPerInterface;
     
private static java.lang.String[]getDisallowedMethodsNamesFor(java.lang.Class interfaceType)

return
the list of disallowed methods for a particular interface

         return (String[]) getDisallowedMethodsNames().get(interfaceType);
     
private static java.util.CollectiongetDisallowedTransactionMethodsFor(java.lang.Class interfaceType)

         return extractDisallowedMethodsFor(interfaceType, getDisallowedMethodsNamesFor(interfaceType));                  
     
public static java.util.VectorgetMethodsFor(EjbDescriptor ejbDescriptor, java.lang.ClassLoader classLoader)

        Vector methods = new Vector();
        
        if (ejbDescriptor.isRemoteInterfacesSupported()) {
            addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getHomeClassName()));
            addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getRemoteClassName()));
        } 

        if (ejbDescriptor.isRemoteBusinessInterfacesSupported()) {
            for(String intf : ejbDescriptor.getRemoteBusinessClassNames()) {
                addAllInterfaceMethodsIn(methods, classLoader.loadClass(intf));
            }
        }

        if (ejbDescriptor.isLocalInterfacesSupported()) {
            addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getLocalHomeClassName()));
            addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getLocalClassName()));
        }

        if (ejbDescriptor.isLocalBusinessInterfacesSupported()) {
            for(String intf : ejbDescriptor.getLocalBusinessClassNames()) {
                addAllInterfaceMethodsIn(methods, classLoader.loadClass(intf));
            }
        }

        if (ejbDescriptor.hasWebServiceEndpointInterface()) {
            addAllInterfaceMethodsIn(methods, classLoader.loadClass(ejbDescriptor.getWebServiceEndpointInterfaceName()));
                                                                    
        }
        return methods;
    
public static java.util.VectorgetPossibleCmpCmrFields(java.lang.ClassLoader cl, java.lang.String className)


        Vector fieldDescriptors = new Vector();
        Class theClass = cl.loadClass(className);

        // Start with all *public* methods
        Method[] methods = theClass.getMethods();

        // Find all accessors that could be cmp fields. This list 
        // will contain all cmr field accessors as well, since there
        // is no good way to distinguish between the two purely based
        // on method signature.
        for(int mIndex = 0; mIndex < methods.length; mIndex++) {
            Method next = methods[mIndex];
            String nextName = next.getName();
            int nextModifiers = next.getModifiers();
            if( Modifier.isAbstract(nextModifiers) ) {
                if( nextName.startsWith("get") &&
                    nextName.length() > 3 ) {
                    String field = 
                        nextName.substring(3,4).toLowerCase() + 
                        nextName.substring(4);
                    fieldDescriptors.add(new FieldDescriptor(field));
                }
            }
        }
        return fieldDescriptors;
    
private static java.util.CollectiongetTransactionMethodsFor(java.lang.ClassLoader loader, java.lang.String interfaceName, java.util.Collection disallowedMethods)

         Class clazz = loader.loadClass(interfaceName);
         return getTransactionMethodsFor(clazz, disallowedMethods);
     
private static java.util.CollectiongetTransactionMethodsFor(java.lang.Class interfaceImpl, java.util.Collection disallowedMethods)

         Vector v = new Vector(Arrays.asList(interfaceImpl.getMethods()));
         v.removeAll(disallowedMethods);
         return v;
     
private static java.util.CollectiongetTransactionMethodsFor(java.lang.Class interfaceType, java.lang.Class interfaceImpl)

         Collection disallowedTransactionMethods = getDisallowedTransactionMethodsFor(interfaceType);
         return getTransactionMethodsFor(interfaceImpl, disallowedTransactionMethods);
     
public static java.util.CollectiongetTransactionalMethodsFor(EjbDescriptor ejbDescriptor, java.lang.ClassLoader loader)

return
a collection of MethodDescriptor for all the methods of my ejb which are elligible to have a particular transaction setting.

        // only set if desc is a stateful session bean.  NOTE that 
        // !statefulSessionBean does not imply stateless session bean
        boolean statefulSessionBean = false;

        Vector methods = new Vector();
        if (ejbDescriptor instanceof EjbSessionDescriptor) {
            statefulSessionBean = 
                ((EjbSessionDescriptor) ejbDescriptor).isStateful();
            
	    // Session Beans
            if (ejbDescriptor.isRemoteInterfacesSupported()) {                
                Collection disallowedMethods = extractDisallowedMethodsFor(javax.ejb.EJBObject.class, sessionBeanMethodsDisallowed);
                Collection potentials = getTransactionMethodsFor(loader, ejbDescriptor.getRemoteClassName() , disallowedMethods);
                transformAndAdd(potentials, MethodDescriptor.EJB_REMOTE, methods);
            }
            
            if( ejbDescriptor.isRemoteBusinessInterfacesSupported() ) {
                
                for(String intfName : 
                        ejbDescriptor.getRemoteBusinessClassNames() ) {

                    Class businessIntf = loader.loadClass(intfName);
                    Method[] busIntfMethods = businessIntf.getMethods();
                    for (Method next : busIntfMethods ) {
                        methods.add(new MethodDescriptor
                                    (next, MethodDescriptor.EJB_REMOTE));
                    }
                }
            }

            if (ejbDescriptor.isLocalInterfacesSupported()) {
                Collection disallowedMethods = extractDisallowedMethodsFor(javax.ejb.EJBLocalObject.class, sessionLocalBeanMethodsDisallowed);
                Collection potentials = getTransactionMethodsFor(loader, ejbDescriptor.getLocalClassName() , disallowedMethods);
                transformAndAdd(potentials, MethodDescriptor.EJB_LOCAL, methods);
                
            }

            if( ejbDescriptor.isLocalBusinessInterfacesSupported() ) {
                
                for(String intfName : 
                        ejbDescriptor.getLocalBusinessClassNames() ) {

                    Class businessIntf = loader.loadClass(intfName);
                    Method[] busIntfMethods = businessIntf.getMethods();
                    for (Method next : busIntfMethods ) {
                        methods.add(new MethodDescriptor
                                    (next, MethodDescriptor.EJB_LOCAL));
                    }
                }
            }

            if (ejbDescriptor.hasWebServiceEndpointInterface()) {
                Class webServiceClass = loader.loadClass
                    (ejbDescriptor.getWebServiceEndpointInterfaceName());
                
                Method[] webMethods = webServiceClass.getMethods();                
                for (int i=0;i<webMethods.length;i++) {
                    methods.add(new MethodDescriptor(webMethods[i],  
                                MethodDescriptor.EJB_WEB_SERVICE));
                    
                }
            }

        } else {
            // entity beans local interfaces
            String homeIntf = ejbDescriptor.getHomeClassName();
            if (homeIntf!=null) {                
 
                Class home = loader.loadClass(homeIntf);
                Collection potentials = getTransactionMethodsFor(javax.ejb.EJBHome.class, home);
                transformAndAdd(potentials, MethodDescriptor.EJB_HOME, methods);
                
                String remoteIntf = ejbDescriptor.getRemoteClassName();                
                Class remote = loader.loadClass(remoteIntf);
                potentials = getTransactionMethodsFor(javax.ejb.EJBObject.class, remote);
                transformAndAdd(potentials, MethodDescriptor.EJB_REMOTE, methods);
            } 
            
            // enity beans remote interfaces
            String localHomeIntf = ejbDescriptor.getLocalHomeClassName();
            if (localHomeIntf!=null) { 
                Class home = loader.loadClass(localHomeIntf);
                Collection potentials = getTransactionMethodsFor(javax.ejb.EJBLocalHome.class, home);
                transformAndAdd(potentials, MethodDescriptor.EJB_LOCALHOME, methods);
                
                String remoteIntf = ejbDescriptor.getLocalClassName();                
                Class remote = loader.loadClass(remoteIntf);
                potentials = getTransactionMethodsFor(javax.ejb.EJBLocalObject.class, remote);
                transformAndAdd(potentials, MethodDescriptor.EJB_LOCAL, methods);                
            }
        }

        if( !statefulSessionBean ) {
            Class ejbClass = loader.loadClass(ejbDescriptor.getEjbClassName());
            if( ejbDescriptor.isTimedObject() ) {
                methods.add(ejbDescriptor.getEjbTimeoutMethod());
            }
        }

        return methods;
     
private static voidtransformAndAdd(java.util.Collection methods, java.lang.String methodIntf, java.util.Vector globalList)
utiliy method to transform our collection of Method objects into MethodDescriptor objects and add them to our global list of elligible methods

param
the collection of acceptable method objects
param
the method-intf identifier for those methods
param
the global list of MethodDescriptors objects

         
         for (Iterator itr = methods.iterator();itr.hasNext();) {
             Method m = (Method) itr.next();
             MethodDescriptor md = new MethodDescriptor(m, methodIntf);
             globalList.add(md);
         }