EjbJar.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.util.Hashtable;
00033 import java.util.List;
00034 import java.util.Map;
00035 import java.util.Vector;
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_ejb.deployment.api.BeanDesc;
00043 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
00044 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
00045 
00046 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00047 import org.objectweb.jonas_lib.genbase.GenBaseException;
00048 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
00049 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
00050 
00051 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
00052 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
00053 
00054 import org.objectweb.util.monolog.api.BasicLevel;
00055 
00061 public class EjbJar extends J2EEArchive implements WsEndpoint {
00062 
00064     private Application app;
00065 
00067     private WSDeploymentDesc wsDD = null;
00068 
00070     private DeploymentDesc ejbDD = null;
00071 
00073     private List ejbs;
00074 
00076     private Map descriptors;
00077 
00079     private Document jEjbJar;
00080 
00090     public EjbJar(Archive archive) throws GenBaseException {
00091         super(archive);
00092         getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in EjbJar");
00093         init();
00094     }
00095 
00107     public EjbJar(Archive archive, Application app) throws GenBaseException {
00108         super(archive);
00109         setApplication(app);
00110         getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in EjbJar");
00111         init();
00112     }
00113 
00121     private void init() throws GenBaseException {
00122         loadDescriptors();
00123     }
00124 
00125 
00130     public void initialize() throws GenBaseException {
00131 
00132         try {
00133             if (app == null) {
00134                 // simple ejb case
00135                 setModuleClassloader(new EjbJarClassLoader(new URL[] {getArchive()
00136                         .getRootFile().toURL()}, Thread.currentThread()
00137                         .getContextClassLoader()));
00138             } else {
00139                 // embedded ejb case
00140                 setModuleClassloader(app.getEJBClassLoader());
00141             }
00142         } catch (IOException ioe) {
00143             String err = getI18n().getMessage("EjbJar.init.loader", getArchive()
00144                     .getRootFile());
00145             throw new GenBaseException(err, ioe);
00146         }
00147 
00148         try {
00149             ejbDD = EjbDeploymentDescManager.getDeploymentDesc(getRootFile()
00150                     .getAbsolutePath(), getModuleClassloader());
00151         } catch (DeploymentDescException dde) {
00152             throw new GenBaseException(dde);
00153         }
00154 
00155         try {
00156             wsDD = WSDeploymentDescManager.getDeploymentDesc(getRootFile()
00157                     .getAbsolutePath(), getModuleClassloader());
00158         } catch (DeploymentDescException dde) {
00159             throw new GenBaseException(dde);
00160         }
00161 
00162         // create inner EJBs
00163         ejbs = new Vector();
00164 
00165         BeanDesc[] bd = ejbDD.getBeanDesc();
00166 
00167         for (int i = 0; i < bd.length; i++) {
00168             ejbs.add(new Ejb(bd[i], XMLUtils.getBeanElement(jEjbJar
00169                     .getDocumentElement(), bd[i].getEjbName())));
00170         }
00171 
00172     }
00173 
00174 
00181     private void loadDescriptors() throws GenBaseException {
00182         try {
00183             jEjbJar = XMLUtils.newDocument(getJonasEjbJarInputStream(), "META-INF/jonas-ejb-jar.xml", isDTDsAllowed());
00184         } catch (SAXException saxe) {
00185             String err = getI18n().getMessage("EjbJar.loadDescriptors.parseError");
00186             throw new GenBaseException(err, saxe);
00187         } catch (ParserConfigurationException pce) {
00188             String err = getI18n().getMessage("EjbJar.loadDescriptors.prepare");
00189             throw new GenBaseException(err, pce);
00190         } catch (IOException ioe) {
00191             String err = getI18n().getMessage("EjbJar.loadDescriptors.parseError");
00192             throw new GenBaseException(err, ioe);
00193         }
00194 
00195         descriptors = new Hashtable();
00196         descriptors.put("META-INF/jonas-ejb-jar.xml", jEjbJar);
00197     }
00198 
00204     public List getEjbs() {
00205         return ejbs;
00206     }
00207 
00215     public List getServiceDescs() {
00216         if (wsDD != null) {
00217             return wsDD.getServiceDescs();
00218         } else {
00219             return new Vector();
00220         }
00221     }
00222 
00229     public void addClasses(File classes) {
00230         addDirectory(classes);
00231     }
00232 
00236     public String getWarName() {
00237        if (wsDD != null) {
00238            return wsDD.getWarFile();
00239        } else {
00240            return null;
00241        }
00242     }
00243 
00247     public String getContextRoot() {
00248         // compute context root name from ejbjar name
00249         String archiveName = this.getArchive().getName();
00250         String root = null;
00251         if (archiveName.toLowerCase().endsWith(".jar")) {
00252             root = archiveName.toLowerCase().substring(0, archiveName.length() - ".jar".length());
00253         } else {
00254             root = archiveName;
00255         }
00256         if (wsDD != null) {
00257             String croot = wsDD.getContextRoot();
00258             if (croot != null) {
00259                 root = croot;
00260             }
00261         }
00262         getLogger().log(BasicLevel.DEBUG, "Computed context root : " + root);
00263         return root;
00264     }
00265 
00272     public void setApplication(Application app) {
00273         this.app = app;
00274     }
00275 
00281     public Application getApplication() {
00282         return app;
00283     }
00284 
00291     public Map getDescriptors() {
00292         return descriptors;
00293     }
00294 
00303     public boolean omit(String name) {
00304         return (name.equals("META-INF/jonas-ejb-jar.xml")
00305                 || name.equals("META-INF\\jonas-ejb-jar.xml"));
00306     }
00307 
00313     public Document getJonasEjbJarDoc() {
00314         return jEjbJar;
00315     }
00316 
00325     public InputStream getJonasEjbJarInputStream() throws IOException {
00326         InputStream is = null;
00327 
00328         if (isPacked()) {
00329             is = getInputStream("META-INF/jonas-ejb-jar.xml");
00330         } else {
00331             is = getInputStream("META-INF" + File.separator
00332                     + "jonas-ejb-jar.xml");
00333         }
00334         if (is == null) {
00335             throw new IOException("Cannot read jonas specific DD for EJB as the entry is not present");
00336         }
00337         return is;
00338     }
00339 }

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