WebApp.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.util.Hashtable;
00032 import java.util.List;
00033 import java.util.Map;
00034 import java.util.Vector;
00035 import java.util.jar.JarFile;
00036 
00037 import javax.xml.parsers.ParserConfigurationException;
00038 
00039 import org.w3c.dom.Document;
00040 import org.xml.sax.SAXException;
00041 
00042 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00043 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
00044 import org.objectweb.jonas_lib.genbase.GenBaseException;
00045 import org.objectweb.jonas_lib.genbase.utils.FileUtils;
00046 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
00047 import org.objectweb.jonas_lib.loader.WebappClassLoader;
00048 
00049 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
00050 import org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager;
00051 
00052 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
00053 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
00054 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
00055 
00056 import org.objectweb.util.monolog.api.BasicLevel;
00057 
00063 public class WebApp extends J2EEArchive implements EjbRefModule, WsClient, WsEndpoint {
00064 
00066     private Application app = null;
00067 
00069     private String webFilename;
00070 
00072     private WebContainerDeploymentDesc webDD;
00073 
00075     private WSDeploymentDesc wsDD = null;
00076 
00078     private List sRefs;
00079 
00081     private Document webApp = null;
00082 
00084     private Document jonasWebApp = null;
00085 
00087     private Document webservices = null;
00088 
00090     private Map descriptors;
00091 
00095     private List ejbRefs;
00096 
00104     public WebApp(Archive archive) throws GenBaseException {
00105         super(archive);
00106         webFilename = archive.getName();
00107         getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp");
00108         init();
00109     }
00110 
00119     public WebApp(Archive archive, Application app) throws GenBaseException {
00120         super(archive);
00121         webFilename = archive.getName();
00122         setApplication(app);
00123         getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp");
00124         init();
00125     }
00126 
00133     private void init() throws GenBaseException {
00134         // load Deployment Descs
00135         loadDescriptors();
00136     }
00137 
00143     private void loadDescriptors() throws GenBaseException {
00144         try {
00145             webApp = XMLUtils.newDocument(getWebInputStream(), "WEB-INF/web.xml", isDTDsAllowed());
00146 
00147             // jonas-web.xml (optionnal)
00148             InputStream is = getJonasWebInputStream();
00149 
00150             if (is != null) {
00151                 jonasWebApp = XMLUtils.newDocument(is, "WEB-INF/jonas-web.xml", isDTDsAllowed());
00152             }
00153 
00154             // webservices.xml (optionnal)
00155             InputStream isWS = getWebservicesInputStream();
00156 
00157             if (isWS != null) {
00158                 webservices = XMLUtils.newDocument(isWS, "WEB-INF/webservices.xml", isDTDsAllowed());
00159             }
00160         } catch (SAXException saxe) {
00161             String err = getI18n().getMessage("WebApp.loadDescriptors.parseError");
00162             throw new GenBaseException(err, saxe);
00163         } catch (ParserConfigurationException pce) {
00164             String err = getI18n().getMessage("WebApp.loadDescriptors.prepare");
00165             throw new GenBaseException(err, pce);
00166         } catch (IOException ioe) {
00167             String err = getI18n().getMessage("WebApp.loadDescriptors.parseError");
00168             throw new GenBaseException(err, ioe);
00169         }
00170 
00171         descriptors = new Hashtable();
00172         descriptors.put("WEB-INF/web.xml", webApp);
00173 
00174         if (jonasWebApp != null) {
00175             descriptors.put("WEB-INF/jonas-web.xml", jonasWebApp);
00176         }
00177 
00178         if (webservices != null) {
00179             descriptors.put("WEB-INF/webservices.xml", webservices);
00180         }
00181     }
00182 
00190     public String getName() {
00191         return webFilename;
00192     }
00193 
00199     public void setApplication(Application app) {
00200         this.app = app;
00201     }
00202 
00208     public Application getApplication() {
00209         return app;
00210     }
00211 
00217     public List getServiceRefDescs() {
00218         return sRefs;
00219     }
00220 
00228     public List getServiceDescs() {
00229         if (wsDD != null) {
00230             return wsDD.getServiceDescs();
00231         } else {
00232             return new Vector();
00233         }
00234     }
00235 
00241     public void addClasses(File classes) {
00242         addDirectoryIn("WEB-INF/classes", classes);
00243     }
00244 
00250     public Document getWebAppDoc() {
00251         return webApp;
00252     }
00253 
00259     public Document getJonasWebAppDoc() {
00260         return jonasWebApp;
00261     }
00262 
00268     public Document getWebservicesDoc() {
00269         return webservices;
00270     }
00271 
00279     private InputStream getWebInputStream() throws IOException {
00280         InputStream is = null;
00281 
00282         if (isPacked()) {
00283             is = getInputStream("WEB-INF/web.xml");
00284         } else {
00285             is = getInputStream("WEB-INF" + File.separator + "web.xml");
00286         }
00287 
00288         return is;
00289     }
00290 
00298     private InputStream getJonasWebInputStream() throws IOException {
00299         InputStream is = null;
00300 
00301         if (isPacked()) {
00302             is = getInputStream("WEB-INF/jonas-web.xml");
00303         } else {
00304             is = getInputStream("WEB-INF" + File.separator + "jonas-web.xml");
00305         }
00306 
00307         return is;
00308     }
00309 
00317     private InputStream getWebservicesInputStream() throws IOException {
00318         InputStream is = null;
00319 
00320         if (isPacked()) {
00321             is = getInputStream("WEB-INF/webservices.xml");
00322         } else {
00323             is = getInputStream("WEB-INF" + File.separator + "webservices.xml");
00324         }
00325 
00326         return is;
00327     }
00328 
00335     public Map getDescriptors() {
00336         return descriptors;
00337     }
00338 
00346     public boolean omit(String name) {
00347         return (name.equals("WEB-INF/web.xml") || name.equals("WEB-INF\\web.xml")
00348                 || name.equals("WEB-INF/jonas-web.xml") || name.equals("WEB-INF\\jonas-web.xml")
00349                 || name.equals("WEB-INF/webservices.xml") || name.equals("WEB-INF\\webservices.xml"));
00350     }
00351 
00356     public void initialize() throws GenBaseException {
00357         File webappUnpackDir = null;
00358         try {
00359 
00360             if (getArchive().isPacked()) {
00361                 JarFile jf = new JarFile(getRootFile());
00362                 webappUnpackDir = FileUtils.unpack(jf);
00363                 jf.close();
00364                 setArchive(new FileArchive(webappUnpackDir));
00365             }
00366 
00367             if (app == null) {
00368                 // simple webapp case
00369                 setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), Thread.currentThread()
00370                         .getContextClassLoader()));
00371             } else {
00372                 // embedded webapp case
00373                 setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), app.getEJBClassLoader()));
00374             }
00375         } catch (IOException ioe) {
00376             String err = getI18n().getMessage("WebApp.init.loader", getArchive().getRootFile());
00377             throw new GenBaseException(err, ioe);
00378         }
00379 
00380         try {
00381             webDD = WebDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader());
00382         } catch (DeploymentDescException dde) {
00383             throw new GenBaseException(dde);
00384         }
00385 
00386         try {
00387             wsDD = WSDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader());
00388         } catch (DeploymentDescException dde) {
00389             throw new GenBaseException(dde);
00390         }
00391 
00392         // we want a List of service-ref
00393         sRefs = new Vector();
00394 
00395         ServiceRefDesc[] refs = webDD.getServiceRefDesc();
00396 
00397         for (int i = 0; i < refs.length; i++) {
00398             sRefs.add(refs[i]);
00399         }
00400 
00401         // List of ejb-refs
00402         ejbRefs = new Vector();
00403         EjbRefDesc[] refDesc = webDD.getEjbRefDesc();
00404 
00405         for (int i = 0; i < refDesc.length; i++) {
00406             ejbRefs.add(refDesc[i]);
00407         }
00408 
00409     }
00410 
00414     public String getContextRoot() {
00415         return this.wsDD.getContextRoot();
00416     }
00417 
00422     public List getEjbRefDescs() {
00423         return ejbRefs;
00424     }
00425 }

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