KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > classloader > ClassPath


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ClassPath.java 98 2006-02-24 16:18:48Z gblondelle $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.classloader;
23
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.io.FilenameFilter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33
34 /**
35  * @author Rafael Marins
36  * @version $Rev: 98 $ $Date: 2006-02-24 16:18:48Z $
37  */

38 public class ClassPath {
39
40     private static class DirectoryFilter implements FileFilter JavaDoc {
41     
42         /**
43          * @see java.io.FileFilter#accept(java.io.File)
44          */

45         public boolean accept(File JavaDoc pathname) {
46             return pathname.isDirectory();
47         }
48         
49     }
50
51     private static class ExtensionFileFilter implements FilenameFilter JavaDoc {
52     
53         private String JavaDoc[] extensions;
54     
55         public ExtensionFileFilter(String JavaDoc[] exts) {
56             super();
57             this.extensions = exts;
58         }
59     
60         /**
61          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
62          */

63         public boolean accept(File JavaDoc dir, String JavaDoc name) {
64     
65             boolean accept = false;
66     
67             String JavaDoc lcName = name.toLowerCase();
68     
69             for (int i=0; i<extensions.length; i++) {
70                 String JavaDoc x = extensions[i];
71                 if (lcName.endsWith(x)) {
72                     accept = true;
73                     break;
74                 }
75             }
76     
77             return accept;
78         }
79         
80     }
81
82     private List JavaDoc<File JavaDoc> pathComponents = new ArrayList JavaDoc<File JavaDoc>();
83
84     private boolean recursiveScanner = false;
85
86     /**
87      *
88      */

89     public ClassPath() {
90         super();
91     }
92
93     public ClassPath(boolean recursive) {
94         super();
95         this.recursiveScanner = recursive;
96     }
97
98     public void setClasspath(String JavaDoc classpath) {
99         pathComponents.clear();
100
101         String JavaDoc[] elements = classpath.split(File.pathSeparator);
102         setClasspath(elements);
103     }
104
105     public void setClasspath(String JavaDoc[] classpath) {
106         for (int i=0; i<classpath.length; i++) {
107             addPathElement(classpath[i]);
108         }
109     }
110
111     public void addPathElement(String JavaDoc path) {
112         addPathElement(path, recursiveScanner, null);
113     }
114
115     public void addPathElement(String JavaDoc path, boolean recursive,
116             String JavaDoc[] extensions) {
117
118         File JavaDoc pathComponent = new File JavaDoc(path);
119         addPathFile(pathComponent, recursive, extensions);
120     }
121
122     public void addPathFile(File JavaDoc component, boolean recursive) {
123         addPathFile(component, recursive, null);
124     }
125
126     public void addPathFile(File JavaDoc pathElement, boolean recursive,
127             String JavaDoc[] extensions) {
128
129         if (pathElement == null) {
130             throw new IllegalArgumentException JavaDoc("Classpath component must not " +
131                     "be null");
132         }
133
134         // Logic for adding a directory into classpath
135
if (pathElement.isDirectory()) {
136             Collection JavaDoc<File JavaDoc> components;
137
138             if (recursive) {
139                 components = scanComponentsRecursively(pathElement,
140                         extensions);
141             } else {
142                 components = scanComponents(pathElement, extensions);
143             }
144
145             for (File JavaDoc file : components) {
146                 pathComponents.add(file);
147             }
148         // Logic for adding a single file into classpath
149
} else {
150             pathComponents.add(pathElement);
151         }
152     }
153
154     private Collection JavaDoc<File JavaDoc> scanComponentsRecursively(File JavaDoc element,
155             String JavaDoc[] extensions) {
156
157         Collection JavaDoc<File JavaDoc> componentsFound = new ArrayList JavaDoc<File JavaDoc>();
158
159         componentsFound.addAll(scanComponents(element, extensions));
160
161         File JavaDoc[] directories = element.listFiles(new DirectoryFilter());
162
163         for (int i=0; i<directories.length; i++) {
164             File JavaDoc d = directories[i];
165             componentsFound.addAll(scanComponentsRecursively(d, extensions));
166         }
167
168         return componentsFound;
169     }
170
171     private Collection JavaDoc<File JavaDoc> scanComponents(File JavaDoc element,
172             String JavaDoc[] extensions) {
173
174         Collection JavaDoc<File JavaDoc> componentsFound = new ArrayList JavaDoc<File JavaDoc>();
175
176         if (extensions == null) {
177             componentsFound.add(element);
178
179         } else {
180             File JavaDoc[] elements = element.listFiles(
181                     new ExtensionFileFilter(extensions));
182
183             componentsFound.addAll(Arrays.asList(elements));
184         }
185
186         return componentsFound;
187     }
188
189     /**
190      * @see java.lang.Iterable#iterator()
191      */

192     public Iterator JavaDoc iterator() {
193         return pathComponents.iterator();
194     }
195
196     /**
197      * @see java.util.Vector#size()
198      */

199     public int size() {
200         return pathComponents.size();
201     }
202
203     /**
204      * @see java.util.Vector#elementAt(int)
205      */

206     public File JavaDoc elementAt(int index) {
207         return (File JavaDoc) pathComponents.get(index);
208     }
209
210 }
211
Popular Tags