You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TaskClassLoader.java 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package org.apache.ant;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.zip.*;
  5. /**
  6. *
  7. *
  8. * @author James Duncan Davidson (duncan@apache.org)
  9. */
  10. class TaskClassLoader extends ClassLoader {
  11. // -----------------------------------------------------------------
  12. // PRIVATE MEMBERS
  13. // -----------------------------------------------------------------
  14. /**
  15. *
  16. */
  17. private Hashtable cache = new Hashtable();
  18. /**
  19. *
  20. */
  21. private ZipFile zf;
  22. // -----------------------------------------------------------------
  23. // CONSTRUCTORS
  24. // -----------------------------------------------------------------
  25. /**
  26. * Constructs a classloader that loads classes from the specified
  27. * zip file.
  28. */
  29. TaskClassLoader(ClassLoader parent, ZipFile zf) {
  30. super(parent);
  31. this.zf = zf;
  32. }
  33. // -----------------------------------------------------------------
  34. // PUBLIC METHODS
  35. // -----------------------------------------------------------------
  36. /**
  37. *
  38. */
  39. public Class findClass(String name)
  40. throws ClassNotFoundException
  41. {
  42. Class c;
  43. try {
  44. return findSystemClass(name);
  45. } catch (ClassNotFoundException cnfe) {
  46. }
  47. try {
  48. return this.getClass().getClassLoader().loadClass(name);
  49. } catch (Exception e) {
  50. }
  51. Object o = cache.get(name);
  52. if (o != null) {
  53. c = (Class)o;
  54. } else {
  55. byte[] data = loadClassData(name);
  56. c = defineClass(data, 0, data.length);
  57. cache.put(name, c);
  58. }
  59. //if (resolve) {
  60. // resolveClass(c);
  61. //}
  62. return c;
  63. }
  64. /**
  65. *
  66. */
  67. private byte[] loadClassData(String name) throws ClassNotFoundException {
  68. String newName = name.replace('.', '/');
  69. ZipEntry ze = zf.getEntry("/" + newName + ".class");
  70. //System.out.println("/" + newName + ".class");
  71. //System.out.println("ZE: " + ze);
  72. if (ze != null) {
  73. byte[] buf = new byte[((int)ze.getSize())];
  74. // System.out.println("ZE SIZE " + ze.getSize());
  75. try {
  76. InputStream in = zf.getInputStream(ze);
  77. int count = 0;
  78. int thisRead = 0;
  79. while (count < buf.length && thisRead != -1) {
  80. thisRead = in.read(buf, count, buf.length - count);
  81. count += thisRead;
  82. }
  83. in.close();
  84. } catch (IOException ioe) {
  85. throw new ClassNotFoundException("Can't load class: " + name + " " +
  86. ioe.getMessage());
  87. }
  88. return buf;
  89. } else {
  90. throw new ClassNotFoundException("Can't find class for: " + name);
  91. }
  92. }
  93. }