1 23 24 package com.sun.enterprise.connectors.util; 25 26 import com.sun.enterprise.deployment.EnvironmentProperty; 27 28 import java.util.ArrayList ; 29 import java.util.Set ; 30 import java.util.Iterator ; 31 import java.lang.reflect.Method ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import com.sun.logging.LogDomains; 35 import java.security.PrivilegedAction ; 36 import java.security.PrivilegedActionException ; 37 import java.security.PrivilegedExceptionAction ; 38 import com.sun.enterprise.connectors.ConnectorRuntimeException; 39 40 45 public class SetMethodAction implements PrivilegedExceptionAction { 46 47 private Object bean; 48 private Set props; 49 private Method [] methods; 50 51 private static Logger logger = 52 LogDomains.getLogger(LogDomains.RSR_LOGGER); 53 54 57 public SetMethodAction(Object bean, Set props) { 58 this.bean = bean; 59 this.props = props; 60 } 61 62 65 public Object run() throws Exception { 66 Iterator it = props.iterator(); 67 methods = bean.getClass().getMethods(); 68 while (it.hasNext()) { 69 EnvironmentProperty prop = (EnvironmentProperty) it.next(); 70 String propName = prop.getName(); 71 Class type = getTypeOf(prop); 72 if ( type == null ) { 75 type = Class.forName(prop.getType()); 76 } 77 78 if (prop.getResolvedValue() != null && 79 prop.getResolvedValue().trim().length() != 0) { 80 Method meth = null; 81 try { 82 meth = getMutatorMethod(propName, type); 83 if (meth != null) { 84 logger.log(Level.FINER, "Invoking" + meth + " on " 85 + bean.getClass().getName() + "with " + 86 "value [" + prop.getResolvedValueObject().getClass() 87 + " , " + getFilteredPropValue(prop) +" ] "); 88 meth.invoke(bean,new Object [] {prop.getResolvedValueObject()}); 89 } else { 90 logger.log(Level.WARNING, "rardeployment.no_setter_method", 92 new Object []{prop.getName(), bean.getClass().getName()}); 93 } 94 } catch (IllegalArgumentException ia) { 95 logger.log(Level.FINE, "IllegalException while trying to set " + 96 prop.getName() + " and value "+ getFilteredPropValue(prop), 97 ia + " on an instance of " + bean.getClass() + 98 " -- trying again with the type from bean"); 99 boolean prevBoundsChecking = EnvironmentProperty.isBoundsChecking(); 100 try { 101 EnvironmentProperty.setBoundsChecking(false); 102 prop.setType(type.getName()); 103 logger.log(Level.FINE, "2nd try :: Invoking" + meth + " on " 104 + bean.getClass().getName() + "with value [" 105 + prop.getResolvedValueObject().getClass() 106 + " , " + getFilteredPropValue(prop) +" ] "); 107 meth.invoke(bean,new Object [] {prop.getResolvedValueObject()}); 108 } catch (Exception e) { 109 handleException(e, prop, bean); 110 } finally { 111 EnvironmentProperty.setBoundsChecking(prevBoundsChecking); 113 } 114 } catch (Exception ex) { 115 handleException(ex, prop, bean); 116 } 117 } 118 } 119 return null; 120 } 121 122 private void handleException(Exception ex, EnvironmentProperty prop, Object bean) throws ConnectorRuntimeException { 123 logger.log(Level.WARNING, "rardeployment.exception_on_invoke_setter", 124 new Object []{prop.getName(), getFilteredPropValue(prop), 125 ex.getMessage()}); 126 logger.log(Level.FINE, "Exception while trying to set " + 127 prop.getName() + " and value "+ getFilteredPropValue(prop), 128 ex + " on an instance of " + bean.getClass()); 129 throw (ConnectorRuntimeException) 130 (new ConnectorRuntimeException(ex.getMessage()).initCause(ex)); 131 } 132 133 private static String getFilteredPropValue(EnvironmentProperty prop){ 134 if (prop == null) 135 return "null"; 136 137 String propname = prop.getName(); 138 if (propname.toLowerCase().contains("password")) 139 return "********"; 140 141 return (prop.getResolvedValue()); 142 } 143 144 145 149 private Method getMutatorMethod(String propertyName, Class type){ 150 String setterMethodName = "set" + getCamelCasedPropertyName(propertyName); 151 Method m = null; 152 153 Method [] setterMethods = findMethod(setterMethodName); 155 156 if (setterMethods.length == 1) { 157 m = (Method )setterMethods[0]; 159 } else { 160 for (int i =0; i < setterMethods.length; i++) { 166 Class [] paramTypes = setterMethods[i].getParameterTypes(); 167 if(paramTypes.length > 0) { 168 if (paramTypes[0].equals(type) && paramTypes.length == 1){ 169 logger.log(Level.FINER, "Method " + methods[i] + 170 "matches with the right arg type"); 171 m = setterMethods[i]; 172 } 173 } 174 } 175 } 176 177 if ( m != null ) { 178 return m; 179 } else { 180 logger.log(Level.WARNING, "no.such.method", 181 new Object [] {setterMethodName, bean.getClass().getName()}); 182 return null; 183 } 184 } 185 186 194 private Class getTypeOf(EnvironmentProperty prop) { 195 String name = prop.getName(); 196 Method accessorMeth = getAccessorMethod(name); 197 if (accessorMeth != null ) { 198 return accessorMeth.getReturnType(); 199 } 200 logger.log(Level.FINE, "method.name.nogetterforproperty", 202 new Object [] {prop.getName(), bean.getClass()}); 203 return null; 204 } 205 206 209 private Method getAccessorMethod(String propertyName){ 210 String getterName = "get" + getCamelCasedPropertyName(propertyName); 211 Method [] getterMethods = findMethod(getterName); 212 if (getterMethods.length > 0) { 213 return getterMethods[0]; 214 } else { 215 return null; 216 } 217 } 218 219 225 private Method [] findMethod(String methodName) { 226 ArrayList matchedMethods = new ArrayList (); 227 228 for (int i = 0; i < this.methods.length; i++) { 230 if (methods[i].getName().equals(methodName)){ 231 matchedMethods.add(methods[i]); 232 } 233 } 234 235 for (int i = 0; i < this.methods.length; i++) { 237 if (methods[i].getName().equalsIgnoreCase(methodName)){ 238 matchedMethods.add(methods[i]); 239 } 240 } 241 return (Method [])matchedMethods.toArray(new Method []{}); 242 } 243 244 248 private String getCamelCasedPropertyName(String propertyName){ 249 return propertyName.substring(0,1).toUpperCase() + 250 propertyName.substring(1); 251 } 252 253 } 254 | Popular Tags |