BootClassLoader.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (C) 2005, 2007 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.gcj.runtime;
  7. import gnu.java.net.protocol.core.Handler;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.net.URL;
  11. import java.net.URLClassLoader;
  12. import java.util.Enumeration;
  13. import java.util.StringTokenizer;
  14. import java.util.Vector;
  15. /**
  16. * This is a helper for the bootstrap class loader. It is a
  17. * URLClassLoader so that we can read a class path and re-use all the
  18. * existing code for finding classes, extracting them from jars, etc.
  19. * However, it is never called the way that an ordinary ClassLoader is
  20. * called. For instance, loadClass() is never used.
  21. */
  22. public final class BootClassLoader extends HelperClassLoader
  23. {
  24. // This forces the core URL handler to be included in statically
  25. // linked executables. The line that adds core:/ to the search
  26. // path fails otherwise.
  27. static Class coreHandler = gnu.java.net.protocol.core.Handler.class;
  28. private boolean initialized;
  29. private URLClassLoader bootURLLoader;
  30. BootClassLoader(String libdir)
  31. {
  32. // The BootClassLoader is the top of the delegation chain. It does not
  33. // have a parent.
  34. super((ClassLoader) null);
  35. addDirectoriesFromProperty("java.endorsed.dirs");
  36. addDirectoriesFromProperty("gnu.gcj.runtime.endorsed.dirs");
  37. try
  38. {
  39. // Add core:/ to the end so any resources compiled into this
  40. // executable may be found.
  41. addURL(new URL("core", "", -1, "/"));
  42. }
  43. catch (java.net.MalformedURLException x)
  44. {
  45. // This should never happen.
  46. throw new RuntimeException(x);
  47. }
  48. }
  49. public Class bootLoadClass(String name)
  50. throws ClassNotFoundException
  51. {
  52. Class c = findLoadedClass(name);
  53. if (c == null)
  54. {
  55. try
  56. {
  57. // We could hack URLClassLoader to make this more
  58. // efficient, if it mattered.
  59. c = findClass(name);
  60. }
  61. catch (ClassNotFoundException _)
  62. {
  63. c = null;
  64. }
  65. }
  66. return c;
  67. }
  68. // Parse the boot classpath and create a URLClassLoader that loads
  69. // resources from it. This is provided for the benefit of code that
  70. // does things like
  71. // ClassLoader.getResourceAsStream("java/lang/Object.class")
  72. private synchronized URLClassLoader getBootURLLoader()
  73. {
  74. if (initialized)
  75. return bootURLLoader;
  76. initialized = true;
  77. Vector<URL> urls = new Vector<URL>();
  78. String bootClasspath = System.getProperty ("sun.boot.class.path");
  79. StringTokenizer st =
  80. new StringTokenizer(bootClasspath, File.pathSeparator);
  81. while (st.hasMoreTokens())
  82. {
  83. try
  84. {
  85. urls.add(new File(st.nextToken()).toURL());
  86. }
  87. catch (java.net.MalformedURLException e)
  88. {
  89. }
  90. }
  91. if (urls.size() > 0)
  92. bootURLLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
  93. return bootURLLoader;
  94. }
  95. public URL bootGetResource(String name)
  96. {
  97. URL url = findResource(name);
  98. if (url != null)
  99. return url;
  100. URLClassLoader loader = getBootURLLoader();
  101. if (loader != null)
  102. url = loader.findResource(name);
  103. return url;
  104. }
  105. public Enumeration bootGetResources(String name) throws IOException
  106. {
  107. URLClassLoader loader = getBootURLLoader();
  108. Enumeration[] e =
  109. {
  110. findResources(name),
  111. (loader != null) ? loader.findResources(name) : null
  112. };
  113. Vector v = new Vector();
  114. for (Enumeration en : e)
  115. if (en != null)
  116. while (en.hasMoreElements())
  117. v.add(en.nextElement());
  118. return v.elements();
  119. }
  120. }