EarClassPathManager.java

00001 
00027 package org.objectweb.jonas.ear.lib;
00028 
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.IOException;
00032 import java.net.MalformedURLException;
00033 import java.net.URL;
00034 import java.util.StringTokenizer;
00035 import java.util.jar.Attributes;
00036 import java.util.jar.JarFile;
00037 import java.util.jar.Manifest;
00038 
00047 public class EarClassPathManager {
00048 
00052     private URL[] urls = null;
00053 
00057     private JarList toParse = null;
00058 
00062     private JarList parsed = null;
00063 
00067     private JarList libraries = null;
00068 
00072     private JarList ejbs = null;
00073 
00077     private JarList wars = null;
00078 
00082     private JarList clients = null;
00083 
00087     private URL directory = null;
00088 
00096     public EarClassPathManager(JarList ejbs, JarList wars, URL directory) throws EarClassPathManagerException {
00097 
00098         if ((ejbs == null) || (wars == null) || (directory == null)) {
00099             throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters");
00100         }
00101 
00102         //check protocol
00103         if (!directory.getProtocol().equalsIgnoreCase("file")) {
00104             throw new EarClassPathManagerException("Only the file:/ URL can be used");
00105         }
00106         this.ejbs = ejbs;
00107         this.wars = wars;
00108         this.clients = new JarList();
00109         this.directory = directory;
00110     }
00111 
00118     public EarClassPathManager(JarList clients, URL directory) throws EarClassPathManagerException {
00119 
00120         if ((clients == null) || (directory == null)) {
00121             throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters");
00122         }
00123 
00124         //check protocol
00125         if (!directory.getProtocol().equalsIgnoreCase("file")) {
00126             throw new EarClassPathManagerException("Only the file:/ URL can be used");
00127         }
00128         this.ejbs = new JarList();
00129         this.wars = new JarList();
00130         this.clients = clients;
00131         this.directory = directory;
00132     }
00133 
00142     private JarList getManifestClassPath(URL url) throws EarClassPathManagerException, IOException {
00143 
00144         if (url == null) {
00145             throw new EarClassPathManagerException("JarList.getManifestClassPath : The url parameter can't be null");
00146         }
00147 
00148         Manifest manifest = null;
00149 
00150         if (new File(url.getFile()).isDirectory()) {
00151             File manifestFile = new File(url.getFile() + File.separator + JarFile.MANIFEST_NAME);
00152             if (manifestFile.exists()) {
00153                 manifest = new Manifest(new FileInputStream(manifestFile));
00154             }
00155 
00156         } else {
00157             //Construct a JarFile in order to access to the manifest
00158             // IOException it if failed
00159             JarFile jarFile = new JarFile(url.getFile());
00160 
00161             //get manifest from the jarFile
00162             manifest = jarFile.getManifest();
00163         }
00164 
00165         //classpath
00166         String classPath = null;
00167 
00168         //Only if a manifest is found
00169         if (manifest != null) {
00170             //get attributes (classpath)
00171             Attributes attributes = manifest.getMainAttributes();
00172             classPath = attributes.getValue(Attributes.Name.CLASS_PATH);
00173         }
00174 
00175         //New JarList
00176         JarList jarList = null;
00177 
00178         //The jarList will be Empty if classpath is null or populate with
00179         // classpath entries
00180         if (classPath != null) {
00181             jarList = new JarList(new StringTokenizer(classPath));
00182         } else {
00183             jarList = new JarList();
00184         }
00185 
00186         //Return the list
00187         return jarList;
00188     }
00189 
00195     public URL[] getResolvedClassPath() throws EarClassPathManagerException {
00196 
00197         //If we don't have already compute
00198         if (urls == null) {
00199             resolveClassPath();
00200         }
00201 
00202         //return the cache
00203         return urls;
00204     }
00205 
00210     private void resolveClassPath() throws EarClassPathManagerException {
00211 
00212         //Set the list to parsed
00213         toParse = new JarList();
00214 
00215         //Set the list already parsed
00216         parsed = new JarList();
00217 
00218         //Set the list of libraries
00219         libraries = new JarList();
00220 
00221         //add the ejbs, wars and clients to this list
00222         toParse.merge(ejbs);
00223         toParse.merge(wars);
00224         toParse.merge(clients);
00225 
00226         //dependencies list
00227         JarList lstOfFilesDep = new JarList();
00228 
00229         //Url of the current filename
00230         URL depUrl = null;
00231 
00232         //While there are elements to analyse
00233         while (toParse.size() > 0) {
00234 
00235             //File to look for Manifest
00236             String fileName = (String) toParse.firstElement();
00237 
00238             if (fileName.endsWith("/")) {
00239                 throw new EarClassPathManagerException("In j2ee application, Class-Path with directory is forbidden. '"
00240                         + fileName + "' is not authorized.");
00241             }
00242             try {
00243                 //Get dependency entries
00244                 depUrl = new URL(directory.toExternalForm() + "/" + fileName);
00245                 lstOfFilesDep = getManifestClassPath(depUrl);
00246             } catch (MalformedURLException mue) {
00247                 lstOfFilesDep.removeAllElements();
00248                 throw new EarClassPathManagerException("Error while trying to get the url for "
00249                         + directory.toExternalForm() + File.separator + fileName + " : " + mue.getMessage());
00250             } catch (IOException ioe) {
00251                 lstOfFilesDep.removeAllElements();
00252                 throw new EarClassPathManagerException("Error while reading manifest file from the file " + fileName
00253                         + " : " + ioe.getMessage());
00254             }
00255 
00256             String parentDir = new File(fileName).getParent();
00257             //subDirectory (the parent dir or "")
00258             String subDir = null;
00259             if (parentDir != null) {
00260                 subDir = parentDir;
00261             } else {
00262                 subDir = "";
00263             }
00264 
00265             //Set the relative path of EAR / file
00266             lstOfFilesDep.setRelativePath(subDir);
00267 
00268             //Merge the list
00269             toParse.merge(lstOfFilesDep);
00270 
00271             //Add the parsed file
00272             parsed.add(fileName);
00273 
00274             //Add to the libraries if it's not an EJB or a WEB application
00275             if (isALibrary(fileName)) {
00276                 libraries.add(fileName);
00277             }
00278 
00279             //Remove the fileName
00280             toParse.remove(fileName);
00281 
00282         }
00283 
00284         //We've got the list of files, its the JarList : parsed
00285         try {
00286             urls = libraries.getURLs(directory.toExternalForm());
00287         } catch (JarListException e) {
00288             throw new EarClassPathManagerException(
00289                     "Error while geting the URLs of the jars files which must be loaded at the EAR level");
00290 
00291         }
00292 
00293     }
00294 
00301     private boolean isALibrary(String fileName) {
00302         return (!ejbs.contains(fileName) && !wars.contains(fileName));
00303     }
00304 
00305 }

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