1 20 package org.apache.axis.utils; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.FileFilter ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.net.URLDecoder ; 30 import java.util.StringTokenizer ; 31 import java.util.jar.Attributes ; 32 import java.util.jar.JarFile ; 33 import java.util.jar.JarInputStream ; 34 import java.util.jar.Manifest ; 35 36 import org.apache.axis.AxisProperties; 37 import org.apache.axis.MessageContext; 38 import org.apache.axis.transport.http.HTTPConstants; 39 40 43 public class ClasspathUtils { 44 45 56 public static String expandDirs(String dirPaths) { 57 StringTokenizer st = new StringTokenizer (dirPaths, File.pathSeparator); 58 StringBuffer buffer = new StringBuffer (); 59 while (st.hasMoreTokens()) { 60 String d = st.nextToken(); 61 File dir = new File (d); 62 if (dir.isDirectory()) { 63 File [] files = dir.listFiles(new JavaArchiveFilter()); 64 for (int i = 0; i < files.length; i++) { 65 buffer.append(files[i]).append(File.pathSeparator); 66 } 67 } 68 } 69 return buffer.toString(); 70 } 71 72 77 public static boolean isJar(InputStream is) { 78 try { 79 JarInputStream jis = new JarInputStream (is); 80 if (jis.getNextEntry() != null) { 81 return true; 82 } 83 } catch (IOException ioe) { 84 } 85 return false; 86 } 87 88 93 public static String getDefaultClasspath(MessageContext msgContext) { 94 StringBuffer classpath = new StringBuffer (); 95 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 96 fillClassPath(cl, classpath); 97 98 101 String webBase = (String ) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETLOCATION); 102 if (webBase != null) { 103 classpath.append(webBase + File.separatorChar + "classes" + 104 File.pathSeparatorChar); 105 try { 106 String libBase = webBase + File.separatorChar + "lib"; 107 File libDir = new File (libBase); 108 String [] jarFiles = libDir.list(); 109 for (int i = 0; i < jarFiles.length; i++) { 110 String jarFile = jarFiles[i]; 111 if (jarFile.endsWith(".jar")) { 112 classpath.append(libBase + 113 File.separatorChar + 114 jarFile + 115 File.pathSeparatorChar); 116 } 117 } 118 } catch (Exception e) { 119 } 121 } 122 123 getClassPathFromDirectoryProperty(classpath, "axis.ext.dirs"); 125 126 getClassPathFromProperty(classpath, "org.apache.catalina.jsp_classpath"); 128 129 getClassPathFromProperty(classpath, "ws.ext.dirs"); 131 getClassPathFromProperty(classpath, "com.ibm.websphere.servlet.application.classpath"); 132 133 getClassPathFromProperty(classpath, "java.class.path"); 135 136 getClassPathFromDirectoryProperty(classpath, "java.ext.dirs"); 138 139 getClassPathFromProperty(classpath, "sun.boot.class.path"); 141 return classpath.toString(); 142 } 143 144 149 private static void getClassPathFromDirectoryProperty(StringBuffer classpath, String property) { 150 String dirs = AxisProperties.getProperty(property); 151 String path = null; 152 try { 153 path = ClasspathUtils.expandDirs(dirs); 154 } catch (Exception e) { 155 } 157 if (path != null) { 158 classpath.append(path); 159 classpath.append(File.pathSeparatorChar); 160 } 161 } 162 163 168 private static void getClassPathFromProperty(StringBuffer classpath, String property) { 169 String path = AxisProperties.getProperty(property); 170 if (path != null) { 171 classpath.append(path); 172 classpath.append(File.pathSeparatorChar); 173 } 174 } 175 176 181 private static void fillClassPath(ClassLoader cl, StringBuffer classpath) { 182 while (cl != null) { 183 if (cl instanceof URLClassLoader ) { 184 URL [] urls = ((URLClassLoader ) cl).getURLs(); 185 for (int i = 0; (urls != null) && i < urls.length; i++) { 186 String path = urls[i].getPath(); 187 if (path.length() >= 3 && path.charAt(0) == '/' && path.charAt(2) == ':') 189 path = path.substring(1); 190 classpath.append(URLDecoder.decode(path)); 191 classpath.append(File.pathSeparatorChar); 192 193 File file = new File (urls[i].getFile()); 195 if (file.isFile()) { 196 FileInputStream fis = null; 197 try { 198 fis = new FileInputStream (file); 199 if (isJar(fis)) { 200 JarFile jar = new JarFile (file); 201 Manifest manifest = jar.getManifest(); 202 if (manifest != null) { 203 Attributes attributes = manifest.getMainAttributes(); 204 if (attributes != null) { 205 String s = attributes.getValue(Attributes.Name.CLASS_PATH); 206 String base = file.getParent(); 207 if (s != null) { 208 StringTokenizer st = new StringTokenizer (s, " "); 209 while (st.hasMoreTokens()) { 210 String t = st.nextToken(); 211 classpath.append(base + File.separatorChar + t); 212 classpath.append(File.pathSeparatorChar); 213 } 214 } 215 } 216 } 217 } 218 } catch (IOException ioe) { 219 if (fis != null) 220 try { 221 fis.close(); 222 } catch (IOException ioe2) { 223 } 224 } 225 } 226 } 227 } 228 cl = cl.getParent(); 229 } 230 } 231 232 235 private static class JavaArchiveFilter implements FileFilter { 236 public boolean accept(File file) { 237 String name = file.getName().toLowerCase(); 238 return (name.endsWith(".jar") || name.endsWith(".zip")); 239 } 240 } 241 } 242 | Popular Tags |