FileDocCategorySizeDatePackage
ClasspathUtils.javaAPI DocApache Axis 1.49277Sat Apr 22 18:57:28 BST 2006org.apache.axis.utils

ClasspathUtils

public class ClasspathUtils extends Object
Utility class for constructing the classpath

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringexpandDirs(java.lang.String dirPaths)
Expand a directory path or list of directory paths (File.pathSeparator delimited) into a list of file paths of all the jar files in those directories.

param
dirPaths The string containing the directory path or list of directory paths.
return
The file paths of the jar files in the directories. This is an empty string if no files were found, and is terminated by an additional pathSeparator in all other cases.

        StringTokenizer st = new StringTokenizer(dirPaths, File.pathSeparator);
        StringBuffer buffer = new StringBuffer();
        while (st.hasMoreTokens()) {
            String d = st.nextToken();
            File dir = new File(d);
            if (dir.isDirectory()) {
                File[] files = dir.listFiles(new JavaArchiveFilter());
                for (int i = 0; i < files.length; i++) {
                    buffer.append(files[i]).append(File.pathSeparator);
                }
            }
        }
        return buffer.toString();
    
private static voidfillClassPath(java.lang.ClassLoader cl, java.lang.StringBuffer classpath)
Walk the classloader hierarchy and add to the classpath

param
cl
param
classpath

        while (cl != null) {
            if (cl instanceof URLClassLoader) {
                URL[] urls = ((URLClassLoader) cl).getURLs();
                for (int i = 0; (urls != null) && i < urls.length; i++) {
                    String path = urls[i].getPath();
                    //If it is a drive letter, adjust accordingly.
                    if (path.length() >= 3 && path.charAt(0) == '/" && path.charAt(2) == ':")
                        path = path.substring(1);
                    classpath.append(URLDecoder.decode(path));
                    classpath.append(File.pathSeparatorChar);

                    // if its a jar extract Class-Path entries from manifest
                    File file = new File(urls[i].getFile());
                    if (file.isFile()) {
                        FileInputStream fis = null;
                        try {
                            fis = new FileInputStream(file);
                            if (isJar(fis)) {
                                JarFile jar = new JarFile(file);
                                Manifest manifest = jar.getManifest();
                                if (manifest != null) {
                                    Attributes attributes = manifest.getMainAttributes();
                                    if (attributes != null) {
                                        String s = attributes.getValue(Attributes.Name.CLASS_PATH);
                                        String base = file.getParent();
                                        if (s != null) {
                                            StringTokenizer st = new StringTokenizer(s, " ");
                                            while (st.hasMoreTokens()) {
                                                String t = st.nextToken();
                                                classpath.append(base + File.separatorChar + t);
                                                classpath.append(File.pathSeparatorChar);
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (IOException ioe) {
                        } finally {
                            if (fis != null) {
                                try {
                                    fis.close();
                                } catch (IOException ioe2) {
                                }
                            }
                        }
                    }
                }
            }
            cl = cl.getParent();
        }
    
private static voidgetClassPathFromDirectoryProperty(java.lang.StringBuffer classpath, java.lang.String property)
Add all files in the specified directory to the classpath

param
classpath
param
property

        String dirs = AxisProperties.getProperty(property);
        String path = null;
        try {
            path = ClasspathUtils.expandDirs(dirs);
        } catch (Exception e) {
            // Oh well.  No big deal.
        }
        if (path != null) {
            classpath.append(path);
            classpath.append(File.pathSeparatorChar);
        }
    
private static voidgetClassPathFromProperty(java.lang.StringBuffer classpath, java.lang.String property)
Add a classpath stored in a property.

param
classpath
param
property

        String path = AxisProperties.getProperty(property);
        if (path != null) {
            classpath.append(path);
            classpath.append(File.pathSeparatorChar);
        }
    
public static java.lang.StringgetDefaultClasspath(org.apache.axis.MessageContext msgContext)
Get the default classpath from various thingies in the message context

param
msgContext
return
default classpath

        StringBuffer classpath = new StringBuffer();
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        fillClassPath(cl, classpath);

        // Just to be safe (the above doesn't seem to return the webapp
        // classpath in all cases), manually do this:

        String webBase = (String) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETLOCATION);
        if (webBase != null) {
            classpath.append(webBase + File.separatorChar + "classes" +
                    File.pathSeparatorChar);
            try {
                String libBase = webBase + File.separatorChar + "lib";
                File libDir = new File(libBase);
                String[] jarFiles = libDir.list();
                for (int i = 0; i < jarFiles.length; i++) {
                    String jarFile = jarFiles[i];
                    if (jarFile.endsWith(".jar")) {
                        classpath.append(libBase +
                                File.separatorChar +
                                jarFile +
                                File.pathSeparatorChar);
                    }
                }
            } catch (Exception e) {
                // Oh well.  No big deal.
            }
        }

        // axis.ext.dirs can be used in any appserver
        getClassPathFromDirectoryProperty(classpath, "axis.ext.dirs");

        // classpath used by Jasper 
        getClassPathFromProperty(classpath, "org.apache.catalina.jsp_classpath");
        
        // websphere stuff.
        getClassPathFromProperty(classpath, "ws.ext.dirs");
        getClassPathFromProperty(classpath, "com.ibm.websphere.servlet.application.classpath");
        
        // java class path
        getClassPathFromProperty(classpath, "java.class.path");
        
        // Load jars from java external directory
        getClassPathFromDirectoryProperty(classpath, "java.ext.dirs");
        
        // boot classpath isn't found in above search
        getClassPathFromProperty(classpath, "sun.boot.class.path");
        return classpath.toString();
    
public static booleanisJar(java.io.InputStream is)
Check if this inputstream is a jar/zip

param
is
return
true if inputstream is a jar

        try {
            JarInputStream jis = new JarInputStream(is);
            if (jis.getNextEntry() != null) {
                return true;
            }
        } catch (IOException ioe) {
        }
        return false;