View Javadoc

1   /*
2    * $Id ClassPathUtils.java $
3    *
4    */
5   
6   package org.astrogrid.utils;
7   
8   
9   /***
10   * A set of methods for examining and checking the classpath
11   *
12   * @author M Hill
13   */
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.util.StringTokenizer;
18  import java.util.Vector;
19  import java.util.jar.JarFile;
20  
21  public class ClassPathUtils
22  {
23     
24     /***
25      * Examines class path for jar files that do not exist (as files) and
26      * directory paths that are not directories...
27      * <p>
28      * @return a list of invalid paths and jar files - null if there are none
29      */
30     public static String[] getInvalidPaths()
31     {
32        String[] paths = getClassPathList();
33        Vector invalidPaths = new Vector();
34        
35        for (int i=0; i<paths.length;i++)
36        {
37           String path = paths[i];
38           
39           if (path.toLowerCase().endsWith(".jar"))
40           {
41              String error = isJarFileValid(path);
42              if (error != null) {
43                 invalidPaths.add(path+" ("+error+")");
44              }
45           }
46           else
47           {
48              String error = isDirValid(path);
49              if (error != null) {
50                 invalidPaths.add(path+" ("+error+")");
51              }
52           }
53        }
54  
55        if (invalidPaths.size() == 0) {
56           return null;
57        } else {
58           return (String[]) invalidPaths.toArray(new String[] {});
59        }
60     }
61  
62     /***
63      * Checks that the given filename is a valid jar file. Returns null if
64      * it is valid, or a string describing the reason if it is invalid.
65      */
66     public static String isJarFileValid(String filename)
67     {
68        try
69        {
70           JarFile jarFile = new JarFile(new File(filename));
71  
72           //not sure of the performance hit of this Enumeration entries = jarFile.entries();
73           
74           return null;
75        }
76        catch (IOException e)
77        {
78           return e.getMessage();
79        }
80     }
81     
82     /***
83      * Checks that the given path is a valid directory. Returns null if
84      * it is valid, or a string describing the reason if it is invalid.
85      */
86     public static String isDirValid(String dirname)
87     {
88        File file = new File(dirname);
89        
90        if (!file.exists())
91        {
92           return "Does not exist";
93        }
94        
95        if (!file.isDirectory())
96        {
97           return "Not a directory";
98        }
99  
100       return null;
101    }
102    
103 
104    /***
105     * Returns an array of the class path elements, ie directories and
106     * jar files
107     */
108    public static String[] getClassPathList()
109    {
110       String path = getClassPath();
111       StringTokenizer tokenizer = new StringTokenizer(path, File.pathSeparator);
112       Vector list = new Vector();
113       
114       while (tokenizer.hasMoreElements())
115       {
116          list.add(tokenizer.nextToken());
117       }
118       
119       return (String[]) list.toArray(new String[] {});
120    }
121    
122    /***
123     * Returns the classpath as it is stored - in one long string of files
124     * and directories separated by a platform specific character, eg colon
125     * or semicolon.
126     */
127    public static String getClassPath()
128    {
129       return System.getProperty("java.class.path");
130    }
131    
132    
133    /***
134     * Test harness - this can be run on its own to test the classpath
135     * setup
136     */
137    public static void main(String[] args)
138    {
139       String[] invalidPaths = getInvalidPaths();
140       
141       if (invalidPaths == null) {
142          System.out.println("all paths are valid");
143       }
144       else {
145       
146          for (int i=0;i<invalidPaths.length;i++)
147          {
148             System.out.println(invalidPaths[i]);
149          }
150       }
151       
152    }
153 }
154 
155 /*
156 $Log: ClassPathUtils.java,v $
157 Revision 1.1  2005/03/22 13:00:41  mch
158 Seperated utils from common
159 
160 Revision 1.2  2003/11/13 22:14:56  mch
161 Removed unused import
162 
163 Revision 1.1  2003/09/24 18:32:22  mch
164 Tool for checking classpath
165 
166 */