001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.harmony.sql.tests.java.sql;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.jar.JarEntry;
025: import java.util.jar.JarFile;
026:
027: public class TestHelper_ClassLoader extends ClassLoader {
028:
029: public TestHelper_ClassLoader() {
030: super (null);
031: }
032:
033: /**
034: * Loads a class specified by its name
035: * <p>
036: * This classloader makes the assumption that any class it is asked to load
037: * is in the current directory....
038: */
039: @Override
040: public Class<?> findClass(String className)
041: throws ClassNotFoundException {
042: Class<?> theClass = null;
043:
044: if (!className
045: .equals("org.apache.harmony.sql.tests.java.sql.TestHelper_DriverManager")) {
046: return null;
047: }
048:
049: String classNameAsFile = className.replace('.', '/') + ".class";
050: // System.out.println("findClass - class filename = " + classNameAsFile
051: // );
052:
053: String classPath = System.getProperty("java.class.path");
054: // System.out.println("Test class loader - classpath = " + classPath );
055:
056: String theSeparator = String.valueOf(File.pathSeparatorChar);
057: String[] theClassPaths = classPath.split(theSeparator);
058: for (int i = 0; (i < theClassPaths.length)
059: && (theClass == null); i++) {
060: // Ignore jar files...
061: if (theClassPaths[i].endsWith(".jar")) {
062: theClass = loadClassFromJar(theClassPaths[i],
063: className, classNameAsFile);
064: } else {
065: theClass = loadClassFromFile(theClassPaths[i],
066: className, classNameAsFile);
067: } // end if
068: } // end for
069:
070: return theClass;
071: } // end method findClass( String )
072:
073: @Override
074: public Class<?> loadClass(String className)
075: throws ClassNotFoundException {
076: // Allowed classes:
077: String[] disallowedClasses = {
078: "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver1",
079: "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver2",
080: "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver4",
081: "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver5" };
082:
083: Class<?> theClass;
084:
085: theClass = findLoadedClass(className);
086: if (theClass != null) {
087: return theClass;
088: }
089:
090: theClass = this .findClass(className);
091:
092: if (theClass == null) {
093: for (String element : disallowedClasses) {
094: if (element.equals(className)) {
095: return null;
096: } // end if
097: } // end for
098: theClass = Class.forName(className);
099: } // end if
100:
101: return theClass;
102: } // end method loadClass( String )
103:
104: private Class<?> loadClassFromFile(String pathName,
105: String className, String classNameAsFile) {
106: Class<?> theClass = null;
107: FileInputStream theInput = null;
108: File theFile = null;
109: try {
110: theFile = new File(pathName, classNameAsFile);
111: if (theFile.exists()) {
112: int length = (int) theFile.length();
113: theInput = new FileInputStream(theFile);
114: byte[] theBytes = new byte[length + 100];
115: int dataRead = 0;
116: while (dataRead < length) {
117: int count = theInput.read(theBytes, dataRead,
118: theBytes.length - dataRead);
119: if (count == -1) {
120: break;
121: }
122: dataRead += count;
123: }
124:
125: if (dataRead > 0) {
126: // Create the class from the bytes read in...
127: theClass = this .defineClass(className, theBytes, 0,
128: dataRead);
129: ClassLoader testClassLoader = theClass
130: .getClassLoader();
131: if (testClassLoader != this ) {
132: System.out
133: .println("findClass - wrong classloader!!");
134: }
135: }
136: }
137: } catch (Exception e) {
138: System.out
139: .println("findClass - exception reading class file.");
140: e.printStackTrace();
141: } finally {
142: try {
143: if (theInput != null) {
144: theInput.close();
145: }
146: } catch (Exception e) {
147: }
148: }
149: return theClass;
150: }
151:
152: /*
153: * Loads a named class from a specified JAR file
154: */
155: private Class<?> loadClassFromJar(String jarfileName,
156: String className, String classNameAsFile) {
157: Class<?> theClass = null;
158:
159: // First, try to open the Jar file
160: JarFile theJar = null;
161: try {
162: theJar = new JarFile(jarfileName);
163: JarEntry theEntry = theJar.getJarEntry(classNameAsFile);
164:
165: if (theEntry == null) {
166: // System.out.println("TestHelper_Classloader - did not find
167: // class file in Jar " + jarfileName );
168: return theClass;
169: } // end if
170:
171: theEntry.getMethod();
172: InputStream theStream = theJar.getInputStream(theEntry);
173:
174: long size = theEntry.getSize();
175: if (size < 0) {
176: size = 100000;
177: }
178: byte[] theBytes = new byte[(int) size + 100];
179:
180: int dataRead = 0;
181: while (dataRead < size) {
182: int count = theStream.read(theBytes, dataRead,
183: theBytes.length - dataRead);
184: if (count == -1) {
185: break;
186: }
187: dataRead += count;
188: } // end while
189:
190: // System.out.println("loadClassFromJar: read " + dataRead + " bytes
191: // from class file");
192: if (dataRead > 0) {
193: // Create the class from the bytes read in...
194: theClass = this .defineClass(className, theBytes, 0,
195: dataRead);
196: /* System.out.println("findClass: created Class object."); */
197: ClassLoader testClassLoader = theClass.getClassLoader();
198: if (testClassLoader != this ) {
199: System.out
200: .println("findClass - wrong classloader!!");
201: } else {
202: System.out
203: .println("Testclassloader loaded class from jar: "
204: + className);
205: } // end if
206: } // end if
207: } catch (IOException ie) {
208: System.out
209: .println("TestHelper_ClassLoader: IOException opening Jar "
210: + jarfileName);
211: } catch (Exception e) {
212: System.out
213: .println("TestHelper_ClassLoader: Exception loading class from Jar ");
214: } catch (ClassFormatError ce) {
215: System.out
216: .println("TestHelper_ClassLoader: ClassFormatException loading class from Jar ");
217: } finally {
218: try {
219: if (theJar != null) {
220: theJar.close();
221: }
222: } catch (Exception e) {
223: } // end try
224: } // end try
225:
226: return theClass;
227: } // end method loadClassFromJar(
228:
229: } // end class TestHelper_ClassLoader
|