KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > util > ClassPath


1 package com.sun.org.apache.bcel.internal.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.*;
58 import java.util.zip.*;
59 import java.io.*;
60
61 /**
62  * Responsible for loading (class) files from the CLASSPATH. Inspired by
63  * sun.tools.ClassPath.
64  *
65  * @version $Id: ClassPath.java,v 1.1 2003/12/12 09:01:32 rameshm Exp $
66  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
67  */

68 public class ClassPath {
69   private PathEntry[] paths;
70
71   /**
72    * Search for classes in given path.
73    */

74   public ClassPath(String JavaDoc class_path) {
75     ArrayList vec = new ArrayList();
76
77     for(StringTokenizer tok=new StringTokenizer(class_path,
78                         java.io.File.pathSeparator);
79     tok.hasMoreTokens();)
80     {
81       String JavaDoc path = tok.nextToken();
82       
83       if(!path.equals("")) {
84     File file = new File(path);
85
86     try {
87       if(file.exists()) {
88         if(file.isDirectory())
89           vec.add(new Dir(path));
90         else
91           vec.add(new Zip(new ZipFile(file)));
92       }
93     } catch(IOException e) {
94       System.err.println("CLASSPATH component " + file + ": " + e);
95     }
96       }
97     }
98
99     paths = new PathEntry[vec.size()];
100     vec.toArray(paths);
101   }
102
103   /**
104    * Search for classes in CLASSPATH.
105    */

106   public ClassPath() {
107     this(getClassPath());
108   }
109
110   private static final void getPathComponents(String JavaDoc path, ArrayList list) {
111     if(path != null) {
112       StringTokenizer tok = new StringTokenizer(path, File.pathSeparator);
113
114       while(tok.hasMoreTokens()) {
115         String JavaDoc name = tok.nextToken();
116         File file = new File(name);
117
118     if(file.exists())
119       list.add(name);
120       }
121     }
122   }
123
124   private static final String JavaDoc getClassPath() {
125     String JavaDoc class_path = System.getProperty("java.class.path");
126     String JavaDoc boot_path = System.getProperty("sun.boot.class.path");
127     String JavaDoc ext_path = System.getProperty("java.ext.dirs");
128
129     ArrayList list = new ArrayList();
130
131     getPathComponents(class_path, list);
132     getPathComponents(boot_path, list);
133
134     ArrayList dirs = new ArrayList();
135     getPathComponents(ext_path, dirs);
136
137     for(Iterator e = dirs.iterator(); e.hasNext(); ) {
138       File ext_dir = new File((String JavaDoc)e.next());
139       String JavaDoc[] extensions = ext_dir.list(new FilenameFilter() {
140     public boolean accept(File dir, String JavaDoc name) {
141       name = name.toLowerCase();
142       return name.endsWith(".zip") || name.endsWith(".jar");
143     }
144       });
145
146       if(extensions != null)
147     for(int i=0; i < extensions.length; i++)
148       list.add(ext_path + File.separatorChar + extensions[i]);
149     }
150
151     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
152
153     for(Iterator e = list.iterator(); e.hasNext(); ) {
154       buf.append((String JavaDoc)e.next());
155
156       if(e.hasNext())
157     buf.append(File.pathSeparatorChar);
158     }
159
160     return buf.toString();
161   }
162
163   /**
164    * @param name fully qualified class name, e.g. java.lang.String
165    * @return input stream for class
166    */

167   public InputStream getInputStream(String JavaDoc name) throws IOException {
168     return getInputStream(name, ".class");
169   }
170     
171   /**
172    * @param name fully qualified file name, e.g. java/lang/String
173    * @param suffix file name ends with suff, e.g. .java
174    * @return input stream for file on class path
175    */

176   public InputStream getInputStream(String JavaDoc name, String JavaDoc suffix) throws IOException {
177     return getClassFile(name, suffix).getInputStream();
178   }
179
180   /**
181    * @param name fully qualified file name, e.g. java/lang/String
182    * @param suffix file name ends with suff, e.g. .java
183    * @return class file for the java class
184    */

185   public ClassFile getClassFile(String JavaDoc name, String JavaDoc suffix) throws IOException {
186     for(int i=0; i < paths.length; i++) {
187       ClassFile cf;
188
189       if((cf = paths[i].getClassFile(name, suffix)) != null)
190     return cf;
191     }
192
193     throw new IOException("Couldn't find: " + name + suffix);
194   }
195
196   /**
197    * @param name fully qualified class name, e.g. java.lang.String
198    * @return input stream for class
199    */

200   public ClassFile getClassFile(String JavaDoc name) throws IOException {
201     return getClassFile(name, ".class");
202   }
203
204   /**
205    * @param name fully qualified file name, e.g. java/lang/String
206    * @param suffix file name ends with suffix, e.g. .java
207    * @return byte array for file on class path
208    */

209   public byte[] getBytes(String JavaDoc name, String JavaDoc suffix) throws IOException {
210     InputStream is = getInputStream(name, suffix);
211     
212     if(is == null)
213       throw new IOException("Couldn't find: " + name + suffix);
214
215     DataInputStream dis = new DataInputStream(is);
216     byte[] bytes = new byte[is.available()];
217     dis.readFully(bytes);
218     dis.close(); is.close();
219
220     return bytes;
221   }
222
223   /**
224    * @return byte array for class
225    */

226   public byte[] getBytes(String JavaDoc name) throws IOException {
227     return getBytes(name, ".class");
228   }
229
230   /**
231    * @param name name of file to search for, e.g. java/lang/String.java
232    * @return full (canonical) path for file
233    */

234   public String JavaDoc getPath(String JavaDoc name) throws IOException {
235     int index = name.lastIndexOf('.');
236     String JavaDoc suffix = "";
237
238     if(index > 0) {
239       suffix = name.substring(index);
240       name = name.substring(0, index);
241     }
242     
243     return getPath(name, suffix);
244   }
245
246   /**
247    * @param name name of file to search for, e.g. java/lang/String
248    * @param suffix file name suffix, e.g. .java
249    * @return full (canonical) path for file, if it exists
250    */

251   public String JavaDoc getPath(String JavaDoc name, String JavaDoc suffix) throws IOException {
252     return getClassFile(name, suffix).getPath();
253   }
254
255   private static abstract class PathEntry {
256     abstract ClassFile getClassFile(String JavaDoc name, String JavaDoc suffix) throws IOException;
257   }
258
259   /** Contains information about file/ZIP entry of the Java class.
260    */

261   public abstract static class ClassFile {
262     /** @return input stream for class file.
263      */

264     public abstract InputStream getInputStream() throws IOException;
265
266     /** @return canonical path to class file.
267      */

268     public abstract String JavaDoc getPath();
269
270     /** @return modification time of class file.
271      */

272     public abstract long getTime();
273
274     /** @return size of class file.
275      */

276     public abstract long getSize();
277   }
278     
279   private static class Dir extends PathEntry {
280     private String JavaDoc dir;
281
282     Dir(String JavaDoc d) { dir = d; }
283
284     ClassFile getClassFile(String JavaDoc name, String JavaDoc suffix) throws IOException {
285       final File file = new File(dir + File.separatorChar +
286                  name.replace('.', File.separatorChar) + suffix);
287       
288       return file.exists()? new ClassFile() {
289     public InputStream getInputStream() throws IOException { return new FileInputStream(file); }
290
291     public String JavaDoc getPath() { try {
292       return file.getCanonicalPath();
293     } catch(IOException e) { return null; }
294
295     }
296     public long getTime() { return file.lastModified(); }
297     public long getSize() { return file.length(); }
298       } : null;
299     }
300
301     public String JavaDoc toString() { return dir; }
302   }
303
304   private static class Zip extends PathEntry {
305     private ZipFile zip;
306
307     Zip(ZipFile z) { zip = z; }
308
309     ClassFile getClassFile(String JavaDoc name, String JavaDoc suffix) throws IOException {
310       final ZipEntry entry = zip.getEntry(name.replace('.', '/') + suffix);
311
312       return (entry != null)? new ClassFile() {
313     public InputStream getInputStream() throws IOException { return zip.getInputStream(entry); }
314     public String JavaDoc getPath() { return entry.toString(); }
315     public long getTime() { return entry.getTime(); }
316     public long getSize() { return entry.getSize(); }
317       } : null;
318     }
319   }
320 }
321
322
323
Popular Tags