1 25 26 package org.objectweb.jonas_lib.genbase.archive; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.util.Hashtable ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Vector ; 35 import java.util.jar.JarFile ; 36 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.w3c.dom.Document ; 40 import org.xml.sax.SAXException ; 41 42 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 43 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 44 import org.objectweb.jonas_lib.files.FileUtils; 45 import org.objectweb.jonas_lib.files.FileUtilsException; 46 import org.objectweb.jonas_lib.genbase.GenBaseException; 47 import org.objectweb.jonas_lib.genbase.utils.TempRepository; 48 import org.objectweb.jonas_lib.genbase.utils.XMLUtils; 49 import org.objectweb.jonas_lib.loader.WebappClassLoader; 50 51 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc; 52 import org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager; 53 54 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 55 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc; 56 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager; 57 58 import org.objectweb.util.monolog.api.BasicLevel; 59 60 65 public class WebApp extends J2EEArchive implements EjbRefModule, WsClient, WsEndpoint { 66 67 68 private Application app = null; 69 70 71 private String webFilename; 72 73 74 private WebContainerDeploymentDesc webDD; 75 76 77 private WSDeploymentDesc wsDD = null; 78 79 80 private List sRefs; 81 82 83 private Document webApp = null; 84 85 86 private Document jonasWebApp = null; 87 88 89 private Document webservices = null; 90 91 92 private Document context = null; 93 94 95 private Document webjetty = null; 96 97 98 private Map descriptors; 99 100 103 private List ejbRefs; 104 105 112 public WebApp(Archive archive) throws GenBaseException { 113 super(archive); 114 webFilename = archive.getName(); 115 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 116 getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp"); 117 } 118 init(); 119 } 120 121 129 public WebApp(Archive archive, Application app) throws GenBaseException { 130 super(archive); 131 webFilename = archive.getName(); 132 setApplication(app); 133 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 134 getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp"); 135 } 136 init(); 137 } 138 139 145 private void init() throws GenBaseException { 146 loadDescriptors(); 148 } 149 150 155 private void loadDescriptors() throws GenBaseException { 156 try { 157 webApp = XMLUtils.newDocument(getWebInputStream(), "WEB-INF/web.xml", isDTDsAllowed()); 158 159 InputStream is = getJonasWebInputStream(); 161 162 if (is != null) { 163 jonasWebApp = XMLUtils.newDocument(is, "WEB-INF/jonas-web.xml", isDTDsAllowed()); 164 } 165 166 InputStream isWS = getWebservicesInputStream(); 168 169 if (isWS != null) { 170 webservices = XMLUtils.newDocument(isWS, "WEB-INF/webservices.xml", isDTDsAllowed()); 171 } 172 173 InputStream isContext = getContextInputStream(); 175 if (isContext != null) { 176 context = XMLUtils.newDocument (isContext, "META-INF/context.xml", isDTDsAllowed(), false); 177 } 178 179 InputStream isWebJetty = getWebJettyInputStream(); 181 if (isWebJetty != null) { 182 webjetty = XMLUtils.newDocument (isWebJetty, "WEB-INF/web-jetty.xml", true, true); 184 } 185 186 } catch (SAXException saxe) { 187 String err = getI18n().getMessage("WebApp.loadDescriptors.parseError"); 188 throw new GenBaseException(err, saxe); 189 } catch (ParserConfigurationException pce) { 190 String err = getI18n().getMessage("WebApp.loadDescriptors.prepare"); 191 throw new GenBaseException(err, pce); 192 } catch (IOException ioe) { 193 String err = getI18n().getMessage("WebApp.loadDescriptors.parseError"); 194 throw new GenBaseException(err, ioe); 195 } 196 197 descriptors = new Hashtable (); 198 descriptors.put("WEB-INF/web.xml", webApp); 199 200 if (jonasWebApp != null) { 201 descriptors.put("WEB-INF/jonas-web.xml", jonasWebApp); 202 } 203 204 if (webservices != null) { 205 descriptors.put("WEB-INF/webservices.xml", webservices); 206 } 207 208 if (context != null) { 209 descriptors.put("META-INF/context.xml", context); 210 } 211 212 if (webjetty != null) { 213 descriptors.put("WEB-INF/web-jetty.xml", webjetty); 214 } 215 } 216 217 224 public String getName() { 225 return webFilename; 226 } 227 228 233 public void setApplication(Application app) { 234 this.app = app; 235 } 236 237 242 public Application getApplication() { 243 return app; 244 } 245 246 251 public List getServiceRefDescs() { 252 return sRefs; 253 } 254 255 262 public List getServiceDescs() { 263 if (wsDD != null) { 264 return wsDD.getServiceDescs(); 265 } else { 266 return new Vector (); 267 } 268 } 269 270 275 public void addClasses(File classes) { 276 addDirectoryIn("WEB-INF/classes", classes); 277 } 278 279 284 public Document getWebAppDoc() { 285 return webApp; 286 } 287 288 293 public Document getJonasWebAppDoc() { 294 return jonasWebApp; 295 } 296 297 302 public Document getWebservicesDoc() { 303 return webservices; 304 } 305 306 311 public Document getContextDoc() { 312 return context; 313 } 314 315 320 public Document getWebJettyDoc() { 321 return webjetty; 322 } 323 324 331 private InputStream getWebInputStream() throws IOException { 332 InputStream is = null; 333 334 if (isPacked()) { 335 is = getInputStream("WEB-INF/web.xml"); 336 } else { 337 is = getInputStream("WEB-INF" + File.separator + "web.xml"); 338 } 339 340 return is; 341 } 342 343 350 private InputStream getJonasWebInputStream() throws IOException { 351 InputStream is = null; 352 353 if (isPacked()) { 354 is = getInputStream("WEB-INF/jonas-web.xml"); 355 } else { 356 is = getInputStream("WEB-INF" + File.separator + "jonas-web.xml"); 357 } 358 359 return is; 360 } 361 362 369 private InputStream getWebservicesInputStream() throws IOException { 370 InputStream is = null; 371 372 if (isPacked()) { 373 is = getInputStream("WEB-INF/webservices.xml"); 374 } else { 375 is = getInputStream("WEB-INF" + File.separator + "webservices.xml"); 376 } 377 378 return is; 379 } 380 381 387 private InputStream getContextInputStream() throws IOException { 388 InputStream is = null; 389 if (isPacked()) { 390 is = getInputStream("META-INF/context.xml"); 391 } else { 392 is = getInputStream("META-INF" + File.separator + "context.xml"); 393 } 394 395 return is; 396 } 397 398 404 private InputStream getWebJettyInputStream() throws IOException { 405 InputStream is = null; 406 if (isPacked()) { 407 is = getInputStream("WEB-INF/web-jetty.xml"); 408 } else { 409 is = getInputStream("WEB-INF" + File.separator + "web-jetty.xml"); 410 } 411 412 return is; 413 } 414 415 421 public Map getDescriptors() { 422 return descriptors; 423 } 424 425 432 public boolean omit(String name) { 433 return (name.equals("WEB-INF/web.xml") || name.equals("WEB-INF\\web.xml") 434 || name.equals("WEB-INF/jonas-web.xml") || name.equals("WEB-INF\\jonas-web.xml") 435 || name.equals("WEB-INF/webservices.xml") || name.equals("WEB-INF\\webservices.xml") 436 || name.equals("META-INF/context.xml") || name.equals("META-INF\\context.xml") 437 || name.equals("WEB-INF/web-jetty.xml") || name.equals("WEB-INF\\web-jetty.xml")); 438 } 439 440 444 public void initialize() throws GenBaseException { 445 File webappUnpackDir = null; 446 try { 447 448 if (getArchive().isPacked()) { 449 JarFile jf = new JarFile (getRootFile()); 450 TempRepository tr = TempRepository.getInstance(); 451 webappUnpackDir = tr.createDir(); 452 FileUtils.unpack(jf, webappUnpackDir); 453 jf.close(); 454 setArchive(new FileArchive(webappUnpackDir)); 455 } 456 457 if (app == null) { 458 setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), Thread.currentThread() 460 .getContextClassLoader())); 461 } else { 462 setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), app.getEJBClassLoader())); 464 } 465 } catch (IOException ioe) { 466 String err = getI18n().getMessage("WebApp.init.loader", getArchive().getRootFile()); 467 throw new GenBaseException(err, ioe); 468 } catch (FileUtilsException fue) { 469 String err = getI18n().getMessage("WebApp.init.loader", getArchive().getRootFile()); 470 throw new GenBaseException(err, fue); 471 } 472 473 try { 474 webDD = WebDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader()); 475 } catch (DeploymentDescException dde) { 476 throw new GenBaseException(dde); 477 } 478 479 try { 480 wsDD = WSDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader()); 481 } catch (DeploymentDescException dde) { 482 throw new GenBaseException(dde); 483 } 484 485 sRefs = new Vector (); 487 488 ServiceRefDesc[] refs = webDD.getServiceRefDesc(); 489 490 for (int i = 0; i < refs.length; i++) { 491 sRefs.add(refs[i]); 492 } 493 494 ejbRefs = new Vector (); 496 EjbRefDesc[] refDesc = webDD.getEjbRefDesc(); 497 498 for (int i = 0; i < refDesc.length; i++) { 499 ejbRefs.add(refDesc[i]); 500 } 501 502 } 503 504 507 public String getContextRoot() { 508 return this.wsDD.getContextRoot(); 509 } 510 511 515 public List getEjbRefDescs() { 516 return ejbRefs; 517 } 518 519 522 public void close() { 523 sRefs = null; 524 ejbRefs = null; 525 webDD = null; 526 wsDD = null; 527 webApp = null; 528 app = null; 529 descriptors = null; 530 jonasWebApp = null; 531 webFilename = null; 532 webservices = null; 533 context = null; 534 webjetty = null; 535 } 536 } | Popular Tags |