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

PluginClassLoader.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.plugins;
  23. import com.dmdirc.util.resourcemanager.ResourceManager;
  24. import java.io.IOException;
  25. import java.net.URL;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.Enumeration;
  29. import java.util.List;
  30. public class PluginClassLoader extends ClassLoader {
  31. /** The plugin Info object for the plugin we are loading. */
  32. private final PluginInfo pluginInfo;
  33. /** Global Class Loader */
  34. private final GlobalClassLoader globalLoader;
  35. /** The parent class loaders. */
  36. private final PluginClassLoader[] parents;
  37. /**
  38. * Create a new PluginClassLoader.
  39. *
  40. * @param info PluginInfo this classloader will be for
  41. * @param globalLoader Global class loader to use where needed.
  42. * @param parents Parent ClassLoaders
  43. */
  44. public PluginClassLoader(final PluginInfo info, final GlobalClassLoader globalLoader,
  45. final PluginClassLoader... parents) {
  46. this.pluginInfo = info;
  47. this.parents = parents;
  48. this.globalLoader = globalLoader;
  49. }
  50. /**
  51. * Get a PluginClassLoader that is a subclassloader of this one.
  52. *
  53. * @param info PluginInfo the new classloader will be for
  54. *
  55. * @return A classloader configured with this one as its parent
  56. */
  57. public PluginClassLoader getSubClassLoader(final PluginInfo info) {
  58. return new PluginClassLoader(info, globalLoader, this);
  59. }
  60. /**
  61. * Load the plugin with the given className.
  62. *
  63. * @param name Class Name of plugin
  64. *
  65. * @return plugin class
  66. *
  67. * @throws ClassNotFoundException if the class to be loaded could not be found.
  68. */
  69. @Override
  70. public Class<?> loadClass(final String name) throws ClassNotFoundException {
  71. return loadClass(name, true);
  72. }
  73. /**
  74. * Have we already loaded the given class name?
  75. *
  76. * @param name Name to check.
  77. * @param checkGlobal Should we check if the GCL has loaded it as well?
  78. *
  79. * @return True if the specified class is loaded, false otherwise
  80. */
  81. public boolean isClassLoaded(final String name, final boolean checkGlobal) {
  82. return findLoadedClass(name) != null || (checkGlobal
  83. && globalLoader.isClassLoaded(name));
  84. }
  85. /**
  86. * Load the plugin with the given className.
  87. *
  88. * @param name Class Name of plugin
  89. * @param askGlobal Ask the global class loaded for this class if we can't find it?
  90. *
  91. * @return plugin class
  92. *
  93. * @throws ClassNotFoundException if the class to be loaded could not be found.
  94. */
  95. @Override
  96. public Class<?> loadClass(final String name, final boolean askGlobal) throws
  97. ClassNotFoundException {
  98. Class<?> loadedClass = null;
  99. for (PluginClassLoader parent : parents) {
  100. try {
  101. loadedClass = parent.loadClass(name, false);
  102. if (loadedClass != null) {
  103. return loadedClass;
  104. }
  105. } catch (ClassNotFoundException cnfe) {
  106. // Parent doesn't have the class, carry on trying...
  107. }
  108. }
  109. ResourceManager res;
  110. try {
  111. res = pluginInfo.getResourceManager();
  112. } catch (IOException ioe) {
  113. throw new ClassNotFoundException("Error with resourcemanager", ioe);
  114. }
  115. final String fileName = name.replace('.', '/') + ".class";
  116. try {
  117. if (pluginInfo.isPersistent(name) || !res.resourceExists(fileName)) {
  118. if (!pluginInfo.isPersistent(name) && askGlobal) {
  119. return globalLoader.loadClass(name);
  120. } else {
  121. // Try to load class from previous load.
  122. try {
  123. if (askGlobal) {
  124. return globalLoader.loadClass(name, pluginInfo);
  125. }
  126. } catch (ClassNotFoundException e) {
  127. /* Class doesn't exist, we load it ourself below */
  128. }
  129. }
  130. }
  131. } catch (NoClassDefFoundError e) {
  132. throw new ClassNotFoundException("Error loading '" + name + "' (wanted by "
  133. + pluginInfo.getMetaData().getName() + ") -> " + e.getMessage(), e);
  134. }
  135. // Don't duplicate a class
  136. if (isClassLoaded(name, false)) {
  137. return findLoadedClass(name);
  138. }
  139. // We are meant to be loading this one!
  140. byte[] data;
  141. if (res.resourceExists(fileName)) {
  142. data = res.getResourceBytes(fileName);
  143. } else {
  144. throw new ClassNotFoundException("Resource '" + name + "' (wanted by " + pluginInfo.
  145. getMetaData().getName() + ") does not exist.");
  146. }
  147. try {
  148. if (pluginInfo.isPersistent(name)) {
  149. globalLoader.defineClass(name, data);
  150. } else {
  151. loadedClass = defineClass(name, data, 0, data.length);
  152. }
  153. } catch (LinkageError e) {
  154. throw new ClassNotFoundException(e.getMessage(), e);
  155. }
  156. if (loadedClass == null) {
  157. throw new ClassNotFoundException("Could not load " + name);
  158. } else {
  159. resolveClass(loadedClass);
  160. }
  161. return loadedClass;
  162. }
  163. /**
  164. * {@inheritDoc}
  165. *
  166. * @since 0.6.3
  167. */
  168. @Override
  169. protected URL findResource(final String name) {
  170. try {
  171. final ResourceManager res = pluginInfo.getResourceManager();
  172. final URL url = res.getResourceURL(name);
  173. if (url != null) {
  174. return url;
  175. }
  176. } catch (IOException ioe) {
  177. // Do nothing, fall through
  178. }
  179. // Try the parents
  180. for (PluginClassLoader parent : parents) {
  181. final URL url = parent.findResource(name);
  182. if (url != null) {
  183. return url;
  184. }
  185. }
  186. return super.findResource(name);
  187. }
  188. /**
  189. * {@inheritDoc}
  190. *
  191. * @since 0.6.3
  192. */
  193. @Override
  194. protected Enumeration<URL> findResources(final String name)
  195. throws IOException {
  196. final URL resource = findResource(name);
  197. final List<URL> resources = new ArrayList<>();
  198. if (resource != null) {
  199. resources.add(resource);
  200. }
  201. final Enumeration<URL> urls = super.findResources(name);
  202. while (urls.hasMoreElements()) {
  203. resources.add(urls.nextElement());
  204. }
  205. return Collections.enumeration(resources);
  206. }
  207. }