KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > junit > XDocletClassLoader


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

17
18 import java.io.*;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
24
25 /**
26  * Special class loader for xdoclet tests.
27  *
28  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
29  */

30 public class XDocletClassLoader extends ClassLoader JavaDoc
31 {
32     private ArrayList JavaDoc _classPath = new ArrayList JavaDoc();
33
34     public XDocletClassLoader(String JavaDoc classPath)
35     {
36         super();
37         setClassPath(classPath);
38     }
39
40     public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc
41     {
42         Class JavaDoc result = null;
43
44         if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("org.") || name.startsWith("sun."))
45         {
46             try
47             {
48                 result = findSystemClass(name);
49             }
50             catch (ClassNotFoundException JavaDoc ex)
51             {}
52         }
53         if (result == null)
54         {
55             result = findLoadedClass(name);
56         }
57         if (result == null)
58         {
59             byte[] data = loadClassData(name);
60
61             if (data != null)
62             {
63                 result = defineClass(name, data, 0, data.length);
64             }
65         }
66         if (result == null)
67         {
68             throw new ClassNotFoundException JavaDoc(name);
69         }
70         if (resolve)
71         {
72             resolveClass(result);
73         }
74         return result;
75     }
76
77     private void setClassPath(String JavaDoc classPath)
78     {
79         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classPath, System.getProperty("path.separator"));
80
81         _classPath.clear();
82         while (tokenizer.hasMoreTokens())
83         {
84             _classPath.add(tokenizer.nextToken());
85         }
86     }
87
88     private byte[] loadClassData(String JavaDoc className) throws ClassNotFoundException JavaDoc
89     {
90         byte[] data = null;
91         String JavaDoc path = null;
92         String JavaDoc fileName = className.replace('.', '/') + ".class";
93
94         for (Iterator JavaDoc it = _classPath.iterator(); it.hasNext();)
95         {
96             path = (String JavaDoc)it.next();
97             if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip"))
98             {
99                 data = loadJar(path, fileName);
100             }
101             else
102             {
103                 data = loadFile(path, fileName);
104             }
105             if (data != null)
106             {
107                 return data;
108             }
109         }
110         throw new ClassNotFoundException JavaDoc(className);
111     }
112
113     private byte[] loadFile(String JavaDoc path, String JavaDoc fileName)
114     {
115         File JavaDoc file = new File JavaDoc(path, fileName);
116
117         if (file.exists())
118         {
119             try
120             {
121                 FileInputStream input = new FileInputStream(file);
122                 ByteArrayOutputStream output = new ByteArrayOutputStream(1000);
123                 byte[] data = new byte[1000];
124                 int numRead;
125
126                 while ((numRead = input.read(data)) != -1)
127                 {
128                     output.write(data, 0, numRead);
129                 }
130                 input.close();
131                 output.close();
132                 return output.toByteArray();
133             }
134             catch (IOException ex)
135             {}
136         }
137         return null;
138     }
139
140     private byte[] loadJar(String JavaDoc path, String JavaDoc fileName)
141     {
142         File JavaDoc file = new File JavaDoc(path);
143
144         if (!file.exists())
145         {
146             return null;
147         }
148
149         ZipFile JavaDoc zipFile = null;
150
151         try
152         {
153             zipFile = new ZipFile JavaDoc(file);
154         }
155         catch(IOException ex)
156         {
157             return null;
158         }
159
160         ZipEntry JavaDoc entry = zipFile.getEntry(fileName);
161
162         if (entry == null)
163         {
164             return null;
165         }
166
167         InputStream input = null;
168         int size = (int)entry.getSize();
169         byte[] data = new byte[size];
170         int numRead;
171
172         try
173         {
174             input = zipFile.getInputStream(entry);
175
176             for (int pos = 0; pos < size; pos += numRead)
177             {
178                 numRead = input.read(data, pos, data.length - pos);
179             }
180             zipFile.close();
181             return data;
182         }
183         catch (IOException ex)
184         {}
185         finally
186         {
187             try
188             {
189                 if (input != null)
190                 {
191                     input.close();
192                 }
193             }
194             catch (IOException ex)
195             {}
196         }
197         return null;
198     }
199 }
200
Popular Tags