001: package xdoclet.junit;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.*;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.StringTokenizer;
022: import java.util.zip.ZipEntry;
023: import java.util.zip.ZipFile;
024:
025: /**
026: * Special class loader for xdoclet tests.
027: *
028: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
029: */
030: public class XDocletClassLoader extends ClassLoader {
031: private ArrayList _classPath = new ArrayList();
032:
033: public XDocletClassLoader(String classPath) {
034: super ();
035: setClassPath(classPath);
036: }
037:
038: public synchronized Class loadClass(String name, boolean resolve)
039: throws ClassNotFoundException {
040: Class result = null;
041:
042: if (name.startsWith("java.") || name.startsWith("javax.")
043: || name.startsWith("org.") || name.startsWith("sun.")) {
044: try {
045: result = findSystemClass(name);
046: } catch (ClassNotFoundException ex) {
047: }
048: }
049: if (result == null) {
050: result = findLoadedClass(name);
051: }
052: if (result == null) {
053: byte[] data = loadClassData(name);
054:
055: if (data != null) {
056: result = defineClass(name, data, 0, data.length);
057: }
058: }
059: if (result == null) {
060: throw new ClassNotFoundException(name);
061: }
062: if (resolve) {
063: resolveClass(result);
064: }
065: return result;
066: }
067:
068: private void setClassPath(String classPath) {
069: StringTokenizer tokenizer = new StringTokenizer(classPath,
070: System.getProperty("path.separator"));
071:
072: _classPath.clear();
073: while (tokenizer.hasMoreTokens()) {
074: _classPath.add(tokenizer.nextToken());
075: }
076: }
077:
078: private byte[] loadClassData(String className)
079: throws ClassNotFoundException {
080: byte[] data = null;
081: String path = null;
082: String fileName = className.replace('.', '/') + ".class";
083:
084: for (Iterator it = _classPath.iterator(); it.hasNext();) {
085: path = (String) it.next();
086: if (path.toLowerCase().endsWith(".jar")
087: || path.toLowerCase().endsWith(".zip")) {
088: data = loadJar(path, fileName);
089: } else {
090: data = loadFile(path, fileName);
091: }
092: if (data != null) {
093: return data;
094: }
095: }
096: throw new ClassNotFoundException(className);
097: }
098:
099: private byte[] loadFile(String path, String fileName) {
100: File file = new File(path, fileName);
101:
102: if (file.exists()) {
103: try {
104: FileInputStream input = new FileInputStream(file);
105: ByteArrayOutputStream output = new ByteArrayOutputStream(
106: 1000);
107: byte[] data = new byte[1000];
108: int numRead;
109:
110: while ((numRead = input.read(data)) != -1) {
111: output.write(data, 0, numRead);
112: }
113: input.close();
114: output.close();
115: return output.toByteArray();
116: } catch (IOException ex) {
117: }
118: }
119: return null;
120: }
121:
122: private byte[] loadJar(String path, String fileName) {
123: File file = new File(path);
124:
125: if (!file.exists()) {
126: return null;
127: }
128:
129: ZipFile zipFile = null;
130:
131: try {
132: zipFile = new ZipFile(file);
133: } catch (IOException ex) {
134: return null;
135: }
136:
137: ZipEntry entry = zipFile.getEntry(fileName);
138:
139: if (entry == null) {
140: return null;
141: }
142:
143: InputStream input = null;
144: int size = (int) entry.getSize();
145: byte[] data = new byte[size];
146: int numRead;
147:
148: try {
149: input = zipFile.getInputStream(entry);
150:
151: for (int pos = 0; pos < size; pos += numRead) {
152: numRead = input.read(data, pos, data.length - pos);
153: }
154: zipFile.close();
155: return data;
156: } catch (IOException ex) {
157: } finally {
158: try {
159: if (input != null) {
160: input.close();
161: }
162: } catch (IOException ex) {
163: }
164: }
165: return null;
166: }
167: }
|