/external/apache-harmony/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TestHelper_ClassLoader.java

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk · Java · 222 lines · 156 code · 28 blank · 38 comment · 33 complexity · 5822b353bb9050adb84d9ab408f0f337 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. 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. package org.apache.harmony.sql.tests.java.sql;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.jar.JarEntry;
  23. import java.util.jar.JarFile;
  24. public class TestHelper_ClassLoader extends ClassLoader {
  25. public TestHelper_ClassLoader() {
  26. super(null);
  27. }
  28. /**
  29. * Loads a class specified by its name
  30. * <p>
  31. * This classloader makes the assumption that any class it is asked to load
  32. * is in the current directory....
  33. */
  34. @Override
  35. public Class<?> findClass(String className) throws ClassNotFoundException {
  36. Class<?> theClass = null;
  37. if (!className
  38. .equals("org.apache.harmony.sql.tests.java.sql.TestHelper_DriverManager")) {
  39. return null;
  40. }
  41. String classNameAsFile = className.replace('.', '/') + ".class";
  42. // System.out.println("findClass - class filename = " + classNameAsFile
  43. // );
  44. String classPath = System.getProperty("java.class.path");
  45. // System.out.println("Test class loader - classpath = " + classPath );
  46. String theSeparator = String.valueOf(File.pathSeparatorChar);
  47. String[] theClassPaths = classPath.split(theSeparator);
  48. for (int i = 0; (i < theClassPaths.length) && (theClass == null); i++) {
  49. // Ignore jar files...
  50. if (theClassPaths[i].endsWith(".jar")) {
  51. theClass = loadClassFromJar(theClassPaths[i], className,
  52. classNameAsFile);
  53. } else {
  54. theClass = loadClassFromFile(theClassPaths[i], className,
  55. classNameAsFile);
  56. } // end if
  57. } // end for
  58. return theClass;
  59. } // end method findClass( String )
  60. @Override
  61. public Class<?> loadClass(String className) throws ClassNotFoundException {
  62. // Allowed classes:
  63. String[] disallowedClasses = {
  64. "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver1",
  65. "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver2",
  66. "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver4",
  67. "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver5" };
  68. Class<?> theClass;
  69. theClass = findLoadedClass(className);
  70. if (theClass != null) {
  71. return theClass;
  72. }
  73. theClass = this.findClass(className);
  74. if (theClass == null) {
  75. for (String element : disallowedClasses) {
  76. if (element.equals(className)) {
  77. return null;
  78. } // end if
  79. } // end for
  80. theClass = Class.forName(className);
  81. } // end if
  82. return theClass;
  83. } // end method loadClass( String )
  84. private Class<?> loadClassFromFile(String pathName, String className,
  85. String classNameAsFile) {
  86. Class<?> theClass = null;
  87. FileInputStream theInput = null;
  88. File theFile = null;
  89. try {
  90. theFile = new File(pathName, classNameAsFile);
  91. if (theFile.exists()) {
  92. int length = (int) theFile.length();
  93. theInput = new FileInputStream(theFile);
  94. byte[] theBytes = new byte[length + 100];
  95. int dataRead = 0;
  96. while (dataRead < length) {
  97. int count = theInput.read(theBytes, dataRead,
  98. theBytes.length - dataRead);
  99. if (count == -1) {
  100. break;
  101. }
  102. dataRead += count;
  103. }
  104. if (dataRead > 0) {
  105. // Create the class from the bytes read in...
  106. theClass = this.defineClass(className, theBytes, 0,
  107. dataRead);
  108. ClassLoader testClassLoader = theClass.getClassLoader();
  109. if (testClassLoader != this) {
  110. System.out.println("findClass - wrong classloader!!");
  111. }
  112. }
  113. }
  114. } catch (Exception e) {
  115. System.out.println("findClass - exception reading class file.");
  116. e.printStackTrace();
  117. } finally {
  118. try {
  119. if (theInput != null) {
  120. theInput.close();
  121. }
  122. } catch (Exception e) {
  123. }
  124. }
  125. return theClass;
  126. }
  127. /*
  128. * Loads a named class from a specified JAR file
  129. */
  130. private Class<?> loadClassFromJar(String jarfileName, String className,
  131. String classNameAsFile) {
  132. Class<?> theClass = null;
  133. // First, try to open the Jar file
  134. JarFile theJar = null;
  135. try {
  136. theJar = new JarFile(jarfileName);
  137. JarEntry theEntry = theJar.getJarEntry(classNameAsFile);
  138. if (theEntry == null) {
  139. // System.out.println("TestHelper_Classloader - did not find
  140. // class file in Jar " + jarfileName );
  141. return theClass;
  142. } // end if
  143. theEntry.getMethod();
  144. InputStream theStream = theJar.getInputStream(theEntry);
  145. long size = theEntry.getSize();
  146. if (size < 0) {
  147. size = 100000;
  148. }
  149. byte[] theBytes = new byte[(int) size + 100];
  150. int dataRead = 0;
  151. while (dataRead < size) {
  152. int count = theStream.read(theBytes, dataRead, theBytes.length
  153. - dataRead);
  154. if (count == -1) {
  155. break;
  156. }
  157. dataRead += count;
  158. } // end while
  159. // System.out.println("loadClassFromJar: read " + dataRead + " bytes
  160. // from class file");
  161. if (dataRead > 0) {
  162. // Create the class from the bytes read in...
  163. theClass = this.defineClass(className, theBytes, 0, dataRead);
  164. /* System.out.println("findClass: created Class object."); */
  165. ClassLoader testClassLoader = theClass.getClassLoader();
  166. if (testClassLoader != this) {
  167. System.out.println("findClass - wrong classloader!!");
  168. } else {
  169. System.out
  170. .println("Testclassloader loaded class from jar: "
  171. + className);
  172. } // end if
  173. } // end if
  174. } catch (IOException ie) {
  175. System.out
  176. .println("TestHelper_ClassLoader: IOException opening Jar "
  177. + jarfileName);
  178. } catch (Exception e) {
  179. System.out
  180. .println("TestHelper_ClassLoader: Exception loading class from Jar ");
  181. } catch (ClassFormatError ce) {
  182. System.out
  183. .println("TestHelper_ClassLoader: ClassFormatException loading class from Jar ");
  184. } finally {
  185. try {
  186. if (theJar != null) {
  187. theJar.close();
  188. }
  189. } catch (Exception e) {
  190. } // end try
  191. } // end try
  192. return theClass;
  193. } // end method loadClassFromJar(
  194. } // end class TestHelper_ClassLoader