1 26 27 package org.objectweb.jonas.ear.lib; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.util.StringTokenizer ; 35 import java.util.jar.Attributes ; 36 import java.util.jar.JarFile ; 37 import java.util.jar.Manifest ; 38 39 47 public class EarClassPathManager { 48 49 52 private URL [] urls = null; 53 54 57 private JarList toParse = null; 58 59 62 private JarList parsed = null; 63 64 67 private JarList libraries = null; 68 69 72 private JarList ejbs = null; 73 74 77 private JarList wars = null; 78 79 82 private JarList clients = null; 83 84 87 private URL directory = null; 88 89 96 public EarClassPathManager(JarList ejbs, JarList wars, URL directory) throws EarClassPathManagerException { 97 98 if ((ejbs == null) || (wars == null) || (directory == null)) { 99 throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters"); 100 } 101 102 if (!directory.getProtocol().equalsIgnoreCase("file")) { 104 throw new EarClassPathManagerException("Only the file:/ URL can be used"); 105 } 106 this.ejbs = ejbs; 107 this.wars = wars; 108 this.clients = new JarList(); 109 this.directory = directory; 110 } 111 112 118 public EarClassPathManager(JarList clients, URL directory) throws EarClassPathManagerException { 119 120 if ((clients == null) || (directory == null)) { 121 throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters"); 122 } 123 124 if (!directory.getProtocol().equalsIgnoreCase("file")) { 126 throw new EarClassPathManagerException("Only the file:/ URL can be used"); 127 } 128 this.ejbs = new JarList(); 129 this.wars = new JarList(); 130 this.clients = clients; 131 this.directory = directory; 132 } 133 134 142 private JarList getManifestClassPath(URL url) throws EarClassPathManagerException, IOException { 143 144 if (url == null) { 145 throw new EarClassPathManagerException("JarList.getManifestClassPath : The url parameter can't be null"); 146 } 147 148 Manifest manifest = null; 149 150 if (new File (url.getFile()).isDirectory()) { 151 File manifestFile = new File (url.getFile() + File.separator + JarFile.MANIFEST_NAME); 152 if (manifestFile.exists()) { 153 manifest = new Manifest (new FileInputStream (manifestFile)); 154 } 155 156 } else { 157 JarFile jarFile = new JarFile (url.getFile()); 160 161 manifest = jarFile.getManifest(); 163 } 164 165 String classPath = null; 167 168 if (manifest != null) { 170 Attributes attributes = manifest.getMainAttributes(); 172 classPath = attributes.getValue(Attributes.Name.CLASS_PATH); 173 } 174 175 JarList jarList = null; 177 178 if (classPath != null) { 181 jarList = new JarList(new StringTokenizer (classPath)); 182 } else { 183 jarList = new JarList(); 184 } 185 186 return jarList; 188 } 189 190 195 public URL [] getResolvedClassPath() throws EarClassPathManagerException { 196 197 if (urls == null) { 199 resolveClassPath(); 200 } 201 202 return urls; 204 } 205 206 210 private void resolveClassPath() throws EarClassPathManagerException { 211 212 toParse = new JarList(); 214 215 parsed = new JarList(); 217 218 libraries = new JarList(); 220 221 toParse.merge(ejbs); 223 toParse.merge(wars); 224 toParse.merge(clients); 225 226 JarList lstOfFilesDep = new JarList(); 228 229 URL depUrl = null; 231 232 while (toParse.size() > 0) { 234 235 String fileName = (String ) toParse.firstElement(); 237 238 if (fileName.endsWith("/")) { 239 throw new EarClassPathManagerException("In j2ee application, Class-Path with directory is forbidden. '" 240 + fileName + "' is not authorized."); 241 } 242 try { 243 depUrl = new URL (directory.toExternalForm() + "/" + fileName); 245 lstOfFilesDep = getManifestClassPath(depUrl); 246 } catch (MalformedURLException mue) { 247 lstOfFilesDep.removeAllElements(); 248 throw new EarClassPathManagerException("Error while trying to get the url for " 249 + directory.toExternalForm() + File.separator + fileName + " : " + mue.getMessage()); 250 } catch (IOException ioe) { 251 lstOfFilesDep.removeAllElements(); 252 throw new EarClassPathManagerException("Error while reading manifest file from the file " + fileName 253 + " : " + ioe.getMessage()); 254 } 255 256 String parentDir = new File (fileName).getParent(); 257 String subDir = null; 259 if (parentDir != null) { 260 subDir = parentDir; 261 } else { 262 subDir = ""; 263 } 264 265 lstOfFilesDep.setRelativePath(subDir); 267 268 toParse.merge(lstOfFilesDep); 270 271 parsed.add(fileName); 273 274 if (isALibrary(fileName)) { 276 libraries.add(fileName); 277 } 278 279 toParse.remove(fileName); 281 282 } 283 284 try { 286 urls = libraries.getURLs(directory.toExternalForm()); 287 } catch (JarListException e) { 288 throw new EarClassPathManagerException( 289 "Error while geting the URLs of the jars files which must be loaded at the EAR level"); 290 291 } 292 293 } 294 295 301 private boolean isALibrary(String fileName) { 302 return (!ejbs.contains(fileName) && !wars.contains(fileName)); 303 } 304 305 } | Popular Tags |