1 22 package org.jboss.resource.connectionmanager; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import javax.management.Notification ; 31 import javax.management.ObjectName ; 32 import javax.resource.ResourceException ; 33 import javax.resource.spi.ConnectionManager ; 34 import javax.resource.spi.ConnectionRequestInfo ; 35 import javax.resource.spi.ManagedConnection ; 36 import javax.resource.spi.ManagedConnectionFactory ; 37 import javax.resource.spi.ResourceAdapter ; 38 import javax.resource.spi.ResourceAdapterAssociation ; 39 40 import org.jboss.deployment.DeploymentException; 41 import org.jboss.logging.Logger; 42 import org.jboss.metadata.MetaData; 43 import org.jboss.resource.metadata.ConfigPropertyMetaData; 44 import org.jboss.resource.metadata.ConnectionDefinitionMetaData; 45 import org.jboss.resource.metadata.ConnectorMetaData; 46 import org.jboss.system.ServiceMBeanSupport; 47 import org.jboss.util.Classes; 48 import org.jboss.util.NestedRuntimeException; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.Node ; 51 import org.w3c.dom.NodeList ; 52 65 public class RARDeployment extends ServiceMBeanSupport 66 implements RARDeploymentMBean, ManagedConnectionFactory 67 { 68 static final long serialVersionUID = -294341806721616790L; 69 70 public final static String MCF_ATTRIBUTE_CHANGED_NOTIFICATION = "jboss.mcfattributechangednotification"; 71 72 private Logger log = Logger.getLogger(getClass()); 73 74 private ObjectName oldRarDeployment; 76 77 private String rarName; 78 79 private String connectionDefinition; 80 81 private String vendorName; 82 83 private String specVersion; 84 85 private String eisType; 86 87 private String version; 88 89 private String managedConnectionFactoryClass; 90 91 private String connectionFactoryInterface; 92 93 private String connectionFactoryImplClass; 94 95 private String connectionInterface; 96 97 private String connectionImplClass; 98 99 private String transactionSupport; 100 101 private Element managedConnectionFactoryProperties; 102 103 private String authenticationMechanismType; 104 105 private String credentialInterface; 106 107 private boolean reauthenticationSupport; 108 109 private Class mcfClass; 110 111 private ManagedConnectionFactory mcf; 112 113 116 public RARDeployment() 117 { 118 } 119 120 public ObjectName getOldRarDeployment() 121 { 122 return oldRarDeployment; 123 } 124 125 public void setOldRarDeployment(final ObjectName oldRarDeployment) 126 { 127 this.oldRarDeployment = oldRarDeployment; 128 } 129 130 public String getRARName() 131 { 132 return rarName; 133 } 134 135 public void setRARName(String rarName) 136 { 137 this.rarName = rarName; 138 } 139 140 public String getConnectionDefinition() 141 { 142 return connectionDefinition; 143 } 144 145 public void setConnectionDefinition(String connectionDefinition) 146 { 147 this.connectionDefinition = connectionDefinition; 148 } 149 150 public String getVendorName() 151 { 152 return vendorName; 153 } 154 155 public void setVendorName(String vendorName) 156 { 157 this.vendorName = vendorName; 158 } 159 160 public String getSpecVersion() 161 { 162 return specVersion; 163 } 164 165 public void setSpecVersion(String specVersion) 166 { 167 this.specVersion = specVersion; 168 } 169 170 public String getEisType() 171 { 172 return eisType; 173 } 174 175 public void setEisType(String eisType) 176 { 177 this.eisType = eisType; 178 } 179 180 public String getVersion() 181 { 182 return version; 183 } 184 185 public void setVersion(String version) 186 { 187 this.version = version; 188 } 189 190 public String getManagedConnectionFactoryClass() 191 { 192 return managedConnectionFactoryClass; 193 } 194 195 public void setManagedConnectionFactoryClass(final String managedConnectionFactoryClass) 196 { 197 this.managedConnectionFactoryClass = managedConnectionFactoryClass; 198 } 199 200 public String getConnectionFactoryInterface() 201 { 202 return connectionFactoryInterface; 203 } 204 205 public void setConnectionFactoryInterface(String connectionFactoryInterface) 206 { 207 this.connectionFactoryInterface = connectionFactoryInterface; 208 } 209 210 public String getConnectionFactoryImplClass() 211 { 212 return connectionFactoryImplClass; 213 } 214 215 public void setConnectionFactoryImplClass(String connectionFactoryImplClass) 216 { 217 this.connectionFactoryImplClass = connectionFactoryImplClass; 218 } 219 220 public String getConnectionInterface() 221 { 222 return connectionInterface; 223 } 224 225 public void setConnectionInterface(String connectionInterface) 226 { 227 this.connectionInterface = connectionInterface; 228 } 229 230 public String getConnectionImplClass() 231 { 232 return connectionImplClass; 233 } 234 235 public void setConnectionImplClass(String connectionImplClass) 236 { 237 this.connectionImplClass = connectionImplClass; 238 } 239 240 public String getTransactionSupport() 241 { 242 return transactionSupport; 243 } 244 245 public void setTransactionSupport(String transactionSupport) 246 { 247 this.transactionSupport = transactionSupport; 248 } 249 250 public Element getManagedConnectionFactoryProperties() 251 { 252 return managedConnectionFactoryProperties; 253 } 254 255 public void setManagedConnectionFactoryProperties(Element managedConnectionFactoryProperties) 256 { 257 this.managedConnectionFactoryProperties = managedConnectionFactoryProperties; 258 } 259 260 public String getAuthenticationMechanismType() 261 { 262 return authenticationMechanismType; 263 } 264 265 public void setAuthenticationMechanismType(String authenticationMechanismType) 266 { 267 this.authenticationMechanismType = authenticationMechanismType; 268 } 269 270 public String getCredentialInterface() 271 { 272 return credentialInterface; 273 } 274 275 public void setCredentialInterface(String credentialInterface) 276 { 277 this.credentialInterface = credentialInterface; 278 } 279 280 public boolean isReauthenticationSupport() 281 { 282 return reauthenticationSupport; 283 } 284 285 public void setReauthenticationSupport(boolean reauthenticationSupport) 286 { 287 this.reauthenticationSupport = reauthenticationSupport; 288 } 289 290 public ManagedConnectionFactory getMcfInstance() 291 { 292 return mcf; 293 } 294 295 protected void startService() throws Exception 296 { 297 if (mcf != null) 298 throw new DeploymentException("Stop the RARDeployment before restarting it"); 299 300 ConnectorMetaData cmd = null; 301 ConnectionDefinitionMetaData cdmd = null; 302 ResourceAdapter resourceAdapter = null; 303 if (oldRarDeployment != null) 304 { 305 try 306 { 307 resourceAdapter = (ResourceAdapter ) getServer().getAttribute(oldRarDeployment, "ResourceAdapter"); 308 cmd = (ConnectorMetaData) getServer().getAttribute(oldRarDeployment, "MetaData"); 309 cdmd = cmd.getConnectionDefinition(connectionDefinition); 310 if (cdmd == null) 311 throw new DeploymentException("ConnectionDefinition '" + connectionDefinition + "' not found in rar '" + rarName + "'"); 312 setManagedConnectionFactoryClass(cdmd.getManagedConnectionFactoryClass()); 313 setReauthenticationSupport(cmd.getReauthenticationSupport()); 314 } 315 catch (Exception e) 316 { 317 throw new DeploymentException("couldn't get oldRarDeployment! " + oldRarDeployment, e); 318 } 319 } 320 try 321 { 322 mcfClass = Thread.currentThread().getContextClassLoader().loadClass(managedConnectionFactoryClass); 323 } 324 catch (ClassNotFoundException cnfe) 325 { 326 log.error("Could not find ManagedConnectionFactory class: " + managedConnectionFactoryClass, cnfe); 327 throw new DeploymentException("Could not find ManagedConnectionFactory class: " 328 + managedConnectionFactoryClass); 329 } 330 try 331 { 332 mcf = (ManagedConnectionFactory ) mcfClass.newInstance(); 333 } 334 catch (Exception e) 335 { 336 log.error("Could not instantiate ManagedConnectionFactory: " + managedConnectionFactoryClass, e); 337 throw new DeploymentException("Could not instantiate ManagedConnectionFactory: " 338 + managedConnectionFactoryClass); 339 } 340 if (cmd != null) 341 { 342 setMcfProperties(cmd.getProperties(), false); 344 setMcfProperties(cdmd.getProperties(), true); 346 } 347 setMcfProperties(managedConnectionFactoryProperties); 349 350 if (resourceAdapter != null && mcf instanceof ResourceAdapterAssociation ) 351 { 352 ResourceAdapterAssociation raa = (ResourceAdapterAssociation ) mcf; 353 raa.setResourceAdapter(resourceAdapter); 354 } 355 } 356 357 protected void stopService() 358 { 359 mcf = null; 360 mcfClass = null; 361 } 362 363 public void setManagedConnectionFactoryAttribute(String name, Class clazz, Object value) 364 { 365 setManagedConnectionFactoryAttribute(name, clazz, value, false); 366 } 367 368 protected void setManagedConnectionFactoryAttribute(String name, Class clazz, Object value, boolean mustExist) 369 { 370 if (name == null || name.length() == 0) 371 throw new IllegalArgumentException ("Null or empty attribute name " + name); 372 String setterName = "set" + Character.toUpperCase(name.charAt(0)); 373 if (name.length() > 1) 374 setterName = setterName.concat(name.substring(1)); 375 Method setter; 376 try 377 { 378 setter = mcfClass.getMethod(setterName, new Class [] {clazz}); 379 } 380 catch (NoSuchMethodException nsme) 381 { 382 String error = "The class '" + mcfClass.toString() + "' has no setter for config property '" + name + "'"; 383 if (mustExist) 384 throw new IllegalArgumentException (error); 385 else 386 { 387 log.trace(error, nsme); 388 return; 389 } 390 } 391 try 392 { 393 setter.invoke(mcf, new Object [] {value}); 394 log.debug("set property " + name + " to value " + value); 395 } 396 catch (Exception e) 397 { 398 String error = "Unable to invoke setter method '" + setter + "' " + "on object '" + mcf + "'"; 399 if (e instanceof InvocationTargetException ) 400 throw new NestedRuntimeException(error, ((InvocationTargetException ) e).getCause()); 401 else 402 throw new NestedRuntimeException(error, e); 403 } 404 sendNotification(new Notification (MCF_ATTRIBUTE_CHANGED_NOTIFICATION, getServiceName(), 405 getNextNotificationSequenceNumber())); 406 } 407 408 public Object getManagedConnectionFactoryAttribute(String name) 409 { 410 if (name == null || name.length() == 0) 411 throw new IllegalArgumentException ("Null or empty attribute name " + name); 412 String getterName = "get" + Character.toUpperCase(name.charAt(0)); 413 if (name.length() > 1) 414 getterName = getterName.concat(name.substring(1)); 415 Method getter; 416 try 417 { 418 getter = mcfClass.getMethod(getterName, new Class [] {}); 419 } 420 catch (NoSuchMethodException e) 421 { 422 String msg = "The class '" + mcfClass + "' has no getter(" 423 + getterName + ") for config property '" + name + "'"; 424 log.debug(msg, e); 425 throw new IllegalArgumentException (msg); 426 } 427 try 428 { 429 Object value = getter.invoke(mcf, new Object []{}); 430 log.debug("get property " + name + ": value " + value); 431 return value; 432 } 433 catch (Exception e) 434 { 435 String error = "Unable to invoke getter method '" + getter + "' " + "on object '" + mcf + "'"; 436 log.debug(error, e); 437 if (e instanceof InvocationTargetException ) 438 throw new NestedRuntimeException(error, ((InvocationTargetException ) e).getCause()); 439 else 440 throw new NestedRuntimeException(error, e); 441 } 442 } 443 444 protected void setMcfProperties(Collection properties, boolean mustExist) throws DeploymentException 445 { 446 for (Iterator i = properties.iterator(); i.hasNext();) 447 { 448 ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next(); 449 String name = cpmd.getName(); 450 String type = cpmd.getType(); 451 String value = cpmd.getValue(); 452 if (name == null || name.length() == 0 || value == null || value.length() == 0) 453 { 454 log.debug("Not setting config property '" + name + "'"); 455 continue; 456 } 457 Class clazz = Classes.getPrimitiveTypeForName(type); 459 if (clazz == null) 460 { 461 try 463 { 464 clazz = Thread.currentThread().getContextClassLoader().loadClass(type); 465 } 466 catch (ClassNotFoundException cnfe) 467 { 468 log.warn("Unable to find class '" + type + "' for " + "property '" + name + "' - skipping property."); 469 continue; 470 } 471 } 472 PropertyEditor pe = PropertyEditorManager.findEditor(clazz); 473 if (pe == null) 474 { 475 log.warn("Unable to find a PropertyEditor for class '" + clazz + "' of property '" + name + "' - " 476 + "skipping property"); 477 continue; 478 } 479 log.debug("setting property: " + name + " to value " + value); 480 try 481 { 482 pe.setAsText(value); 483 } 484 catch (IllegalArgumentException iae) 485 { 486 log.warn("Value '" + value + "' is not valid for property '" + name + "' of class '" + clazz 487 + "' - skipping " + "property"); 488 continue; 489 } 490 Object v = pe.getValue(); 491 setManagedConnectionFactoryAttribute(name, clazz, v, mustExist); 492 } 493 } 494 495 protected void setMcfProperties(Element mcfProps) throws DeploymentException 496 { 497 if (mcfProps == null) 498 return; 499 NodeList props = mcfProps.getChildNodes(); 501 for (int i = 0; i < props.getLength(); i++) 502 { 503 if (props.item(i).getNodeType() == Node.ELEMENT_NODE) 504 { 505 Element prop = (Element ) props.item(i); 506 if (prop.getTagName().equals("config-property")) 507 { 508 String name = null; 509 String type = null; 510 String value = null; 511 if (prop.hasAttribute("name")) 514 { 515 name = prop.getAttribute("name"); 516 type = prop.getAttribute("type"); 517 value = MetaData.getElementContent(prop, null, false); 518 } 519 else 520 { 521 name = MetaData.getElementContent(MetaData.getUniqueChild(prop, "config-property-name")); 522 type = MetaData.getElementContent(MetaData.getUniqueChild(prop, "config-property-type")); 523 value = MetaData.getElementContent(MetaData.getOptionalChild(prop, "config-property-value"), null, false); 524 } 525 if (name == null || name.length() == 0 || value == null || value.length() == 0) 526 { 527 log.debug("Not setting config property '" + name + "'"); 528 continue; 529 } 530 if (type == null || type.length() == 0) 531 { 532 type = "java.lang.String"; 534 } 535 Class clazz = Classes.getPrimitiveTypeForName(type); 537 if (clazz == null) 538 { 539 try 541 { 542 clazz = Thread.currentThread().getContextClassLoader().loadClass(type); 543 } 544 catch (ClassNotFoundException cnfe) 545 { 546 log.warn("Unable to find class '" + type + "' for " + "property '" + name 547 + "' - skipping property."); 548 continue; 549 } 550 } 551 PropertyEditor pe = PropertyEditorManager.findEditor(clazz); 552 if (pe == null) 553 { 554 log.warn("Unable to find a PropertyEditor for class '" + clazz + "' of property '" + name + "' - " 555 + "skipping property"); 556 continue; 557 } 558 log.debug("setting property: " + name + " to value " + value); 559 try 560 { 561 pe.setAsText(value); 562 } 563 catch (IllegalArgumentException iae) 564 { 565 log.warn("Value '" + value + "' is not valid for property '" + name + "' of class '" + clazz 566 + "' - skipping " + "property"); 567 continue; 568 } 569 Object v = pe.getValue(); 570 setManagedConnectionFactoryAttribute(name, clazz, v); 571 } 572 } 573 } 574 } 575 576 public Object createConnectionFactory() throws ResourceException 577 { 578 return mcf.createConnectionFactory(); 579 } 580 581 public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException 582 { 583 return mcf.createConnectionFactory(cxManager); 584 } 585 586 public ManagedConnection createManagedConnection(javax.security.auth.Subject subject, 587 ConnectionRequestInfo cxRequestInfo) throws ResourceException 588 { 589 return mcf.createManagedConnection(subject, cxRequestInfo); 590 } 591 592 public boolean equals(Object other) 593 { 594 return mcf.equals(other); 595 } 596 597 public java.io.PrintWriter getLogWriter() throws ResourceException 598 { 599 return mcf.getLogWriter(); 600 } 601 602 public String toString() 603 { 604 StringBuffer buffer = new StringBuffer (); 605 buffer.append(getClass().getName()); 606 buffer.append('@'); 607 buffer.append(Integer.toHexString(System.identityHashCode(this))); 608 return buffer.toString(); 609 } 610 611 public int hashCode() 612 { 613 return mcf.hashCode(); 614 } 615 616 public ManagedConnection matchManagedConnections(java.util.Set connectionSet, javax.security.auth.Subject subject, 617 ConnectionRequestInfo cxRequestInfo) throws ResourceException 618 { 619 return mcf.matchManagedConnections(connectionSet, subject, cxRequestInfo); 620 } 621 622 public void setLogWriter(java.io.PrintWriter out) throws ResourceException 623 { 624 mcf.setLogWriter(out); 625 } 626 } 627 | Popular Tags |