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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.net.URLClassLoader;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Enumeration;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. /**
  30. * One instance of this class should be created by plugin manager for every available plug-in.
  31. * By default, this class loader is a Parent Last ClassLoader - it loads the classes from the plugin's jars
  32. * before delegating to the parent class loader.
  33. * Use {@link #parentFirst} to change the loading strategy.
  34. *
  35. * @author Decebal Suiu
  36. */
  37. public class PluginClassLoader extends URLClassLoader {
  38. private static final Logger log = LoggerFactory.getLogger(PluginClassLoader.class);
  39. private static final String JAVA_PACKAGE_PREFIX = "java.";
  40. private static final String PLUGIN_PACKAGE_PREFIX = "org.pf4j.";
  41. private PluginManager pluginManager;
  42. private PluginDescriptor pluginDescriptor;
  43. private boolean parentFirst;
  44. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent) {
  45. this(pluginManager, pluginDescriptor, parent, false);
  46. }
  47. /**
  48. * If {@code parentFirst} is {@code true}, indicates that the parent {@link ClassLoader} should be consulted
  49. * before trying to load the a class through this loader.
  50. */
  51. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent, boolean parentFirst) {
  52. super(new URL[0], parent);
  53. this.pluginManager = pluginManager;
  54. this.pluginDescriptor = pluginDescriptor;
  55. this.parentFirst = parentFirst;
  56. }
  57. @Override
  58. public void addURL(URL url) {
  59. log.debug("Add '{}'", url);
  60. super.addURL(url);
  61. }
  62. public void addFile(File file) {
  63. try {
  64. addURL(file.getCanonicalFile().toURI().toURL());
  65. } catch (IOException e) {
  66. // throw new RuntimeException(e);
  67. log.error(e.getMessage(), e);
  68. }
  69. }
  70. /**
  71. * By default, it uses a child first delegation model rather than the standard parent first.
  72. * If the requested class cannot be found in this class loader, the parent class loader will be consulted
  73. * via the standard {@link ClassLoader#loadClass(String)} mechanism.
  74. * Use {@link #parentFirst} to change the loading strategy.
  75. */
  76. @Override
  77. public Class<?> loadClass(String className) throws ClassNotFoundException {
  78. synchronized (getClassLoadingLock(className)) {
  79. // first check whether it's a system class, delegate to the system loader
  80. if (className.startsWith(JAVA_PACKAGE_PREFIX)) {
  81. return findSystemClass(className);
  82. }
  83. // if the class is part of the plugin engine use parent class loader
  84. if (className.startsWith(PLUGIN_PACKAGE_PREFIX) && !className.startsWith("org.pf4j.demo")) {
  85. // log.trace("Delegate the loading of PF4J class '{}' to parent", className);
  86. return getParent().loadClass(className);
  87. }
  88. log.trace("Received request to load class '{}'", className);
  89. // second check whether it's already been loaded
  90. Class<?> loadedClass = findLoadedClass(className);
  91. if (loadedClass != null) {
  92. log.trace("Found loaded class '{}'", className);
  93. return loadedClass;
  94. }
  95. if (!parentFirst) {
  96. // nope, try to load locally
  97. try {
  98. loadedClass = findClass(className);
  99. log.trace("Found class '{}' in plugin classpath", className);
  100. return loadedClass;
  101. } catch (ClassNotFoundException e) {
  102. // try next step
  103. }
  104. // look in dependencies
  105. loadedClass = loadClassFromDependencies(className);
  106. if (loadedClass != null) {
  107. log.trace("Found class '{}' in dependencies", className);
  108. return loadedClass;
  109. }
  110. log.trace("Couldn't find class '{}' in plugin classpath. Delegating to parent", className);
  111. // use the standard ClassLoader (which follows normal parent delegation)
  112. return super.loadClass(className);
  113. } else {
  114. // try to load from parent
  115. try {
  116. return super.loadClass(className);
  117. } catch (ClassNotFoundException e) {
  118. // try next step
  119. }
  120. log.trace("Couldn't find class '{}' in parent. Delegating to plugin classpath", className);
  121. // nope, try to load locally
  122. try {
  123. loadedClass = findClass(className);
  124. log.trace("Found class '{}' in plugin classpath", className);
  125. return loadedClass;
  126. } catch (ClassNotFoundException e) {
  127. // try next step
  128. }
  129. // look in dependencies
  130. loadedClass = loadClassFromDependencies(className);
  131. if (loadedClass != null) {
  132. log.trace("Found class '{}' in dependencies", className);
  133. return loadedClass;
  134. }
  135. throw new ClassNotFoundException(className);
  136. }
  137. }
  138. }
  139. /**
  140. * Load the named resource from this plugin.
  141. * By default, this implementation checks the plugin's classpath first then delegates to the parent.
  142. * Use {@link #parentFirst} to change the loading strategy.
  143. *
  144. * @param name the name of the resource.
  145. * @return the URL to the resource, {@code null} if the resource was not found.
  146. */
  147. @Override
  148. public URL getResource(String name) {
  149. log.trace("Received request to load resource '{}'", name);
  150. if (!parentFirst) {
  151. URL url = findResource(name);
  152. if (url != null) {
  153. log.trace("Found resource '{}' in plugin classpath", name);
  154. return url;
  155. }
  156. url = findResourceFromDependencies(name);
  157. if (url != null) {
  158. log.trace("Found resource '{}' in plugin dependencies", name);
  159. return url;
  160. }
  161. log.trace("Couldn't find resource '{}' in plugin or dependencies classpath. Delegating to parent", name);
  162. return super.getResource(name);
  163. } else {
  164. URL url = super.getResource(name);
  165. if (url != null) {
  166. log.trace("Found resource '{}' in parent", name);
  167. return url;
  168. }
  169. log.trace("Couldn't find resource '{}' in parent", name);
  170. url = findResourceFromDependencies(name);
  171. if (url != null) {
  172. log.trace("Found resource '{}' in dependencies", name);
  173. return url;
  174. }
  175. return findResource(name);
  176. }
  177. }
  178. @Override
  179. public Enumeration<URL> getResources(String name) throws IOException {
  180. List<URL> resources = new ArrayList<>();
  181. if (!parentFirst) {
  182. resources.addAll(Collections.list(findResources(name)));
  183. resources.addAll(findResourcesFromDependencies(name));
  184. if (getParent() != null) {
  185. resources.addAll(Collections.list(getParent().getResources(name)));
  186. }
  187. } else {
  188. if (getParent() != null) {
  189. resources.addAll(Collections.list(getParent().getResources(name)));
  190. }
  191. resources.addAll(findResourcesFromDependencies(name));
  192. resources.addAll(Collections.list(super.findResources(name)));
  193. }
  194. return Collections.enumeration(resources);
  195. }
  196. private Class<?> loadClassFromDependencies(String className) {
  197. log.trace("Search in dependencies for class '{}'", className);
  198. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  199. for (PluginDependency dependency : dependencies) {
  200. ClassLoader classLoader = pluginManager.getPluginClassLoader(dependency.getPluginId());
  201. // If the dependency is marked as optional, its class loader might not be available.
  202. if (classLoader == null || dependency.isOptional()) {
  203. continue;
  204. }
  205. try {
  206. return classLoader.loadClass(className);
  207. } catch (ClassNotFoundException e) {
  208. // try next dependency
  209. }
  210. }
  211. return null;
  212. }
  213. private URL findResourceFromDependencies(String name) {
  214. log.trace("Search in dependencies for resource '{}'", name);
  215. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  216. for (PluginDependency dependency : dependencies) {
  217. PluginClassLoader classLoader = (PluginClassLoader) pluginManager.getPluginClassLoader(dependency.getPluginId());
  218. // If the dependency is marked as optional, its class loader might not be available.
  219. if (classLoader == null || dependency.isOptional()) {
  220. continue;
  221. }
  222. URL url = classLoader.findResource(name);
  223. if (Objects.nonNull(url)) {
  224. return url;
  225. }
  226. }
  227. return null;
  228. }
  229. private Collection<URL> findResourcesFromDependencies(String name) throws IOException {
  230. log.trace("Search in dependencies for resources '{}'", name);
  231. List<URL> results = new ArrayList<>();
  232. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  233. for (PluginDependency dependency : dependencies) {
  234. PluginClassLoader classLoader = (PluginClassLoader) pluginManager.getPluginClassLoader(dependency.getPluginId());
  235. // If the dependency is marked as optional, its class loader might not be available.
  236. if (classLoader == null || dependency.isOptional()) {
  237. continue;
  238. }
  239. results.addAll(Collections.list(classLoader.findResources(name)));
  240. }
  241. return results;
  242. }
  243. }