KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > ClasspathUtils


1 /*
2  * ClasspathUtils.java
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */

20 package org.apache.axis.utils;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.FileFilter JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.net.URLDecoder JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.jar.Attributes JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.jar.JarInputStream JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35
36 import org.apache.axis.AxisProperties;
37 import org.apache.axis.MessageContext;
38 import org.apache.axis.transport.http.HTTPConstants;
39
40 /**
41  * Utility class for constructing the classpath
42  */

43 public class ClasspathUtils {
44
45     /**
46      * Expand a directory path or list of directory paths (File.pathSeparator
47      * delimited) into a list of file paths of all the jar files in those
48      * directories.
49      *
50      * @param dirPaths The string containing the directory path or list of
51      * directory paths.
52      * @return The file paths of the jar files in the directories. This is an
53      * empty string if no files were found, and is terminated by an
54      * additional pathSeparator in all other cases.
55      */

56     public static String JavaDoc expandDirs(String JavaDoc dirPaths) {
57         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dirPaths, File.pathSeparator);
58         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59         while (st.hasMoreTokens()) {
60             String JavaDoc d = st.nextToken();
61             File JavaDoc dir = new File JavaDoc(d);
62             if (dir.isDirectory()) {
63                 File JavaDoc[] 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     /**
73      * Check if this inputstream is a jar/zip
74      * @param is
75      * @return true if inputstream is a jar
76      */

77     public static boolean isJar(InputStream JavaDoc is) {
78         try {
79             JarInputStream JavaDoc jis = new JarInputStream JavaDoc(is);
80             if (jis.getNextEntry() != null) {
81                 return true;
82             }
83         } catch (IOException JavaDoc ioe) {
84         }
85         return false;
86     }
87
88     /**
89      * Get the default classpath from various thingies in the message context
90      * @param msgContext
91      * @return default classpath
92      */

93     public static String JavaDoc getDefaultClasspath(MessageContext msgContext) {
94         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
95         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
96         fillClassPath(cl, classpath);
97
98         // Just to be safe (the above doesn't seem to return the webapp
99
// classpath in all cases), manually do this:
100

101         String JavaDoc webBase = (String JavaDoc) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETLOCATION);
102         if (webBase != null) {
103             classpath.append(webBase + File.separatorChar + "classes" +
104                     File.pathSeparatorChar);
105             try {
106                 String JavaDoc libBase = webBase + File.separatorChar + "lib";
107                 File JavaDoc libDir = new File JavaDoc(libBase);
108                 String JavaDoc[] jarFiles = libDir.list();
109                 for (int i = 0; i < jarFiles.length; i++) {
110                     String JavaDoc 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 JavaDoc e) {
119                 // Oh well. No big deal.
120
}
121         }
122
123         // axis.ext.dirs can be used in any appserver
124
getClassPathFromDirectoryProperty(classpath, "axis.ext.dirs");
125
126         // classpath used by Jasper
127
getClassPathFromProperty(classpath, "org.apache.catalina.jsp_classpath");
128         
129         // websphere stuff.
130
getClassPathFromProperty(classpath, "ws.ext.dirs");
131         getClassPathFromProperty(classpath, "com.ibm.websphere.servlet.application.classpath");
132         
133         // java class path
134
getClassPathFromProperty(classpath, "java.class.path");
135         
136         // Load jars from java external directory
137
getClassPathFromDirectoryProperty(classpath, "java.ext.dirs");
138         
139         // boot classpath isn't found in above search
140
getClassPathFromProperty(classpath, "sun.boot.class.path");
141         return classpath.toString();
142     }
143
144     /**
145      * Add all files in the specified directory to the classpath
146      * @param classpath
147      * @param property
148      */

149     private static void getClassPathFromDirectoryProperty(StringBuffer JavaDoc classpath, String JavaDoc property) {
150         String JavaDoc dirs = AxisProperties.getProperty(property);
151         String JavaDoc path = null;
152         try {
153             path = ClasspathUtils.expandDirs(dirs);
154         } catch (Exception JavaDoc e) {
155             // Oh well. No big deal.
156
}
157         if (path != null) {
158             classpath.append(path);
159             classpath.append(File.pathSeparatorChar);
160         }
161     }
162
163     /**
164      * Add a classpath stored in a property.
165      * @param classpath
166      * @param property
167      */

168     private static void getClassPathFromProperty(StringBuffer JavaDoc classpath, String JavaDoc property) {
169         String JavaDoc path = AxisProperties.getProperty(property);
170         if (path != null) {
171             classpath.append(path);
172             classpath.append(File.pathSeparatorChar);
173         }
174     }
175
176     /**
177      * Walk the classloader hierarchy and add to the classpath
178      * @param cl
179      * @param classpath
180      */

181     private static void fillClassPath(ClassLoader JavaDoc cl, StringBuffer JavaDoc classpath) {
182         while (cl != null) {
183             if (cl instanceof URLClassLoader JavaDoc) {
184                 URL JavaDoc[] urls = ((URLClassLoader JavaDoc) cl).getURLs();
185                 for (int i = 0; (urls != null) && i < urls.length; i++) {
186                     String JavaDoc path = urls[i].getPath();
187                     //If it is a drive letter, adjust accordingly.
188
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                     // if its a jar extract Class-Path entries from manifest
194
File JavaDoc file = new File JavaDoc(urls[i].getFile());
195                     if (file.isFile()) {
196                         FileInputStream JavaDoc fis = null;
197                         try {
198                             fis = new FileInputStream JavaDoc(file);
199                             if (isJar(fis)) {
200                                 JarFile JavaDoc jar = new JarFile JavaDoc(file);
201                                 Manifest JavaDoc manifest = jar.getManifest();
202                                 if (manifest != null) {
203                                     Attributes JavaDoc attributes = manifest.getMainAttributes();
204                                     if (attributes != null) {
205                                         String JavaDoc s = attributes.getValue(Attributes.Name.CLASS_PATH);
206                                         String JavaDoc base = file.getParent();
207                                         if (s != null) {
208                                             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, " ");
209                                             while (st.hasMoreTokens()) {
210                                                 String JavaDoc t = st.nextToken();
211                                                 classpath.append(base + File.separatorChar + t);
212                                                 classpath.append(File.pathSeparatorChar);
213                                             }
214                                         }
215                                     }
216                                 }
217                             }
218                         } catch (IOException JavaDoc ioe) {
219                             if (fis != null)
220                                 try {
221                                     fis.close();
222                                 } catch (IOException JavaDoc ioe2) {
223                                 }
224                         }
225                     }
226                 }
227             }
228             cl = cl.getParent();
229         }
230     }
231
232     /**
233      * Filter for zip/jar
234      */

235     private static class JavaArchiveFilter implements FileFilter JavaDoc {
236         public boolean accept(File JavaDoc file) {
237             String JavaDoc name = file.getName().toLowerCase();
238             return (name.endsWith(".jar") || name.endsWith(".zip"));
239         }
240     }
241 }
242
Popular Tags