1 20 package org.apache.cactus.integration.ant.container.jboss; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.StringReader ; 25 import java.util.jar.Attributes ; 26 import java.util.jar.JarFile ; 27 import java.util.jar.Manifest ; 28 import java.util.zip.ZipEntry ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import org.apache.cactus.integration.ant.container.AbstractJavaContainer; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.taskdefs.Copy; 37 import org.apache.tools.ant.taskdefs.Java; 38 import org.apache.tools.ant.types.FileSet; 39 import org.apache.tools.ant.types.Path; 40 import org.apache.tools.ant.util.FileUtils; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.xml.sax.EntityResolver ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 48 53 public class JBoss3xContainer extends AbstractJavaContainer 54 { 55 57 60 private File dir; 61 62 65 private String config = "default"; 66 67 71 private File configDir; 72 73 76 private int port = 8080; 77 78 81 private int jndiPort = 1099; 82 83 87 private String version; 88 89 92 private String testContextRoot; 93 94 97 private File tmpDir; 98 99 101 108 public final void setDir(File theDir) throws BuildException 109 { 110 this.dir = theDir; 111 } 112 113 118 public final void setConfig(String theConfig) 119 { 120 this.config = theConfig; 121 } 122 123 128 public final void setConfigDir(File theConfigDir) 129 { 130 this.configDir = theConfigDir; 131 } 132 133 147 public final void setPort(int thePort) 148 { 149 this.port = thePort; 150 } 151 152 157 public final void setJndiPort(int theJndiPort) 158 { 159 this.jndiPort = theJndiPort; 160 } 161 162 167 public final void setTmpDir(File theTmpDir) 168 { 169 this.tmpDir = theTmpDir; 170 } 171 172 174 177 public String getTestContext() 178 { 179 return this.testContextRoot; 180 } 181 182 185 public final String getName() 186 { 187 return "JBoss " + this.version; 188 } 189 190 193 public final int getPort() 194 { 195 return this.port; 196 } 197 198 203 public final int getJndiPort() 204 { 205 return this.jndiPort; 206 } 207 208 212 protected final File getTmpDir() 213 { 214 return this.tmpDir; 215 } 216 217 220 public final void init() 221 { 222 this.version = getVersion(this.dir); 224 if (this.version == null) 225 { 226 throw new BuildException(this.dir 227 + " not recognized as a JBoss 3.x installation"); 228 } 229 if (!this.version.startsWith("3")) 230 { 231 throw new BuildException( 232 "This element doesn't support version " + this.version 233 + " of JBoss"); 234 } 235 236 this.testContextRoot = getTestContextFromJBossWebXml(); 239 240 } 244 245 248 public final void startUp() 249 { 250 try 251 { 252 if (getTmpDir() == null) 259 { 260 setTmpDir(new File (this.dir, "server/cactus")); 261 } 262 263 File customServerDir = setupTempDirectory(getTmpDir(), 264 "cactus/jboss3x"); 265 cleanTempDirectory(customServerDir); 266 267 prepare("cactus/jboss3x", customServerDir); 268 269 File binDir = new File (this.dir, "bin"); 270 271 Java java = createJavaForStartUp(); 272 java.setDir(binDir); 273 274 java.addSysproperty( 275 createSysProperty("program.name", 276 new File (binDir, "run.bat"))); 277 java.addSysproperty( 278 createSysProperty("jboss.server.home.dir", customServerDir)); 279 java.addSysproperty( 280 createSysProperty("jboss.server.home.url", 281 customServerDir.toURL().toString())); 282 283 Path classpath = java.createClasspath(); 284 classpath.createPathElement().setLocation( 285 new File (binDir, "run.jar")); 286 addToolsJarToClasspath(classpath); 287 java.setClassname("org.jboss.Main"); 288 java.createArg().setValue("-c"); 289 java.createArg().setValue(this.config); 290 java.execute(); 291 } 292 catch (IOException ioe) 293 { 294 getLog().error("Failed to startup the container", ioe); 295 throw new BuildException(ioe); 296 } 297 } 298 299 302 public final void shutDown() 303 { 304 File binDir = new File (this.dir, "bin"); 305 306 Java java = createJavaForShutDown(); 307 java.setFork(true); 308 309 Path classPath = java.createClasspath(); 310 classPath.createPathElement().setLocation( 311 new File (binDir, "shutdown.jar")); 312 313 java.setClassname("org.jboss.Shutdown"); 314 315 if (this.version.startsWith("3.2")) 316 { 317 java.createArg().setValue("--server=localhost:" 318 + this.getJndiPort()); 319 java.createArg().setValue("--shutdown"); 320 } 321 else 322 { 323 java.createArg().setValue("localhost"); 324 java.createArg().setValue(String.valueOf(getPort())); 325 } 326 java.execute(); 327 } 328 329 331 335 private String getTestContextFromJBossWebXml() 336 { 337 String testContext = null; 338 339 try 340 { 341 Document doc = getJBossWebXML(); 342 Element root = doc.getDocumentElement(); 343 Node context = root.getElementsByTagName("context-root").item(0); 344 testContext = context.getFirstChild().getNodeValue(); 345 } 346 catch (Exception e) 347 { 348 } 350 351 return testContext; 352 } 353 354 364 private void prepare(String theDirName, File theCustomServerDir) 365 throws IOException 366 { 367 FileUtils fileUtils = FileUtils.newFileUtils(); 368 369 File computedConfigDir; 372 if (this.configDir == null) 373 { 374 computedConfigDir = new File (this.dir, "server"); 375 } 376 else 377 { 378 computedConfigDir = this.configDir; 379 } 380 381 Copy copy = new Copy(); 384 copy.setTaskName("cactus"); 385 copy.setProject(getProject()); 386 copy.setTodir(theCustomServerDir); 387 FileSet srcFiles = new FileSet(); 388 srcFiles.setDir(new File (computedConfigDir, this.config)); 389 copy.addFileset(srcFiles); 390 copy.execute(); 391 392 File deployDir = new File (theCustomServerDir, "/deploy"); 395 fileUtils.copyFile(getDeployableFile().getFile(), 396 new File (deployDir, getDeployableFile().getFile().getName()), 397 null, true); 398 } 399 400 407 private String getVersion(File theDir) 408 { 409 String retVal = null; 411 try 412 { 413 JarFile runJar = new JarFile (new File (theDir, "bin/run.jar")); 414 Manifest mf = runJar.getManifest(); 415 if (mf != null) 416 { 417 Attributes attrs = mf.getMainAttributes(); 418 retVal = attrs.getValue(Attributes.Name.SPECIFICATION_VERSION); 419 } 420 else 421 { 422 getLog().warn("Couldn't find MANIFEST.MF in " + runJar); 423 } 424 } 425 catch (IOException ioe) 426 { 427 getLog().warn("Couldn't retrieve JBoss version information", ioe); 428 } 429 return retVal; 430 } 431 432 440 private Document getJBossWebXML() throws 441 IOException , ParserConfigurationException , SAXException 442 { 443 Document doc = null; 444 File configDir = new File (this.dir, "server"); 445 File deployDir = new File (configDir, this.config + "/deploy"); 446 File warFile = new File (deployDir, 447 getDeployableFile().getFile().getName()); 448 449 JarFile war = new JarFile (warFile); 450 ZipEntry entry = war.getEntry("WEB-INF/jboss-web.xml"); 451 if (entry != null) 452 { 453 DocumentBuilderFactory factory = 454 DocumentBuilderFactory.newInstance(); 455 factory.setValidating(false); 456 factory.setNamespaceAware(false); 457 458 DocumentBuilder builder = factory.newDocumentBuilder(); 459 builder.setEntityResolver(new EntityResolver () 460 { 461 public InputSource resolveEntity(String thePublicId, 462 String theSystemId) throws SAXException 463 { 464 return new InputSource (new StringReader ("")); 465 } 466 }); 467 doc = builder.parse(war.getInputStream(entry)); 468 } 469 war.close(); 470 return doc; 471 } 472 473 } 474 | Popular Tags |