Application.java

00001 
00026 package org.objectweb.jonas_lib.genbase.archive;
00027 
00028 import java.io.File;
00029 import java.io.IOException;
00030 import java.io.InputStream;
00031 import java.net.URL;
00032 import java.net.URLClassLoader;
00033 import java.util.Hashtable;
00034 import java.util.Iterator;
00035 import java.util.List;
00036 import java.util.Map;
00037 import java.util.Vector;
00038 import java.util.jar.Attributes;
00039 import java.util.jar.JarFile;
00040 
00041 import javax.xml.parsers.ParserConfigurationException;
00042 
00043 import org.w3c.dom.Document;
00044 import org.xml.sax.SAXException;
00045 
00046 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
00047 import org.objectweb.jonas_ear.deployment.lib.EarDeploymentDescManager;
00048 import org.objectweb.jonas_ear.deployment.xml.Web;
00049 
00050 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00051 import org.objectweb.jonas_lib.genbase.GenBaseException;
00052 import org.objectweb.jonas_lib.genbase.utils.FileUtils;
00053 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
00054 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
00055 
00056 
00057 import org.objectweb.util.monolog.api.BasicLevel;
00058 
00065 public class Application extends J2EEArchive {
00066 
00068     private List clients;
00069 
00071     private List webapps;
00072 
00074     private List ejbjars;
00075 
00077     private EarDeploymentDesc earDD = null;
00078 
00080     private Map descriptors;
00081 
00083     private Document app;
00084 
00086     private URLClassLoader ejbCL = null;
00087 
00089     private URLClassLoader commonCL = null;
00090 
00092     private List pathFiles;
00093 
00095     private String appFilename;
00096 
00104     public Application(Archive archive) throws GenBaseException {
00105         super(archive);
00106         appFilename = archive.getRootFile().getName();
00107         getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in Application");
00108         init();
00109     }
00110 
00118     protected void init() throws GenBaseException {
00119 
00120         pathFiles = new Vector();
00121 
00122         ejbjars = new Vector();
00123         webapps = new Vector();
00124         clients = new Vector();
00125 
00126         if (isPacked()) {
00127 
00128             try {
00129                 // unpack data
00130                 JarFile jf = new JarFile(getArchive().getRootFile());
00131                 File unpacked = FileUtils.unpack(jf);
00132                 jf.close();
00133                 setArchive(new FileArchive(unpacked));
00134             } catch (IOException ioe) {
00135                 String err = getI18n().getMessage("Application.init.unpackException", getArchive().getRootFile());
00136                 throw new GenBaseException(err, ioe);
00137             }
00138         }
00139 
00140         // load META-INF/application.xml
00141         try {
00142             earDD =
00143                 EarDeploymentDescManager.getDeploymentDesc(getRootFile().getAbsolutePath(),
00144                     Thread.currentThread().getContextClassLoader());
00145         } catch (DeploymentDescException dde) {
00146             String err =
00147                 getI18n().getMessage("Application.init.earDDExc",
00148                     getArchive().getRootFile());
00149             throw new GenBaseException(err, dde);
00150         }
00151 
00152         // add EjbJars
00153         String[] ejbs = earDD.getEjbTags();
00154 
00155         for (int i = 0; i < ejbs.length; i++) {
00156             File ejbFile = new File(getRootFile(), ejbs[i]);
00157             Archive ejbArch = null;
00158 
00159             if (ejbFile.isDirectory()) {
00160                 // Unpacked Jar
00161                 ejbArch = new FileArchive(ejbFile);
00162             } else {
00163                 // Packed Jar
00164                 ejbArch = new JarArchive(ejbFile);
00165             }
00166             ejbjars.add(new EjbJar(ejbArch, this));
00167 
00168             // add entries for Class-Path
00169             addClassPathEntry(ejbArch);
00170         }
00171 
00172         // add WebApps
00173         Web[] webs = earDD.getWebTags();
00174 
00175         for (int i = 0; i < webs.length; i++) {
00176             File webFile = new File(getRootFile(), webs[i].getWebUri());
00177 
00178             if (webFile.isDirectory()) {
00179                 // Unpacked Jar
00180                 webapps.add(new WebApp(new FileArchive(webFile), this));
00181             } else {
00182                 // Packed Jar
00183                 webapps.add(new WebApp(new JarArchive(webFile), this));
00184             }
00185         }
00186 
00187         // add Clients
00188         String[] clts = earDD.getClientTags();
00189 
00190         for (int i = 0; i < clts.length; i++) {
00191             File clientFile = new File(getRootFile(), clts[i]);
00192 
00193             if (clientFile.isDirectory()) {
00194                 // Unpacked Jar
00195                 clients.add(new Client(new FileArchive(clientFile), this));
00196             } else {
00197                 // Packed Jar
00198                 clients.add(new Client(new JarArchive(clientFile), this));
00199             }
00200         }
00201 
00202         // Create ear ClassLoader (from MANIFEST/Class-Path general entry)
00203         setModuleClassloader(createEARClassLoader());
00204 
00205         // Create EJB ClassLoader
00206         ejbCL = createEJBClassLoader();
00207 
00208         loadDescriptors();
00209     }
00210 
00216     protected void loadDescriptors() throws GenBaseException {
00217         try {
00218             app = XMLUtils.newDocument(getApplicationInputStream(), "META-INF/application.xml", isDTDsAllowed());
00219         } catch (SAXException saxe) {
00220             String err = getI18n().getMessage("Application.loadDescriptors.parseError");
00221             throw new GenBaseException(err, saxe);
00222         } catch (ParserConfigurationException pce) {
00223             String err = getI18n().getMessage("Application.loadDescriptors.prepare");
00224             throw new GenBaseException(err, pce);
00225         } catch (IOException ioe) {
00226             String err = getI18n().getMessage("Application.loadDescriptors.parseError");
00227             throw new GenBaseException(err, ioe);
00228         }
00229 
00230         descriptors = new Hashtable();
00231         descriptors.put("META-INF/application.xml", app);
00232     }
00233 
00238     public void initialize() throws GenBaseException {
00239 
00240         // init ejbjars
00241         for (Iterator i = ejbjars.iterator(); i.hasNext();) {
00242             EjbJar ejb = (EjbJar) i.next();
00243             ejb.initialize();
00244         }
00245 
00246         // init webapps
00247         for (Iterator i = webapps.iterator(); i.hasNext();) {
00248             WebApp web = (WebApp) i.next();
00249             web.initialize();
00250         }
00251 
00252         // init clients
00253         for (Iterator i = clients.iterator(); i.hasNext();) {
00254             Client client = (Client) i.next();
00255             client.initialize();
00256         }
00257 
00258     }
00259 
00267     public String getName() {
00268         return appFilename;
00269     }
00270 
00278     private URLClassLoader createEJBClassLoader() throws GenBaseException {
00279 
00280         URL[] urls = new URL[pathFiles.size()];
00281         int index = 0;
00282         for (Iterator i = pathFiles.iterator(); i.hasNext(); index++) {
00283             File f = (File) i.next();
00284             try {
00285                 urls[index] = f.toURL();
00286             } catch (IOException ioe) {
00287                 String err = "Cannot convert " + f + " to URL.";
00288                 throw new GenBaseException(err, ioe);
00289             }
00290         }
00291 
00292         commonCL = new URLClassLoader(urls, getModuleClassloader());
00293 
00294         urls = new URL[ejbjars.size()];
00295         index = 0;
00296         for (Iterator i = ejbjars.iterator(); i.hasNext(); index++) {
00297             try {
00298                 urls[index] = ((EjbJar) i.next()).getRootFile().toURL();
00299             } catch (IOException ioe) {
00300                 String err = "Cannot transform as a URL : " + ioe.getMessage();
00301                 throw new GenBaseException(err, ioe);
00302             }
00303         }
00304 
00305         try {
00306             return new EjbJarClassLoader(urls, commonCL);
00307         } catch (IOException ioe) {
00308             String err = "Cannot create EjbJarClassLoader";
00309             throw new GenBaseException(err, ioe);
00310         }
00311 
00312     }
00313 
00321     private URLClassLoader createEARClassLoader() throws GenBaseException {
00322 
00323         // find parent ClassLoader
00324         ClassLoader parent = Thread.currentThread().getContextClassLoader();
00325 
00326         // get Manifest Attributes if any
00327         String classpath = getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
00328         URL[] urls = new URL[0];
00329         if (classpath != null) {
00330             // Lookup specified files.
00331             String[] paths = classpath.split(",");
00332             urls = new URL[paths.length];
00333             for (int i = 0; i < paths.length; i++) {
00334                 try {
00335                     URL path = new File(getRootFile(), paths[i]).toURL();
00336                     urls[i] = path;
00337                 } catch (IOException ioe) {
00338                     String err = "Cannot transform '" + paths[i] + "' as a URL";
00339                     throw new GenBaseException(err, ioe);
00340                 }
00341             }
00342         }
00343 
00344         return new URLClassLoader(urls, parent);
00345     }
00346 
00355     private void addClassPathEntry(Archive a) throws GenBaseException {
00356         // get Manifest Attributes if any
00357         String classpath = a.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
00358 
00359         if (classpath != null) {
00360             // Lookup specified files.
00361             String[] paths = classpath.split(",");
00362             for (int i = 0; i < paths.length; i++) {
00363                 try {
00364                     File path = new File(a.getRootFile().getParentFile(), paths[i]).getCanonicalFile();
00365                     if (!pathFiles.contains(path)) {
00366                         pathFiles.add(path);
00367                     }
00368                 } catch (IOException ioe) {
00369                     String err = "Cannot add in EAR classpath :" + paths[i];
00370                     throw new GenBaseException(err, ioe);
00371                 }
00372             }
00373 
00374         }
00375 
00376     }
00377 
00383     public Document getApplicationDoc() {
00384         return app;
00385     }
00386 
00395     public InputStream getApplicationInputStream() throws IOException {
00396         InputStream is = null;
00397 
00398         if (isPacked()) {
00399             is = getInputStream("META-INF/application.xml");
00400         } else {
00401             is = getInputStream("META-INF" + File.separator + "application.xml");
00402         }
00403 
00404         return is;
00405     }
00406 
00412     public void addEjbJar(EjbJar ejbjar) {
00413         ejbjars.add(ejbjar);
00414         // add module in application.xml
00415         XMLUtils.addEjb(app, ejbjar);
00416     }
00417 
00423     public void addClient(Client client) {
00424         clients.add(client);
00425         // add module in application.xml
00426         XMLUtils.addClient(app, client);
00427     }
00428 
00435     public void addWebApp(WebApp webapp, String context) {
00436         webapps.add(webapp);
00437         // add module in application.xml
00438         XMLUtils.addWebApp(app, webapp, context);
00439         addFile(webapp.getRootFile(), webapp.getName());
00440     }
00441 
00447     public Iterator getEjbJars() {
00448         return ejbjars.iterator();
00449     }
00450 
00456     public Iterator getWebApps() {
00457         return webapps.iterator();
00458     }
00459 
00465     public Iterator getClients() {
00466         return clients.iterator();
00467     }
00468 
00474     public URLClassLoader getEARClassLoader() {
00475         return commonCL;
00476     }
00477 
00483     public URLClassLoader getEJBClassLoader() {
00484         return ejbCL;
00485     }
00486 
00493     public Map getDescriptors() {
00494         return descriptors;
00495     }
00496 
00504     public boolean omit(String name) {
00505         return (name.equals("META-INF/application.xml") || name.equals("META-INF\\application.xml"));
00506     }
00507 
00511     public void setClients(List clients) {
00512         this.clients = clients;
00513     }
00514 
00518     public void setEjbjars(List ejbjars) {
00519         this.ejbjars = ejbjars;
00520     }
00521 
00525     public void setWebapps(List webapps) {
00526         this.webapps = webapps;
00527     }
00528 
00532     public Document getApp() {
00533         return app;
00534     }
00535 }

Generated on Tue Feb 15 15:05:36 2005 for JOnAS by  doxygen 1.3.9.1