1 2 24 25 package com.sun.enterprise.connectors.util; 26 27 import com.sun.enterprise.deployment.*; 28 import com.sun.enterprise.config.serverbeans.ElementProperty; 29 import com.sun.enterprise.connectors.*; 30 import com.sun.enterprise.util.*; 31 import com.sun.logging.LogDomains; 32 import java.util.logging.*; 33 import java.util.*; 34 import java.lang.*; 35 import java.lang.reflect.*; 36 import java.io.IOException ; 37 import org.xml.sax.SAXParseException ; 38 import javax.resource.spi.ResourceAdapter ; 39 import javax.resource.spi.ResourceAdapterAssociation ; 40 41 42 49 50 public class ConnectorConfigParserUtils { 51 52 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 53 54 58 59 public ConnectorConfigParserUtils() { 60 61 } 62 63 73 74 public Properties mergeProps(Set ddVals, 75 Properties introspectedVals) 76 { 77 Properties mergedVals = new Properties(introspectedVals); 78 79 if(ddVals != null) { 80 Object [] ddProps = ddVals.toArray(); 81 82 String name = null; 83 String value = null; 84 for (int i = 0; i < ddProps.length; i++) { 85 name = ((EnvironmentProperty)ddProps[i]).getName(); 86 value =((EnvironmentProperty)ddProps[i]).getValue(); 87 mergedVals.setProperty(name,value); 88 } 89 } 90 91 return mergedVals; 92 } 93 94 109 110 public Properties mergePropsReturnTypes(Set ddVals, 111 Properties introspectedVals) 112 { 113 Properties mergedVals = new Properties(introspectedVals); 114 115 if(ddVals != null) { 116 Object [] ddProps = ddVals.toArray(); 117 118 String name = null; 119 String value = null; 120 for (int i = 0; i < ddProps.length; i++) { 121 name = ((EnvironmentProperty)ddProps[i]).getName(); 122 value = ((EnvironmentProperty)ddProps[i]).getType(); 123 mergedVals.setProperty(name,value); 124 } 125 } 126 127 return mergedVals; 128 } 129 130 public Properties introspectJavaBean(String className, Set ddPropsSet) 131 throws ConnectorRuntimeException { 132 return introspectJavaBean(className, ddPropsSet, false, null); 133 } 134 135 public Properties introspectJavaBean(String className, Set ddPropsSet, 136 boolean associateResourceAdapter, String resourceAdapterName) 137 throws ConnectorRuntimeException { 138 Class loadedClass = loadClass(className); 139 Object loadedInstance = instantiate(loadedClass); 140 try { 141 if (associateResourceAdapter) { 142 ActiveResourceAdapter activeRA = ConnectorRegistry.getInstance(). 143 getActiveResourceAdapter(resourceAdapterName); 144 if (activeRA == null) { 145 (new ConnectorServiceImpl()).loadDeferredResourceAdapter( 147 resourceAdapterName); 148 activeRA = ConnectorRegistry.getInstance(). 149 getActiveResourceAdapter(resourceAdapterName); 150 } 151 152 if (activeRA instanceof ActiveInboundResourceAdapter) { 154 ResourceAdapter raInstance = 155 ((ActiveInboundResourceAdapter)(activeRA)).getResourceAdapter(); 156 if (loadedInstance instanceof ResourceAdapterAssociation ) { 157 ((ResourceAdapterAssociation )loadedInstance). 158 setResourceAdapter(raInstance); 159 } 160 } 161 } 162 } catch (Exception e) { 163 _logger.log(Level.WARNING, 164 "rardeployment.error_associating_ra",e); 165 _logger.log(Level.FINE, 166 "Exception while associating the resource adapter" + 167 "to the JavaBean",e); 168 } 169 return introspectJavaBean(loadedInstance, ddPropsSet); 170 } 171 172 173 174 188 189 public Properties introspectJavaBean( 190 Object javaBeanInstance ,Set ddPropsSet) throws ConnectorRuntimeException 191 { 192 Class loadedClass = javaBeanInstance.getClass(); 193 194 Method[] methods = loadedClass.getMethods(); 195 if(methods == null) { 196 return null; 197 } 198 Properties props = new Properties(); 199 String name = null; 200 String value = null; 201 Object [] ddProps = null; 202 if(ddPropsSet != null) { 203 ddProps = ddPropsSet.toArray(); 204 } 205 206 for(int i=0; i<methods.length;++i) { 207 _logger.fine("Method -> " + methods[i].getName() + ":" + methods[i].getReturnType()); 208 if(isProperty(methods[i]) && !presentInDDProps(methods[i],ddProps) 209 && isValid(methods[i], loadedClass)) { 210 name = getPropName(methods[i]); 211 value = getPropValue(methods[i], loadedClass, javaBeanInstance); 212 props.setProperty(name,value); 213 } 214 } 215 return props; 216 } 217 218 233 234 public Properties introspectJavaBeanReturnTypes( 235 String className,Set ddPropsSet) throws ConnectorRuntimeException 236 { 237 238 Class loadedClass = loadClass(className); 239 Object loadedInstance = instantiate(loadedClass); 240 Method[] methods = loadedClass.getMethods(); 241 if(methods == null) { 242 return null; 243 } 244 Properties props = new Properties(); 245 String name = null; 246 String value = null; 247 Object [] ddProps = null; 248 if(ddPropsSet != null) { 249 ddProps = ddPropsSet.toArray(); 250 } 251 252 for(int i=0; i<methods.length;++i) { 253 if(isProperty(methods[i])&&!presentInDDProps(methods[i],ddProps)) { 254 name = getPropName(methods[i]); 255 value = getPropType(methods[i]); 256 if(value != null) { 257 props.setProperty(name,value); 258 } 259 } 260 } 261 return props; 262 } 263 269 270 private boolean presentInDDProps(Method method,Object [] ddProps) { 271 272 String name = null; 273 String ddPropName = null; 274 int length = "set".length(); 275 if(method != null) { 276 name = method.getName().substring(length); 277 } 278 for(int i=0; name != null && ddProps != null && i<ddProps.length;++i) { 279 ddPropName = ((EnvironmentProperty)ddProps[i]).getName(); 280 if(name.equalsIgnoreCase(ddPropName) == true) { 281 return true; 282 } 283 } 284 return false; 285 } 286 287 290 private boolean isValid(Method setMethod, Class loadedClass) { 291 Method getMethod = correspondingGetMethod( setMethod, loadedClass); 292 if (getMethod != null) { 293 return RARUtils.isValidRABeanConfigProperty(getMethod.getReturnType()); 294 } else { 295 return false; 296 } 297 } 298 299 306 307 private boolean isProperty(Method method) { 308 309 if(method == null) { 310 return false; 311 } 312 String methodName = method.getName(); 313 Class [] parameterTypes = method.getParameterTypes(); 314 if(methodName.startsWith("set") && 315 parameterTypes != null && parameterTypes.length == 1) { 316 return true; 317 } else { 318 return false; 319 } 320 } 321 322 329 330 private String getPropName(Method method) { 331 332 if(method == null) { 333 return null; 334 } 335 String methodName = method.getName(); 336 int length = "set".length(); 337 String retValue = 338 methodName.substring(length,length+1).toUpperCase() + 339 methodName.substring(length+1); 340 return retValue; 341 } 342 343 349 350 private Method correspondingGetMethod(Method setMethod, 351 Class loadedClass) { 352 353 Method[] allMethods = loadedClass.getMethods(); 354 if(allMethods == null) { 355 return null; 356 } 357 int length = "set".length(); 358 String methodName = setMethod.getName(); 359 Class [] parameterTypes = null; 360 String [] possibleGetMethodNames = new String [2]; 361 possibleGetMethodNames[0] = "is"+methodName.substring(length); 362 possibleGetMethodNames[1] = "get"+methodName.substring(length); 363 364 for(int i = 0;i < allMethods.length;++i) { 365 if(allMethods[i].getName().equals(possibleGetMethodNames[0]) || 366 allMethods[i].getName().equals(possibleGetMethodNames[1])) { 367 parameterTypes = allMethods[i].getParameterTypes(); 368 if(parameterTypes != null && parameterTypes.length == 0) { 369 return allMethods[i]; 370 } 371 } 372 } 373 return null; 374 } 375 376 381 382 private String getPropValue(Method method, 383 Class loadedClass, Object loadedInstance) { 384 385 Object retValue = null; 386 Method getMethod = correspondingGetMethod(method, loadedClass); 387 388 if(getMethod != null) { 389 try { 390 retValue = getMethod.invoke(loadedInstance, (java.lang.Object [])null); 391 } catch (IllegalAccessException ie) { 392 _logger.log(Level.FINE, 393 "rardeployment.illegalaccess_error",loadedClass.getName()); 394 } catch (InvocationTargetException ie) { 395 _logger.log(Level.FINE, 396 "Failed to invoke the method",loadedClass.getName()); 397 } 398 } 399 return convertToString(retValue); 400 } 401 402 private String getPropType(Method method) { 403 404 Class [] parameterTypeClass = method.getParameterTypes(); 405 if(parameterTypeClass.length != 1) { 406 return null; 407 } 408 if(parameterTypeClass[0].isPrimitive() || 409 parameterTypeClass[0].getName().equals("java.lang.String")) { 410 return parameterTypeClass[0].getName(); 411 } else { 412 return null; 413 } 414 } 415 416 420 421 private String convertToString(Object obj) { 422 if(obj == null) { 423 return new String (); 424 } 425 426 if(obj instanceof String ) { 427 return (String )obj; 428 }else if( obj instanceof Integer || 429 obj instanceof Float || 430 obj instanceof Long || 431 obj instanceof Double || 432 obj instanceof Character || 433 obj instanceof Boolean || 434 obj instanceof Byte || 435 obj instanceof Short ) { 436 return String.valueOf(obj); 437 } else { 438 return new String (); 439 } 440 } 441 442 443 447 448 private Class loadClass(String className) 449 throws ConnectorRuntimeException 450 { 451 ClassLoader classLoader = ConnectorClassLoader.getInstance(); 452 try { 453 return classLoader.loadClass(className); 454 } catch(ClassNotFoundException ce) { 455 _logger.log(Level.FINE, 456 "rardeployment.class_not_found",className); 457 throw new ConnectorRuntimeException( 458 "ClassNot Found : " + className); 459 } 460 } 461 464 465 private Object instantiate(Class loadedClass) 466 throws ConnectorRuntimeException 467 { 468 try { 469 return loadedClass.newInstance(); 470 } catch(InstantiationException ie) { 471 _logger.log(Level.FINE, 472 "rardeployment.class_instantiation_error",loadedClass.getName()); 473 throw new ConnectorRuntimeException( 474 "Could not instantiate class : " + loadedClass.getName()); 475 } catch (IllegalAccessException ie) { 476 _logger.log(Level.FINE, 477 "rardeployment.illegalaccess_error",loadedClass.getName()); 478 throw new ConnectorRuntimeException( 479 "Couldnot access class : "+loadedClass.getName()); 480 } 481 } 482 } 483 | Popular Tags |