1 22 package org.jboss.deployment; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.FileInputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.security.Policy ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.ArrayList ; 35 import java.util.jar.JarEntry ; 36 import java.util.jar.JarFile ; 37 import java.util.jar.Manifest ; 38 import java.util.jar.Attributes ; 39 import java.util.jar.JarInputStream ; 40 41 import javax.management.ObjectName ; 42 import javax.security.jacc.PolicyConfiguration ; 43 import javax.security.jacc.PolicyConfigurationFactory ; 44 45 import org.jboss.metadata.MetaData; 46 import org.jboss.metadata.XmlFileLoader; 47 import org.jboss.mx.loading.LoaderRepositoryFactory; 48 import org.jboss.mx.loading.LoaderRepositoryFactory.LoaderRepositoryConfig; 49 import org.jboss.mx.util.MBeanProxyExt; 50 import org.jboss.system.ServiceControllerMBean; 51 import org.jboss.util.file.JarUtils; 52 import org.w3c.dom.Element ; 53 54 61 public class EARDeployer extends SubDeployerSupport 62 implements EARDeployerMBean 63 { 64 65 private static final String [] DEFAULT_ENHANCED_SUFFIXES = new String [] { 66 "650:.ear" 67 }; 68 69 private ServiceControllerMBean serviceController; 70 71 private boolean isolated = false; 72 73 private boolean callByValue = false; 74 75 78 public EARDeployer() 79 { 80 setEnhancedSuffixes(DEFAULT_ENHANCED_SUFFIXES); 81 } 82 83 86 public boolean isIsolated() 87 { 88 return isolated; 89 } 90 91 94 public void setIsolated(boolean isolated) 95 { 96 this.isolated = isolated; 97 } 98 99 102 public boolean isCallByValue() 103 { 104 return callByValue; 105 } 106 107 110 public void setCallByValue(boolean callByValue) 111 { 112 this.callByValue = callByValue; 113 } 114 115 protected void startService() throws Exception 116 { 117 serviceController = (ServiceControllerMBean) 118 MBeanProxyExt.create(ServiceControllerMBean.class, 119 ServiceControllerMBean.OBJECT_NAME, server); 120 super.startService(); 121 } 122 123 public void init(DeploymentInfo di) throws DeploymentException 124 { 125 try 126 { 127 log.info("Init J2EE application: " + di.url); 128 InputStream in = di.localCl.getResourceAsStream("META-INF/application.xml"); 129 boolean hasAppXml = in != null; 130 J2eeApplicationMetaData metaData = new J2eeApplicationMetaData(); 131 if( hasAppXml ) 132 { 133 136 XmlFileLoader xfl = new XmlFileLoader(false); 137 Element application = xfl.getDocument(in, "META-INF/application.xml").getDocumentElement(); 138 metaData.importXml(application); 139 in.close(); 140 } 141 else 142 { 143 scanEar(metaData, di); 145 } 146 di.metaData = metaData; 147 148 if( metaData.getLibraryDirectory() != null ) 150 { 151 addLibraryJars(di, metaData.getLibraryDirectory()); 152 } 153 154 Element loader = null; 156 in = di.localCl.getResourceAsStream("META-INF/jboss-app.xml"); 157 if( in != null ) 158 { 159 XmlFileLoader xfl = new XmlFileLoader(true); 161 Element jbossApp = xfl.getDocument(in, "META-INF/jboss-app.xml").getDocumentElement(); 162 in.close(); 163 metaData.importXml(jbossApp); 165 loader = MetaData.getOptionalChild(jbossApp, "loader-repository"); 167 } 168 initLoaderRepository(di, loader); 169 170 if (di.url.getProtocol().equals("file")) 172 { 173 File file = new File (di.url.getFile()); 174 175 if (!file.isDirectory()) 177 { 178 di.watch = di.url; 179 } 180 else 182 { 183 di.watch = new URL (di.url, "META-INF/application.xml"); 184 } 185 } 186 else 187 { 188 di.watch = di.url; 190 } 191 192 File parentDir = null; 194 HashMap extractedJars = new HashMap (); 195 196 if (di.isDirectory) 197 { 198 parentDir = new File (di.localUrl.getFile()); 199 } 200 else 201 { 202 206 String urlPrefix = "jar:" + di.localUrl + "!/"; 207 JarFile jarFile = new JarFile (di.localUrl.getFile()); 208 for (Enumeration e = jarFile.entries(); e.hasMoreElements();) 211 { 212 JarEntry entry = (JarEntry )e.nextElement(); 213 String name = entry.getName(); 214 try 215 { 216 URL url = new URL (urlPrefix + name); 217 if (metaData.hasModule(name)) 218 { 219 URL nestedURL = JarUtils.extractNestedJar(url, this.tempDeployDir); 221 extractedJars.put(name, nestedURL); 223 log.debug("Extracted deployable content: "+name); 224 } 225 else if( entry.isDirectory() == false ) 226 { 227 JarUtils.extractNestedJar(url, this.tempDeployDir); 228 log.debug("Extracted non-deployable content: "+name); 229 } 230 } 231 catch (MalformedURLException mue) 232 { 233 log.warn("Jar entry invalid. Ignoring: " + name, mue); 234 } 235 catch (IOException ex) 236 { 237 log.warn("Failed to extract nested jar. Ignoring: " + name, ex); 238 } 239 } 240 } 241 242 String contextID = di.shortName; 244 PolicyConfigurationFactory pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 245 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(contextID, true); 246 di.context.put("javax.security.jacc.PolicyConfiguration", pc); 247 248 for (Iterator iter = metaData.getModules(); iter.hasNext(); ) 250 { 251 J2eeModuleMetaData mod = (J2eeModuleMetaData)iter.next(); 252 String fileName = mod.getFileName(); 253 if (fileName != null && (fileName = fileName.trim()).length() > 0) 254 { 255 DeploymentInfo sub = null; 256 if (di.isDirectory) 257 { 258 File f = new File (parentDir, fileName); 259 sub = new DeploymentInfo(f.toURL(), di, getServer()); 260 } 261 else 262 { 263 URL nestedURL = (URL ) extractedJars.get(fileName); 265 if( nestedURL == null ) 266 throw new DeploymentException("Failed to find module file: "+fileName); 267 sub = new DeploymentInfo(nestedURL, di, getServer()); 268 } 269 270 if( mod.isWeb() ) 272 sub.webContext = mod.getWebContext(); 273 274 if (mod.alternativeDD != null) 276 sub.alternativeDD = mod.alternativeDD; 277 278 log.debug("Deployment Info: " + sub + ", isDirectory: " + sub.isDirectory); 279 } 280 } 281 } 282 catch (Exception e) 283 { 284 DeploymentException.rethrowAsDeploymentException("Error in accessing application metadata: ", e); 285 } 286 di.sortedSubDeployments=true; 287 super.init(di); 288 } 289 290 public void create(DeploymentInfo di) throws DeploymentException 291 { 292 super.create(di); 293 294 try 296 { 297 EARDeployment earDeployment = new EARDeployment(di); 298 String name = earDeployment.getJMXName(); 299 ObjectName objectName = new ObjectName (name); 300 di.deployedObject = objectName; 301 server.registerMBean(earDeployment, objectName); 302 serviceController.create(di.deployedObject); 303 } 304 catch (Exception e) 305 { 306 DeploymentException.rethrowAsDeploymentException("Error during create of EARDeployment: " + di.url, e); 307 } 308 } 309 310 public void start(DeploymentInfo di) 311 throws DeploymentException 312 { 313 super.start (di); 314 try 315 { 316 PolicyConfiguration pc = (PolicyConfiguration ) 318 di.context.get("javax.security.jacc.PolicyConfiguration"); 319 pc.commit(); 320 Policy.getPolicy().refresh(); 321 serviceController.start(di.deployedObject); 322 } 323 catch (Exception e) 324 { 325 DeploymentException.rethrowAsDeploymentException("Error during start of EARDeployment: " + di.url, e); 326 } 327 log.info ("Started J2EE application: " + di.url); 328 } 329 330 public void stop(DeploymentInfo di) throws DeploymentException 331 { 332 try 333 { 334 if (di.deployedObject != null) 335 serviceController.stop(di.deployedObject); 336 } 337 catch (Exception e) 338 { 339 DeploymentException.rethrowAsDeploymentException("Error during stop of EARDeployment: " + di.url, e); 340 } 341 super.stop(di); 342 } 343 344 350 public void destroy(DeploymentInfo di) throws DeploymentException 351 { 352 log.info("Undeploying J2EE application, destroy step: " + di.url); 353 try 354 { 355 if (di.deployedObject != null) 356 { 357 serviceController.destroy(di.deployedObject); 358 serviceController.remove(di.deployedObject); 359 } 360 } 361 catch (Exception e) 362 { 363 DeploymentException.rethrowAsDeploymentException("Error during destroy of EARDeployment: " + di.url, e); 364 } 365 super.destroy(di); 366 log.info("Undeployed J2EE application: " + di.url); 367 } 368 369 375 protected void initLoaderRepository(DeploymentInfo di, Element loader) 376 throws Exception 377 { 378 if (loader == null) 379 { 380 if (isolated && di.parent == null) 381 { 382 J2eeApplicationMetaData metaData = (J2eeApplicationMetaData) di.metaData; 383 String name = EARDeployment.getJMXName(metaData, di) + ",extension=LoaderRepository"; 384 ObjectName objectName = new ObjectName (name); 385 386 LoaderRepositoryConfig config = new LoaderRepositoryFactory.LoaderRepositoryConfig(); 387 config.repositoryName = objectName; 388 di.setRepositoryInfo(config); 389 } 390 return; 391 } 392 393 LoaderRepositoryConfig config = LoaderRepositoryFactory.parseRepositoryConfig(loader); 394 di.setRepositoryInfo(config); 395 } 396 397 400 protected boolean isDeployable(String name, URL url) 401 { 402 return super.isDeployable(name, url) || 405 name.endsWith("-ds.xml") || 406 name.endsWith("-service.xml") || 407 name.endsWith(".har"); 408 } 409 410 416 protected void processNestedDeployments(DeploymentInfo di) 417 { 418 } 419 420 443 private void scanEar(J2eeApplicationMetaData metaData, DeploymentInfo di) 444 throws IOException 445 { 446 if (di.isDirectory) 447 { 448 File earDir = new File (di.localUrl.getFile()); 449 String [] content = earDir.list(); 450 int length = content != null ? content.length : 0; 451 for(int n = 0; n < length; n ++) 452 { 453 String module = content[n]; 454 if( module.endsWith(".war") ) 455 { 456 J2eeModuleMetaData war = new J2eeModuleMetaData(J2eeModuleMetaData.WEB, module); 457 metaData.addModule(war); 458 } 459 else if( module.endsWith(".rar") ) 460 { 461 J2eeModuleMetaData war = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 462 metaData.addModule(war); 463 } 464 else if( module.endsWith(".jar") ) 465 { 466 File mfFile = new File (earDir, module+"/META-INF/MANIFEST.MF"); 467 File clientXml = new File (earDir, module+"/META-INF/application-client.xml"); 468 File ejbXml = new File (earDir, module+"/META-INF/ejb-jar.xml"); 469 if( clientXml.exists() ) 470 { 471 J2eeModuleMetaData car = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 472 metaData.addModule(car); 473 } 474 else if( mfFile.exists() ) 475 { 476 FileInputStream fis = new FileInputStream (mfFile); 477 Manifest mf = new Manifest (fis); 478 fis.close(); 479 Attributes attrs = mf.getMainAttributes(); 480 if( attrs.containsKey(Attributes.Name.MAIN_CLASS) ) 481 { 482 J2eeModuleMetaData car = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 483 metaData.addModule(car); 484 } 485 } 486 else if( ejbXml.exists() ) 487 { 488 J2eeModuleMetaData ejb = new J2eeModuleMetaData(J2eeModuleMetaData.EJB, module); 489 metaData.addModule(ejb); 490 } 491 else 492 { 493 } 495 } 496 } 497 } 498 else 499 { 500 JarFile earFile = new JarFile (di.localUrl.getFile()); 501 for (Enumeration e = earFile.entries(); e.hasMoreElements();) 502 { 503 JarEntry entry = (JarEntry )e.nextElement(); 504 String module = entry.getName(); 505 if( module.endsWith(".war") ) 506 { 507 J2eeModuleMetaData war = new J2eeModuleMetaData(J2eeModuleMetaData.WEB, module); 508 metaData.addModule(war); 509 } 510 else if( module.endsWith(".rar") ) 511 { 512 J2eeModuleMetaData war = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 513 metaData.addModule(war); 514 } 515 else if( module.endsWith(".jar") ) 516 { 517 InputStream is = earFile.getInputStream(entry); 518 JarInputStream jis = new JarInputStream (is); 519 Manifest mf = jis.getManifest(); 520 if( mf != null ) 521 { 522 Attributes attrs = mf.getMainAttributes(); 523 if( attrs.containsKey(Attributes.Name.MAIN_CLASS) ) 524 { 525 J2eeModuleMetaData car = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 526 metaData.addModule(car); 527 jis.close(); 528 continue; 529 } 530 } 531 JarEntry jarEntry = jis.getNextJarEntry(); 532 while( jarEntry != null ) 533 { 534 String name = jarEntry.getName(); 535 if( name.equals("META-INF/application-client.xml") ) 536 { 537 J2eeModuleMetaData car = new J2eeModuleMetaData(J2eeModuleMetaData.CONNECTOR, module); 538 metaData.addModule(car); 539 } 540 else if( name.equals("META-INF/ejb-jar.xml") ) 541 { 542 J2eeModuleMetaData ejb = new J2eeModuleMetaData(J2eeModuleMetaData.EJB, module); 543 metaData.addModule(ejb); 544 } 545 jarEntry = jis.getNextJarEntry(); 546 } 547 jis.close(); 548 } 550 } 551 } 552 } 553 554 560 private void addLibraryJars(DeploymentInfo di, String lib) 561 throws IOException 562 { 563 if (di.isDirectory) 564 { 565 File earDir = new File (di.localUrl.getFile(), lib); 566 String [] content = earDir.list(); 567 int length = content != null ? content.length : 0; 568 for(int n = 0; n < length; n ++) 569 { 570 String path = "lib/" + content[n]; 571 URL jarURL = new URL (di.localUrl, path); 572 di.addLibraryJar(jarURL); 573 } 574 } 575 else 576 { 577 JarFile earFile = new JarFile (di.localUrl.getFile()); 578 for (Enumeration e = earFile.entries(); e.hasMoreElements();) 579 { 580 JarEntry entry = (JarEntry )e.nextElement(); 581 String path = "lib/" + entry.getName(); 582 URL jarURL = new URL (di.localUrl, path); 583 di.addLibraryJar(jarURL); 584 } 585 } 586 } 587 588 } 589 | Popular Tags |