1 22 package org.jboss.web.deployers; 23 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.management.MBeanServer ; 35 import javax.management.ObjectName ; 36 37 import org.jboss.deployers.plugins.deployers.helpers.AbstractSimpleRealDeployer; 38 import org.jboss.deployers.plugins.deployers.helpers.AttachmentLocator; 39 import org.jboss.deployers.spi.DeploymentException; 40 import org.jboss.deployers.spi.deployer.DeploymentUnit; 41 import org.jboss.deployers.spi.structure.DeploymentContext; 42 import org.jboss.deployment.J2eeApplicationMetaData; 43 import org.jboss.deployment.J2eeModuleMetaData; 44 import org.jboss.metadata.WebMetaData; 45 import org.jboss.mx.util.MBeanServerLocator; 46 import org.jboss.system.metadata.ServiceConstructorMetaData; 47 import org.jboss.system.metadata.ServiceDependencyMetaData; 48 import org.jboss.system.metadata.ServiceMetaData; 49 import org.jboss.system.server.ServerConfig; 50 import org.jboss.system.server.ServerConfigLocator; 51 import org.jboss.util.file.Files; 52 import org.jboss.util.file.JarUtils; 53 import org.jboss.virtual.VirtualFile; 54 import org.jboss.web.WebApplication; 55 56 78 public abstract class AbstractWarDeployer extends AbstractSimpleRealDeployer<WebMetaData> 79 { 80 public static final String DEPLOYER = "org.jboss.web.AbstractWebContainer.deployer"; 81 public static final String WEB_APP = "org.jboss.web.AbstractWebContainer.webApp"; 82 public static final String WEB_MODULE = "org.jboss.web.AbstractWebContainer.webModule"; 83 public static final String ERROR = "org.jboss.web.AbstractWebContainer.error"; 84 85 86 protected HashMap deploymentMap = new HashMap (); 87 88 protected boolean java2ClassLoadingCompliance = false; 89 90 protected boolean unpackWars = true; 91 93 protected boolean acceptNonWarDirs = false; 94 95 96 protected boolean lenientEjbLink = false; 97 98 99 protected String defaultSecurityDomain = "java:/jaas/other"; 100 101 private String subjectAttributeName = null; 102 103 private MBeanServer server; 104 105 108 public AbstractWarDeployer() 109 { 110 super(WebMetaData.class); 111 setRelativeOrder(COMPONENT_DEPLOYER+1); 112 } 113 114 119 public boolean getJava2ClassLoadingCompliance() 120 { 121 return java2ClassLoadingCompliance; 122 } 123 124 129 public void setJava2ClassLoadingCompliance(boolean flag) 130 { 131 java2ClassLoadingCompliance = flag; 132 } 133 134 141 public boolean getUnpackWars() 142 { 143 return unpackWars; 144 } 145 146 153 public void setUnpackWars(boolean flag) 154 { 155 this.unpackWars = flag; 156 } 157 158 164 public boolean getAcceptNonWarDirs() 165 { 166 return acceptNonWarDirs; 167 } 168 174 public void setAcceptNonWarDirs(boolean flag) 175 { 176 this.acceptNonWarDirs = flag; 177 } 178 179 186 public boolean getLenientEjbLink() 187 { 188 return lenientEjbLink; 189 } 190 191 197 public void setLenientEjbLink(boolean flag) 198 { 199 lenientEjbLink = flag; 200 } 201 202 208 public String getDefaultSecurityDomain() 209 { 210 return defaultSecurityDomain; 211 } 212 219 public void setDefaultSecurityDomain(String defaultSecurityDomain) 220 { 221 this.defaultSecurityDomain = defaultSecurityDomain; 222 } 223 224 227 public String getSubjectAttributeName() 228 { 229 return subjectAttributeName; 230 } 231 234 public void setSubjectAttributeName(String subjectAttributeName) 235 { 236 this.subjectAttributeName = subjectAttributeName; 237 } 238 239 public void start() throws Exception 240 { 241 this.server = MBeanServerLocator.locateJBoss(); 243 } 244 public void stop() throws Exception 245 { 246 247 } 248 249 260 public abstract AbstractWarDeployment getDeployment(DeploymentUnit unit, WebMetaData metaData) 261 throws Exception ; 262 263 278 @Override 279 public void deploy(DeploymentUnit unit, WebMetaData metaData) throws DeploymentException 280 { 281 log.debug("Begin deploy, " + metaData); 282 283 try 284 { 285 292 297 String warName = unit.getName(); 298 URL expWarUrl = unit.getDeploymentContext().getRoot().toURL(); 299 if (warName.endsWith("/") == false 300 || warName.endsWith("!/") == true) 302 { 303 if (warName.startsWith("jar:")) 304 { 305 if (warName.endsWith("!/")) 306 warName = warName.substring(4, warName.length() - 2); 307 else 308 warName = warName.substring(4, warName.length()); 309 } 310 URL warURL = new URL (warName); 311 ServerConfig config = ServerConfigLocator.locate(); 312 String prefix = warURL.getFile(); 313 int lastSlash = prefix.lastIndexOf('/'); 314 if (lastSlash > 0) 315 prefix = prefix.substring(lastSlash + 1); 316 int dotWar = prefix.lastIndexOf(".war"); 317 if (dotWar > 0) 318 prefix = prefix.substring(0, dotWar); 319 320 File expWarFile = File.createTempFile(prefix, "-exp.war", config.getServerTempDeployDir()); 321 expWarFile.delete(); 322 if (expWarFile.mkdir() == false) 323 throw new DeploymentException("Was unable to mkdir: " + expWarFile); 324 log.debug("Unpacking war to: " + expWarFile); 325 VirtualFile root = unit.getDeploymentContext().getRoot(); 326 InputStream is = root.openStream(); 327 JarUtils.unjar(is, expWarFile); 328 is.close(); 329 expWarUrl = expWarFile.toURL(); 330 331 DeploymentContext ctx = unit.getDeploymentContext(); 333 VirtualFile warVF = ctx.getRoot(); 334 String warPathName = warVF.getPathName(); 335 if( warPathName.endsWith("/") == false ) 336 warPathName += "/"; 337 List <VirtualFile> classpathVFs = ctx.getClassPath(); 338 if( classpathVFs != null ) 339 { 340 ArrayList <URL > classpath = new ArrayList <URL >(); 341 for(VirtualFile vf : classpathVFs) 342 { 343 try 344 { 345 String path = vf.getPathName(); 346 path = path.substring(warPathName.length()); 347 URL pathURL = new URL (expWarUrl, path); 348 classpath.add(pathURL); 349 } 350 catch(Exception e) 351 { 352 log.debug("Ignoring path element: "+vf, e); 353 } 354 } 355 unit.addAttachment("org.jboss.web.expandedWarClasspath", classpath); 356 } 357 358 unit.addAttachment("org.jboss.web.expandedWarURL", expWarUrl, URL .class); 360 } 361 362 String altDDPath = metaData.getAltDDPath(); 364 if( altDDPath != null ) 365 { 366 VirtualFile altDD = unit.getMetaDataFile(altDDPath); 368 if( altDD == null ) 369 { 370 File file = new File (altDDPath); 372 if (!file.exists() || !file.isAbsolute()) 373 { 374 DeploymentUnit topUnit = unit.getDeploymentContext().getTopLevel().getDeploymentUnit(); 376 if( topUnit == unit ) 377 throw new DeploymentException("Unable to resolve "+altDDPath+" as WEB-INF path"); 378 altDD = topUnit.getFile(altDDPath); 379 if( altDD == null ) 380 throw new DeploymentException("Unable to resolve "+altDDPath+" as a deployment path"); 381 File webInf = new File (expWarUrl.toURI()); 382 File altDDFile = new File (webInf, "WEB-INF/"+altDD.getName()); 383 log.debug("Copying the altDD to: "+altDDFile); 384 Files.copy(altDD.toURL(), altDDFile); 385 metaData.setAltDDPath(altDDFile.getAbsolutePath()); 386 } 387 } 388 } 389 390 metaData.setJava2ClassLoadingCompliance(this.java2ClassLoadingCompliance); 392 393 String webContext = metaData.getContextRoot(); 395 webContext = buildWebContext(webContext, warName, metaData, unit); 396 metaData.setContextRoot(webContext); 397 398 AbstractWarDeployment deployment = getDeployment(unit, metaData); 399 deployWebModule(unit, metaData, deployment); 400 } 401 catch (Exception e) 402 { 403 throw new DeploymentException("Failed to create web module", e); 404 } 405 } 406 407 410 @Override 411 public void undeploy(DeploymentUnit unit, WebMetaData metaData) 412 { 413 try 414 { 415 URL warURL = unit.getAttachment("org.jboss.web.expandedWarURL", URL .class); 417 if( warURL != null ) 418 { 419 File war = new File (warURL.toURI()); 420 Files.delete(war); 421 } 422 } 423 catch(Exception e) 424 { 425 log.debug("Failed to remove expanded war", e); 426 } 427 } 428 429 public void addDeployedApp(String warURL, WebApplication webApp) 430 { 431 deploymentMap.put(warURL, webApp); 432 } 433 438 public WebApplication getDeployedApp(String warUrl) 439 { 440 WebApplication appInfo = (WebApplication) deploymentMap.get(warUrl); 441 return appInfo; 442 } 443 public WebApplication removeDeployedApp(String warURL) 444 { 445 WebApplication appInfo = (WebApplication) deploymentMap.remove(warURL); 446 return appInfo; 447 } 448 449 453 public Iterator getDeployedApplications() 454 { 455 return deploymentMap.values().iterator(); 456 } 457 458 462 public static URL [] getClassLoaderURLs(ClassLoader cl) 463 { 464 URL [] urls = {}; 465 try 466 { 467 Class returnType = urls.getClass(); 468 Class [] parameterTypes = {}; 469 Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes); 470 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 471 { 472 Object [] args = {}; 473 urls = (URL []) getURLs.invoke(cl, args); 474 } 475 if( urls == null || urls.length == 0 ) 476 { 477 getURLs = cl.getClass().getMethod("getAllURLs", parameterTypes); 478 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 479 { 480 Object [] args = {}; 481 urls = (URL []) getURLs.invoke(cl, args); 482 } 483 } 484 } 485 catch(Exception ignore) 486 { 487 } 488 return urls; 489 } 490 491 500 protected String buildWebContext(String ctxPath, String warName, 501 WebMetaData metaData, DeploymentUnit unit) 502 { 503 String webContext = ctxPath; 505 506 if( webContext == null ) 508 { 509 webContext = warName; 511 webContext = webContext.replace('\\', '/'); 512 if( webContext.endsWith("/") ) 513 webContext = webContext.substring(0, webContext.length()-1); 514 int prefix = webContext.lastIndexOf('/'); 515 if( prefix > 0 ) 516 webContext = webContext.substring(prefix+1); 517 int suffix = webContext.lastIndexOf(".war"); 518 if( suffix > 0 ) 519 webContext = webContext.substring(0, suffix); 520 int index = 0; 522 for(; index < webContext.length(); index ++) 523 { 524 char c = webContext.charAt(index); 525 if( Character.isDigit(c) == false && c != '.' ) 526 break; 527 } 528 webContext = webContext.substring(index); 529 } 530 531 if( webContext.length() > 0 && webContext.charAt(0) != '/' ) 533 webContext = "/" + webContext; 534 else if( webContext.equals("/") ) 536 webContext = ""; 537 return webContext; 538 } 539 540 544 @Deprecated 545 protected MBeanServer getServer() 546 { 547 return server; 548 } 549 550 558 protected String getObjectName(WebMetaData metaData) 559 { 560 String ctxPath = metaData.getContextRoot(); 561 String objectName = "jboss.web.deployment:war="+ctxPath; 562 return objectName; 563 } 564 565 574 protected void deployWebModule(DeploymentUnit unit, WebMetaData metaData, 575 AbstractWarDeployment deployment) 576 throws Exception 577 { 578 log.debug("deployWebModule"); 579 try 580 { 581 ServiceMetaData webModule = new ServiceMetaData(); 582 String name = getObjectName(metaData); 583 ObjectName objectName = new ObjectName (name); 584 webModule.setObjectName(objectName); 585 webModule.setCode(WebModule.class.getName()); 586 ServiceConstructorMetaData constructor = new ServiceConstructorMetaData(); 588 constructor.setSignature(new String [] { DeploymentUnit.class.getName(), 589 AbstractWarDeployer.class.getName(), AbstractWarDeployment.class.getName()}); 590 constructor.setParameters(new Object [] {unit, this, deployment}); 591 webModule.setConstructor(constructor); 592 593 Collection <String > depends = metaData.getDependencies(); 595 List <ServiceDependencyMetaData> dependencies = new ArrayList <ServiceDependencyMetaData>(); 596 for(String iDependOn : depends) 597 { 598 ServiceDependencyMetaData sdmd = new ServiceDependencyMetaData(); 599 sdmd.setIDependOn(iDependOn); 600 } 601 webModule.setDependencies(dependencies); 602 603 unit.addAttachment(ServiceMetaData.class, webModule); 605 } 606 catch (Exception e) 607 { 608 throw DeploymentException.rethrowAsDeploymentException("Error creating rar deployment " + unit.getName(), e); 609 } 610 611 } 612 } 613 | Popular Tags |