1 22 package org.jboss.ejb; 23 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 33 import javax.management.ObjectName ; 34 import javax.security.jacc.PolicyContext ; 35 import javax.transaction.TransactionManager ; 36 37 import org.jboss.deployment.DeploymentException; 38 import org.jboss.deployment.DeploymentInfo; 39 import org.jboss.deployment.J2eeApplicationMetaData; 40 import org.jboss.deployment.SubDeployerExt; 41 import org.jboss.deployment.SubDeployerSupport; 42 import org.jboss.ejb.plugins.EnterpriseBeanPolicyContextHandler; 43 import org.jboss.logging.Logger; 44 import org.jboss.metadata.ApplicationMetaData; 45 import org.jboss.metadata.MetaData; 46 import org.jboss.metadata.XmlFileLoader; 47 import org.jboss.mx.loading.LoaderRepositoryFactory; 48 import org.jboss.mx.util.MBeanProxyExt; 49 import org.jboss.mx.util.ObjectNameConverter; 50 import org.jboss.system.ServiceControllerMBean; 51 import org.jboss.verifier.BeanVerifier; 52 import org.jboss.verifier.event.VerificationEvent; 53 import org.jboss.verifier.event.VerificationListener; 54 import org.w3c.dom.Element ; 55 56 80 public class EJBDeployer extends SubDeployerSupport 81 implements EJBDeployerMBean 82 { 83 84 private static final String [] DEFAULT_ENHANCED_SUFFIXES = new String [] { 85 "400:.jar" 86 }; 87 88 private ServiceControllerMBean serviceController; 89 90 91 private HashMap deployments = new HashMap (); 92 93 94 private boolean verifyDeployments; 95 96 97 private boolean verifierVerbose; 98 99 101 private boolean strictVerifier; 102 103 104 private boolean metricsEnabled; 105 106 107 private boolean validateDTDs; 108 109 110 private ObjectName webServiceName; 111 112 private ObjectName transactionManagerServiceName; 113 private TransactionManager tm; 114 115 private boolean callByValue; 116 117 118 private SubDeployerExt thisProxy; 119 120 123 public EJBDeployer() 124 { 125 setEnhancedSuffixes(DEFAULT_ENHANCED_SUFFIXES); 126 } 127 128 133 public boolean isCallByValue() 134 { 135 return callByValue; 136 } 137 138 143 public void setCallByValue(boolean callByValue) 144 { 145 this.callByValue = callByValue; 146 } 147 148 153 public Iterator listDeployedApplications() 154 { 155 return deployments.values().iterator(); 156 } 157 158 161 protected void startService() throws Exception 162 { 163 serviceController = (ServiceControllerMBean) 164 MBeanProxyExt.create(ServiceControllerMBean.class, 165 ServiceControllerMBean.OBJECT_NAME, server); 166 tm = (TransactionManager )getServer().getAttribute(transactionManagerServiceName, 167 "TransactionManager"); 168 169 Set keys = PolicyContext.getHandlerKeys(); 172 if (!keys.contains(EnterpriseBeanPolicyContextHandler.EJB_CONTEXT_KEY)) 173 { 174 EnterpriseBeanPolicyContextHandler beanHandler = new EnterpriseBeanPolicyContextHandler(); 175 PolicyContext.registerHandler(EnterpriseBeanPolicyContextHandler.EJB_CONTEXT_KEY, 176 beanHandler, false); 177 } 178 if (!keys.contains(BeanMetaDataPolicyContextHandler.METADATA_CONTEXT_KEY)) 179 { 180 BeanMetaDataPolicyContextHandler metadataHandler = new BeanMetaDataPolicyContextHandler(); 181 PolicyContext.registerHandler(BeanMetaDataPolicyContextHandler.METADATA_CONTEXT_KEY, 182 metadataHandler, false); 183 } 184 if (!keys.contains(EJBArgsPolicyContextHandler.EJB_ARGS_KEY)) 185 { 186 EJBArgsPolicyContextHandler argsHandler = new EJBArgsPolicyContextHandler(); 187 PolicyContext.registerHandler(EJBArgsPolicyContextHandler.EJB_ARGS_KEY, 188 argsHandler, false); 189 } 190 if (!keys.contains(SOAPMsgPolicyContextHandler.SEI_ARGS_KEY)) 191 { 192 SOAPMsgPolicyContextHandler msgHandler = new SOAPMsgPolicyContextHandler(); 193 PolicyContext.registerHandler(SOAPMsgPolicyContextHandler.SEI_ARGS_KEY, 194 msgHandler, false); 195 } 196 197 thisProxy = (SubDeployerExt) 200 MBeanProxyExt.create(SubDeployerExt.class, super.getServiceName(), super.getServer()); 201 202 mainDeployer.addDeployer(thisProxy); 204 } 205 206 210 protected void stopService() throws Exception 211 { 212 213 for( Iterator modules = deployments.values().iterator(); 214 modules.hasNext(); ) 215 { 216 DeploymentInfo di = (DeploymentInfo) modules.next(); 217 stop(di); 218 } 219 220 for( Iterator modules = new ArrayList (deployments.values()).iterator(); 222 modules.hasNext(); ) 223 { 224 DeploymentInfo di = (DeploymentInfo) modules.next(); 225 destroy(di); 226 } 227 deployments.clear(); 228 229 mainDeployer.removeDeployer(thisProxy); 231 232 serviceController = null; 233 tm = null; 234 } 235 236 243 public void setVerifyDeployments( boolean verify ) 244 { 245 verifyDeployments = verify; 246 } 247 248 255 public boolean getVerifyDeployments() 256 { 257 return verifyDeployments; 258 } 259 260 267 public void setVerifierVerbose(boolean verbose) 268 { 269 verifierVerbose = verbose; 270 } 271 272 279 public boolean getVerifierVerbose() 280 { 281 return verifierVerbose; 282 } 283 284 292 public void setStrictVerifier( boolean strictVerifier ) 293 { 294 this.strictVerifier = strictVerifier; 295 } 296 297 305 public boolean getStrictVerifier() 306 { 307 return strictVerifier; 308 } 309 310 311 318 public void setMetricsEnabled(boolean enable) 319 { 320 metricsEnabled = enable; 321 } 322 323 330 public boolean isMetricsEnabled() 331 { 332 return metricsEnabled; 333 } 334 335 342 public boolean getValidateDTDs() 343 { 344 return validateDTDs; 345 } 346 347 354 public void setValidateDTDs(boolean validate) 355 { 356 this.validateDTDs = validate; 357 } 358 359 360 366 public ObjectName getWebServiceName() 367 { 368 return webServiceName; 369 } 370 371 377 public void setWebServiceName(ObjectName webServiceName) 378 { 379 this.webServiceName = webServiceName; 380 } 381 382 383 389 public ObjectName getTransactionManagerServiceName() 390 { 391 return transactionManagerServiceName; 392 } 393 394 400 public void setTransactionManagerServiceName(ObjectName transactionManagerServiceName) 401 { 402 this.transactionManagerServiceName = transactionManagerServiceName; 403 } 404 405 public boolean accepts(DeploymentInfo di) 406 { 407 if (super.accepts(di) == false) 409 { 410 return false; 411 } 412 boolean accepts = false; 414 try 415 { 416 URL dd = di.localCl.findResource("META-INF/ejb-jar.xml"); 417 if (dd == null) 418 { 419 return false; 420 } 421 String urlStr = di.url.getFile(); 422 423 if( di.localUrl != null ) 427 { 428 urlStr = di.localUrl.toString(); 429 } 430 431 String ddStr = dd.toString(); 432 if ( ddStr.indexOf(urlStr) >= 0 ) 433 { 434 accepts = true; 435 } 436 } 437 catch( Exception ignore ) 438 { 439 } 440 441 return accepts; 442 } 443 444 public void init(DeploymentInfo di) 445 throws DeploymentException 446 { 447 log.debug("init, "+di.shortName); 448 try 449 { 450 if( di.url.getProtocol().equalsIgnoreCase("file") ) 451 { 452 File file = new File (di.url.getFile()); 453 454 if( !file.isDirectory() ) 455 { 456 di.watch = di.url; 458 } 459 else 460 { 461 di.watch = new URL (di.url, "META-INF/ejb-jar.xml"); 463 } 464 } 465 else 466 { 467 di.watch = di.url; 469 } 470 471 XmlFileLoader xfl = new XmlFileLoader(); 473 InputStream in = di.localCl.getResourceAsStream("META-INF/jboss.xml"); 474 if( in != null ) 475 { 476 try 477 { 478 Element jboss = xfl.getDocument(in, "META-INF/jboss.xml").getDocumentElement(); 479 Element loader = MetaData.getOptionalChild(jboss, "loader-repository"); 481 if( loader != null ) 482 { 483 LoaderRepositoryFactory.LoaderRepositoryConfig config = 484 LoaderRepositoryFactory.parseRepositoryConfig(loader); 485 di.setRepositoryInfo(config); 486 } 487 } 488 finally 489 { 490 in.close(); 491 } 492 } 493 } 494 catch (Exception e) 495 { 496 if (e instanceof DeploymentException) 497 throw (DeploymentException)e; 498 throw new DeploymentException( "failed to initialize", e ); 499 } 500 501 super.init(di); 503 } 504 505 511 protected void processNestedDeployments(DeploymentInfo di) 512 throws DeploymentException 513 { 514 super.processNestedDeployments(di); 515 } 516 517 public synchronized void create(DeploymentInfo di) 518 throws DeploymentException 519 { 520 log.debug("create, "+di.shortName); 521 ApplicationMetaData ejbMetaData = null; 522 try 523 { 524 URL loaderURL = (di.localUrl != null ? di.localUrl : di.url); 526 di.annotationsCl = new URLClassLoader (new URL [] { loaderURL }, di.ucl); 527 528 XmlFileLoader efm = new XmlFileLoader(validateDTDs); 530 efm.setClassLoader(di.localCl); 531 532 URL alternativeDD = null; 534 if (di.alternativeDD != null) 535 { 536 String contentsDir = new File (di.url.getPath()).getParent(); 537 alternativeDD = new URL ("file:/" + contentsDir + "/" + di.alternativeDD); 538 } 539 540 di.metaData = ejbMetaData = efm.load(alternativeDD); 542 543 if (di.parent != null && di.parent.metaData instanceof J2eeApplicationMetaData) 545 { 546 J2eeApplicationMetaData appMetaData = (J2eeApplicationMetaData)di.parent.metaData; 547 548 if (ejbMetaData.getSecurityDomain() == null) 549 ejbMetaData.setSecurityDomain(appMetaData.getSecurityDomain()); 550 551 if (ejbMetaData.getUnauthenticatedPrincipal() == null) 552 ejbMetaData.setUnauthenticatedPrincipal(appMetaData.getUnauthenticatedPrincipal()); 553 554 ejbMetaData.getAssemblyDescriptor().mergeSecurityRoles(appMetaData.getSecurityRoles()); 555 } 556 } 557 catch (Exception e) 558 { 559 if (e instanceof DeploymentException) 560 throw (DeploymentException)e; 561 throw new DeploymentException( "Failed to load metaData", e ); 562 } 563 564 if( verifyDeployments ) 565 { 566 boolean allOK = true; 568 569 try 572 { 573 BeanVerifier verifier = new BeanVerifier(); 574 575 verifier.addVerificationListener(new VerificationListener() 577 { 578 Logger verifierLog = Logger.getLogger(EJBDeployer.class, 579 "verifier" ); 580 581 public void beanChecked(VerificationEvent event) 582 { 583 verifierLog.debug( "Bean checked: " + event.getMessage() ); 584 } 585 586 public void specViolation(VerificationEvent event) 587 { 588 verifierLog.warn( "EJB spec violation: " + 589 (verifierVerbose ? event.getVerbose() : event.getMessage())); 590 } 591 }); 592 593 log.debug("Verifying " + di.url); 594 verifier.verify( di.url, (ApplicationMetaData) di.metaData, 595 di.ucl ); 596 597 allOK = verifier.getSuccess(); 598 } 599 catch (Throwable t) 600 { 601 log.warn("Verify failed; continuing", t ); 602 allOK = false; 603 } 604 605 if( strictVerifier && !allOK ) 609 { 610 throw new DeploymentException( "Verification of Enterprise " + 611 "Beans failed, see above for error messages." ); 612 } 613 614 } 615 616 try 618 { 619 EjbModule ejbModule = new EjbModule(di, tm, webServiceName); 620 String name = ejbMetaData.getJmxName(); 621 if( name == null ) 622 { 623 name = EjbModule.BASE_EJB_MODULE_NAME + ",module=" + di.shortName; 624 } 625 ObjectName ejbModuleName = ObjectNameConverter.convert(name); 627 if( server.isRegistered(ejbModuleName) == true ) 629 { 630 log.debug("The EJBModule name: "+ejbModuleName 631 +"is already registered, adding uid="+System.identityHashCode(ejbModule)); 632 name = name + ",uid="+System.identityHashCode(ejbModule); 633 ejbModuleName = ObjectNameConverter.convert(name); 634 } 635 636 server.registerMBean(ejbModule, ejbModuleName); 637 di.deployedObject = ejbModuleName; 638 639 log.debug( "Deploying: " + di.url ); 640 serviceController.create(di.deployedObject); 642 } 643 catch (Exception e) 644 { 645 throw new DeploymentException("Error during create of EjbModule: " 646 + di.url, e); 647 } 648 super.create(di); 649 } 650 651 public synchronized void start(DeploymentInfo di) 652 throws DeploymentException 653 { 654 try 655 { 656 log.debug( "start application, deploymentInfo: " + di + 658 ", short name: " + di.shortName + 659 ", parent short name: " + 660 (di.parent == null ? "null" : di.parent.shortName) ); 661 662 serviceController.start(di.deployedObject); 663 664 log.info( "Deployed: " + di.url ); 665 666 deployments.put(di.url, di); 669 } 670 catch (Exception e) 671 { 672 stop(di); 673 destroy(di); 674 675 throw new DeploymentException( "Could not deploy " + di.url, e ); 676 } 677 super.start(di); 678 } 679 680 public void stop(DeploymentInfo di) 681 throws DeploymentException 682 { 683 log.info( "Undeploying: " + di.url ); 684 685 try 686 { 687 if (di.deployedObject != null) 688 serviceController.stop(di.deployedObject); 689 } 690 catch (Exception e) 691 { 692 throw new DeploymentException( "problem stopping ejb module: " + 693 di.url, e ); 694 } 695 super.stop(di); 696 } 697 698 public void destroy(DeploymentInfo di) 699 throws DeploymentException 700 { 701 deployments.remove(di.url); 703 704 try 705 { 706 if (di.deployedObject != null) 707 { 708 serviceController.destroy( di.deployedObject ); 709 serviceController.remove( di.deployedObject ); 710 } 711 } 712 catch (Exception e) 713 { 714 throw new DeploymentException( "problem destroying ejb module: " + 715 di.url, e ); 716 } 717 super.destroy(di); 718 } 719 } 720 | Popular Tags |