FileDocCategorySizeDatePackage
SetMethodAction.javaAPI DocGlassfish v2 API11065Fri May 04 22:34:26 BST 2007com.sun.enterprise.connectors.util

SetMethodAction

public final class SetMethodAction extends Object implements PrivilegedExceptionAction
Executes setter methods on java beans.
author
Qingqing Ouyang, Binod P.G, Sivakumar Thyagarajan

Fields Summary
private Object
bean
private Set
props
private Method[]
methods
private static final Logger
logger
Constructors Summary
public SetMethodAction(Object bean, Set props)
Accepts java bean object and properties to be set.


                  
         
            this.bean  = bean;
            this.props = props;
        
Methods Summary
private java.lang.reflect.Method[]findMethod(java.lang.String methodName)
Finds methods in the resource adapter java bean class with the same name RA developers could inadvertently not camelCase getters and/or setters and this implementation of findMethod returns both camelCased and non-Camel cased methods.

        ArrayList matchedMethods = new ArrayList();
        
        //check for CamelCased Method(s)
        for (int i = 0; i < this.methods.length; i++) {
            if (methods[i].getName().equals(methodName)){
                matchedMethods.add(methods[i]);
            }
        }
        
        //check for nonCamelCased Method(s)
        for (int i = 0; i < this.methods.length; i++) {
            if (methods[i].getName().equalsIgnoreCase(methodName)){
                matchedMethods.add(methods[i]);
            }
        }
        return (Method[])matchedMethods.toArray(new Method[]{});
    
private java.lang.reflect.MethodgetAccessorMethod(java.lang.String propertyName)
Gets the accessor method for a property

        String getterName = "get" + getCamelCasedPropertyName(propertyName);
        Method[] getterMethods = findMethod(getterName);
        if (getterMethods.length > 0) {
            return getterMethods[0];
        } else {
            return null;
        }
    
private java.lang.StringgetCamelCasedPropertyName(java.lang.String propertyName)
Returns camel-cased version of a propertyName. Used to construct correct accessor and mutator method names for a give property.

        return propertyName.substring(0,1).toUpperCase() +
        propertyName.substring(1);
    
private static java.lang.StringgetFilteredPropValue(com.sun.enterprise.deployment.EnvironmentProperty prop)

        if (prop == null)
            return "null";
        
	String propname = prop.getName();
	if (propname.toLowerCase().contains("password"))
	     return "********";
        
        return (prop.getResolvedValue());
    
private java.lang.reflect.MethodgetMutatorMethod(java.lang.String propertyName, java.lang.Class type)
Retrieves the appropriate setter method in the resurce adapter java bean class

        String setterMethodName = "set" + getCamelCasedPropertyName(propertyName);
        Method m = null;

        //Get all setter methods for property
        Method[] setterMethods = findMethod(setterMethodName);
		
    	if (setterMethods.length == 1) {
    	    //Only one setter method for this property 
    		m = (Method)setterMethods[0];
    	} else {
    	    //When more than one setter for the property, do type
    	    //checking to determine property
            //This check is very important, because the resource
            //adapter java-bean's methods might be overridden and calling
            //set over the wrong method will result in an exception
            for (int i =0; i < setterMethods.length; i++) {
                Class[] paramTypes = setterMethods[i].getParameterTypes();
            	if(paramTypes.length > 0) {
                    if (paramTypes[0].equals(type) && paramTypes.length == 1){
                        logger.log(Level.FINER, "Method " +  methods[i] + 
                                        "matches with the right arg type");
                        m = setterMethods[i];
                    }
                }  
           }
        }
        
        if ( m != null ) {
            return m; 
        } else {
            logger.log(Level.WARNING, "no.such.method", 
                  new Object[] {setterMethodName, bean.getClass().getName()});
            return null;
        }
    
private java.lang.ClassgetTypeOf(com.sun.enterprise.deployment.EnvironmentProperty prop)
Use a property's accessor method in the resource adapter javabean to get the Type of the property This helps in ensuring that the type as coded in the java-bean is used while setting values on a java-bean instance, rather than on the values specified in ra.xml

    	String name = prop.getName();
        Method accessorMeth = getAccessorMethod(name);
        if (accessorMeth != null ) {
            return accessorMeth.getReturnType();
        }
        //not having a getter is not a WARNING.
    	logger.log(Level.FINE, "method.name.nogetterforproperty", 
                new Object[] {prop.getName(), bean.getClass()});
    	return null;
    
private voidhandleException(java.lang.Exception ex, com.sun.enterprise.deployment.EnvironmentProperty prop, java.lang.Object bean)

        logger.log(Level.WARNING, "rardeployment.exception_on_invoke_setter", 
            new Object[]{prop.getName(), getFilteredPropValue(prop), 
            ex.getMessage()});
        logger.log(Level.FINE, "Exception while trying to set " +
            prop.getName() + " and value "+ getFilteredPropValue(prop),
            ex + " on an instance of " + bean.getClass());
        throw (ConnectorRuntimeException) 
        (new ConnectorRuntimeException(ex.getMessage()).initCause(ex));
    
public java.lang.Objectrun()
Executes the setter methods in the java bean.

        Iterator it = props.iterator();
        methods = bean.getClass().getMethods();
        while (it.hasNext()) {
            EnvironmentProperty prop = (EnvironmentProperty) it.next();
            String propName = prop.getName();
            Class type = getTypeOf(prop);
            //If there were no getter, use the EnvironmentProperty's
            //property type
            if ( type == null ) {
                type = Class.forName(prop.getType());
            } 
            
	    if (prop.getResolvedValue() != null &&
		prop.getResolvedValue().trim().length() != 0) {
                Method meth = null;
                try {
                    meth = getMutatorMethod(propName, type);
                    if (meth != null) {
                    	logger.log(Level.FINER, "Invoking" + meth + " on "
                                   + bean.getClass().getName() + "with " +
                                   "value [" + prop.getResolvedValueObject().getClass() 
                                   + "  , " + getFilteredPropValue(prop) +" ] ");
                        meth.invoke(bean,new Object[] {prop.getResolvedValueObject()});
                    } else {
                        //log WARNING, deployment can continue.
                        logger.log(Level.WARNING, "rardeployment.no_setter_method", 
                                        new Object[]{prop.getName(), bean.getClass().getName()});
                    }
                } catch (IllegalArgumentException ia) {
                    logger.log(Level.FINE, "IllegalException while trying to set " +
                              prop.getName() + " and value "+ getFilteredPropValue(prop),
                              ia + " on an instance of " + bean.getClass() +
                              " -- trying again with the type from bean");
                    boolean prevBoundsChecking = EnvironmentProperty.isBoundsChecking();
                    try {
                        EnvironmentProperty.setBoundsChecking(false);
                        prop.setType(type.getName());
                        logger.log(Level.FINE, "2nd try :: Invoking" + meth + " on "
                                        + bean.getClass().getName() + "with value [" 
                                        + prop.getResolvedValueObject().getClass() 
                                        + "  , " + getFilteredPropValue(prop) +" ] ");
                        meth.invoke(bean,new Object[] {prop.getResolvedValueObject()});
                    } catch (Exception e) {
                        handleException(e, prop, bean);
                    } finally {
                        //restore boundsChecking
                        EnvironmentProperty.setBoundsChecking(prevBoundsChecking);
                    }
                } catch (Exception ex) {
                    handleException(ex, prop, bean);
                }
	    }
        }
        return null;