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.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.util.Hashtable ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Vector ; 38 import java.util.jar.Attributes ; 39 import java.util.jar.JarFile ; 40 41 import javax.xml.parsers.ParserConfigurationException ; 42 43 import org.w3c.dom.Document ; 44 import org.xml.sax.SAXException ; 45 46 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc; 47 import org.objectweb.jonas_ear.deployment.lib.EarDeploymentDescManager; 48 import org.objectweb.jonas_ear.deployment.xml.Web; 49 50 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 51 import org.objectweb.jonas_lib.files.FileUtils; 52 import org.objectweb.jonas_lib.files.FileUtilsException; 53 import org.objectweb.jonas_lib.genbase.GenBaseException; 54 import org.objectweb.jonas_lib.genbase.utils.TempRepository; 55 import org.objectweb.jonas_lib.genbase.utils.XMLUtils; 56 import org.objectweb.jonas_lib.loader.EjbJarClassLoader; 57 58 import org.objectweb.util.monolog.api.BasicLevel; 59 60 66 public class Application extends J2EEArchive { 67 68 69 private List clients; 70 71 72 private List webapps; 73 74 75 private List ejbjars; 76 77 78 private EarDeploymentDesc earDD = null; 79 80 81 private Map descriptors; 82 83 84 private Document app; 85 86 87 private URLClassLoader ejbCL = null; 88 89 90 private URLClassLoader commonCL = null; 91 92 93 private List pathFiles; 94 95 96 private String appFilename; 97 98 105 public Application(Archive archive) throws GenBaseException { 106 super(archive); 107 appFilename = archive.getRootFile().getName(); 108 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 109 getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in Application"); 110 } 111 init(); 112 } 113 114 121 protected void init() throws GenBaseException { 122 123 pathFiles = new Vector (); 124 125 ejbjars = new Vector (); 126 webapps = new Vector (); 127 clients = new Vector (); 128 129 if (isPacked()) { 130 131 try { 132 JarFile jf = new JarFile (getArchive().getRootFile()); 134 TempRepository tr = TempRepository.getInstance(); 135 File unpacked = tr.createDir(); 136 FileUtils.unpack(jf, unpacked); 137 jf.close(); 138 setArchive(new FileArchive(unpacked)); 139 } catch (FileUtilsException fue) { 140 String err = getI18n().getMessage("Application.init.unpackException", getArchive().getRootFile()); 141 throw new GenBaseException(err, fue); 142 } catch (IOException ioe) { 143 String err = getI18n().getMessage("Application.init.unpackException", getArchive().getRootFile()); 144 throw new GenBaseException(err, ioe); 145 } 146 } 147 148 try { 150 earDD = 151 EarDeploymentDescManager.getDeploymentDesc(getRootFile().getAbsolutePath(), 152 Thread.currentThread().getContextClassLoader()); 153 } catch (DeploymentDescException dde) { 154 String err = 155 getI18n().getMessage("Application.init.earDDExc", 156 getArchive().getRootFile()); 157 throw new GenBaseException(err, dde); 158 } 159 160 String [] ejbs = earDD.getEjbTags(); 162 163 for (int i = 0; i < ejbs.length; i++) { 164 File ejbFile = new File (getRootFile(), ejbs[i]); 165 Archive ejbArch = null; 166 167 if (ejbFile.isDirectory()) { 168 ejbArch = new FileArchive(ejbFile); 170 } else { 171 ejbArch = new JarArchive(ejbFile); 173 } 174 ejbjars.add(new EjbJar(ejbArch, this)); 175 176 addClassPathEntry(ejbArch); 178 } 179 180 Web[] webs = earDD.getWebTags(); 182 183 for (int i = 0; i < webs.length; i++) { 184 File webFile = new File (getRootFile(), webs[i].getWebUri()); 185 186 if (webFile.isDirectory()) { 187 webapps.add(new WebApp(new FileArchive(webFile), this)); 189 } else { 190 webapps.add(new WebApp(new JarArchive(webFile), this)); 192 } 193 } 194 195 String [] clts = earDD.getClientTags(); 197 198 for (int i = 0; i < clts.length; i++) { 199 File clientFile = new File (getRootFile(), clts[i]); 200 201 if (clientFile.isDirectory()) { 202 clients.add(new Client(new FileArchive(clientFile), this)); 204 } else { 205 clients.add(new Client(new JarArchive(clientFile), this)); 207 } 208 } 209 210 setModuleClassloader(createEARClassLoader()); 212 213 ejbCL = createEJBClassLoader(); 215 216 loadDescriptors(); 217 } 218 219 224 protected void loadDescriptors() throws GenBaseException { 225 try { 226 app = XMLUtils.newDocument(getApplicationInputStream(), "META-INF/application.xml", isDTDsAllowed()); 227 } catch (SAXException saxe) { 228 String err = getI18n().getMessage("Application.loadDescriptors.parseError"); 229 throw new GenBaseException(err, saxe); 230 } catch (ParserConfigurationException pce) { 231 String err = getI18n().getMessage("Application.loadDescriptors.prepare"); 232 throw new GenBaseException(err, pce); 233 } catch (IOException ioe) { 234 String err = getI18n().getMessage("Application.loadDescriptors.parseError"); 235 throw new GenBaseException(err, ioe); 236 } 237 238 descriptors = new Hashtable (); 239 descriptors.put("META-INF/application.xml", app); 240 } 241 242 246 public void initialize() throws GenBaseException { 247 248 for (Iterator i = ejbjars.iterator(); i.hasNext();) { 250 EjbJar ejb = (EjbJar) i.next(); 251 ejb.initialize(); 252 } 253 254 for (Iterator i = webapps.iterator(); i.hasNext();) { 256 WebApp web = (WebApp) i.next(); 257 web.initialize(); 258 } 259 260 for (Iterator i = clients.iterator(); i.hasNext();) { 262 Client client = (Client) i.next(); 263 client.initialize(); 264 } 265 266 } 267 268 275 public String getName() { 276 return appFilename; 277 } 278 279 286 private URLClassLoader createEJBClassLoader() throws GenBaseException { 287 288 URL [] urls = new URL [pathFiles.size()]; 289 int index = 0; 290 for (Iterator i = pathFiles.iterator(); i.hasNext(); index++) { 291 File f = (File ) i.next(); 292 try { 293 urls[index] = f.toURL(); 294 } catch (IOException ioe) { 295 String err = "Cannot convert " + f + " to URL."; 296 throw new GenBaseException(err, ioe); 297 } 298 } 299 300 commonCL = new URLClassLoader (urls, getModuleClassloader()); 301 302 urls = new URL [ejbjars.size()]; 303 index = 0; 304 for (Iterator i = ejbjars.iterator(); i.hasNext(); index++) { 305 try { 306 urls[index] = ((EjbJar) i.next()).getRootFile().toURL(); 307 } catch (IOException ioe) { 308 String err = "Cannot transform as a URL : " + ioe.getMessage(); 309 throw new GenBaseException(err, ioe); 310 } 311 } 312 313 try { 314 return new EjbJarClassLoader(urls, commonCL); 315 } catch (IOException ioe) { 316 String err = "Cannot create EjbJarClassLoader"; 317 throw new GenBaseException(err, ioe); 318 } 319 320 } 321 322 329 private URLClassLoader createEARClassLoader() throws GenBaseException { 330 331 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 333 334 String classpath = getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH); 336 URL [] urls = new URL [0]; 337 if (classpath != null) { 338 String [] paths = classpath.split(","); 340 urls = new URL [paths.length]; 341 for (int i = 0; i < paths.length; i++) { 342 try { 343 URL path = new File (getRootFile(), paths[i]).toURL(); 344 urls[i] = path; 345 } catch (IOException ioe) { 346 String err = "Cannot transform '" + paths[i] + "' as a URL"; 347 throw new GenBaseException(err, ioe); 348 } 349 } 350 } 351 352 return new URLClassLoader (urls, parent); 353 } 354 355 363 private void addClassPathEntry(Archive a) throws GenBaseException { 364 String classpath = a.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH); 366 367 if (classpath != null) { 368 String [] paths = classpath.split(","); 370 for (int i = 0; i < paths.length; i++) { 371 try { 372 File path = new File (a.getRootFile().getParentFile(), paths[i]).getCanonicalFile(); 373 if (!pathFiles.contains(path)) { 374 pathFiles.add(path); 375 } 376 } catch (IOException ioe) { 377 String err = "Cannot add in EAR classpath :" + paths[i]; 378 throw new GenBaseException(err, ioe); 379 } 380 } 381 382 } 383 384 } 385 386 391 public Document getApplicationDoc() { 392 return app; 393 } 394 395 403 public InputStream getApplicationInputStream() throws IOException { 404 InputStream is = null; 405 406 if (isPacked()) { 407 is = getInputStream("META-INF/application.xml"); 408 } else { 409 is = getInputStream("META-INF" + File.separator + "application.xml"); 410 } 411 412 return is; 413 } 414 415 420 public void addEjbJar(EjbJar ejbjar) { 421 ejbjars.add(ejbjar); 422 XMLUtils.addEjb(app, ejbjar); 424 } 425 426 431 public void addClient(Client client) { 432 clients.add(client); 433 XMLUtils.addClient(app, client); 435 } 436 437 443 public void addWebApp(WebApp webapp, String context) { 444 webapps.add(webapp); 445 XMLUtils.addWebApp(app, webapp, context); 447 addFile(webapp.getRootFile(), webapp.getName()); 448 } 449 450 455 public Iterator getEjbJars() { 456 return ejbjars.iterator(); 457 } 458 459 464 public Iterator getWebApps() { 465 return webapps.iterator(); 466 } 467 468 473 public Iterator getClients() { 474 return clients.iterator(); 475 } 476 477 482 public URLClassLoader getEARClassLoader() { 483 return commonCL; 484 } 485 486 491 public URLClassLoader getEJBClassLoader() { 492 return ejbCL; 493 } 494 495 501 public Map getDescriptors() { 502 return descriptors; 503 } 504 505 512 public boolean omit(String name) { 513 return (name.equals("META-INF/application.xml") || name.equals("META-INF\\application.xml")); 514 } 515 516 519 public void setClients(List clients) { 520 this.clients = clients; 521 } 522 523 526 public void setEjbjars(List ejbjars) { 527 this.ejbjars = ejbjars; 528 } 529 530 533 public void setWebapps(List webapps) { 534 this.webapps = webapps; 535 } 536 537 540 public Document getApp() { 541 return app; 542 } 543 544 547 public void close() { 548 super.close(); 549 earDD = null; 551 descriptors = null; 553 app = null; 554 ejbCL = null; 555 commonCL = null; 556 pathFiles = null; 557 558 for (Iterator i = getEjbJars(); i.hasNext();) { 560 EjbJar ejbjar = (EjbJar) i.next(); 561 ejbjar.close(); 562 } 563 564 for (Iterator i = getWebApps(); i.hasNext();) { 566 WebApp webapp = (WebApp) i.next(); 567 webapp.close(); 568 } 569 570 for (Iterator i = getClients(); i.hasNext();) { 572 Client client = (Client) i.next(); 573 client.close(); 574 } 575 576 clients = null; 578 webapps = null; 579 ejbjars = null; 580 581 582 } 583 } | Popular Tags |