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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.URL;
  22. import java.net.URLClassLoader;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.Enumeration;
  27. import java.util.List;
  28. import java.util.Objects;
  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 #classLoadingStrategy} 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 ClassLoadingStrategy classLoadingStrategy;
  44. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent) {
  45. this(pluginManager, pluginDescriptor, parent, ClassLoadingStrategy.PDA);
  46. }
  47. /**
  48. * @deprecated Replaced by {@link #PluginClassLoader(PluginManager, PluginDescriptor, ClassLoader, ClassLoadingStrategy)}.
  49. * If {@code parentFirst} is {@code true}, indicates that the parent {@link ClassLoader} should be consulted
  50. * before trying to load the a class through this loader.
  51. */
  52. @Deprecated
  53. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent, boolean parentFirst) {
  54. this(pluginManager, pluginDescriptor, parent, parentFirst ? ClassLoadingStrategy.APD : ClassLoadingStrategy.PDA);
  55. }
  56. /**
  57. * classloading according to {@code classLoadingStrategy}
  58. */
  59. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent, ClassLoadingStrategy classLoadingStrategy) {
  60. super(new URL[0], parent);
  61. this.pluginManager = pluginManager;
  62. this.pluginDescriptor = pluginDescriptor;
  63. this.classLoadingStrategy = classLoadingStrategy;
  64. }
  65. @Override
  66. public void addURL(URL url) {
  67. log.debug("Add '{}'", url);
  68. super.addURL(url);
  69. }
  70. public void addFile(File file) {
  71. try {
  72. addURL(file.getCanonicalFile().toURI().toURL());
  73. } catch (IOException e) {
  74. // throw new RuntimeException(e);
  75. log.error(e.getMessage(), e);
  76. }
  77. }
  78. /**
  79. * By default, it uses a child first delegation model rather than the standard parent first.
  80. * If the requested class cannot be found in this class loader, the parent class loader will be consulted
  81. * via the standard {@link ClassLoader#loadClass(String)} mechanism.
  82. * Use {@link #classLoadingStrategy} to change the loading strategy.
  83. */
  84. @Override
  85. public Class<?> loadClass(String className) throws ClassNotFoundException {
  86. synchronized (getClassLoadingLock(className)) {
  87. // first check whether it's a system class, delegate to the system loader
  88. if (className.startsWith(JAVA_PACKAGE_PREFIX)) {
  89. return findSystemClass(className);
  90. }
  91. // if the class is part of the plugin engine use parent class loader
  92. if (className.startsWith(PLUGIN_PACKAGE_PREFIX) && !className.startsWith("org.pf4j.demo")) {
  93. // log.trace("Delegate the loading of PF4J class '{}' to parent", className);
  94. return getParent().loadClass(className);
  95. }
  96. log.trace("Received request to load class '{}'", className);
  97. // second check whether it's already been loaded
  98. Class<?> loadedClass = findLoadedClass(className);
  99. if (loadedClass != null) {
  100. log.trace("Found loaded class '{}'", className);
  101. return loadedClass;
  102. }
  103. for (ClassLoadingStrategy.Source classLoadingSource : classLoadingStrategy.getSources()) {
  104. Class<?> c = null;
  105. try {
  106. switch (classLoadingSource) {
  107. case APPLICATION:
  108. c = super.loadClass(className);
  109. break;
  110. case PLUGIN:
  111. c = findClass(className);
  112. break;
  113. case DEPENDENCIES:
  114. c = loadClassFromDependencies(className);
  115. break;
  116. }
  117. } catch (ClassNotFoundException ignored) {
  118. }
  119. if (c != null) {
  120. log.trace("Found class '{}' in {} classpath", className,classLoadingSource);
  121. return c;
  122. } else {
  123. log.trace("Couldn't find class '{}' in {} classpath", className,classLoadingSource);
  124. }
  125. }
  126. throw new ClassNotFoundException(className);
  127. }
  128. }
  129. /**
  130. * Load the named resource from this plugin.
  131. * By default, this implementation checks the plugin's classpath first then delegates to the parent.
  132. * Use {@link #classLoadingStrategy} to change the loading strategy.
  133. *
  134. * @param name the name of the resource.
  135. * @return the URL to the resource, {@code null} if the resource was not found.
  136. */
  137. @Override
  138. public URL getResource(String name) {
  139. log.trace("Received request to load resource '{}'", name);
  140. for (ClassLoadingStrategy.Source classLoadingSource : classLoadingStrategy.getSources()) {
  141. URL url = null;
  142. switch (classLoadingSource) {
  143. case APPLICATION:
  144. url = super.getResource(name);
  145. break;
  146. case PLUGIN:
  147. url = findResource(name);
  148. break;
  149. case DEPENDENCIES:
  150. url = findResourceFromDependencies(name);
  151. break;
  152. }
  153. if (url != null) {
  154. log.trace("Found resource '{}' in {} classpath", name,classLoadingSource);
  155. return url;
  156. } else {
  157. log.trace("Couldn't find resource '{}' in {}.", name,classLoadingSource);
  158. }
  159. }
  160. return null;
  161. }
  162. @Override
  163. public Enumeration<URL> getResources(String name) throws IOException {
  164. List<URL> resources = new ArrayList<>();
  165. log.trace("Received request to load resources '{}'", name);
  166. for (ClassLoadingStrategy.Source classLoadingSource : classLoadingStrategy.getSources()) {
  167. switch (classLoadingSource) {
  168. case APPLICATION:
  169. if (getParent() != null) {
  170. resources.addAll(Collections.list(getParent().getResources(name)));
  171. }
  172. break;
  173. case PLUGIN:
  174. resources.addAll(Collections.list(findResources(name)));
  175. break;
  176. case DEPENDENCIES:
  177. resources.addAll(findResourcesFromDependencies(name));
  178. break;
  179. }
  180. }
  181. return Collections.enumeration(resources);
  182. }
  183. protected Class<?> loadClassFromDependencies(String className) {
  184. log.trace("Search in dependencies for class '{}'", className);
  185. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  186. for (PluginDependency dependency : dependencies) {
  187. ClassLoader classLoader = pluginManager.getPluginClassLoader(dependency.getPluginId());
  188. // If the dependency is marked as optional, its class loader might not be available.
  189. if (classLoader == null && dependency.isOptional()) {
  190. continue;
  191. }
  192. try {
  193. return classLoader.loadClass(className);
  194. } catch (ClassNotFoundException e) {
  195. // try next dependency
  196. }
  197. }
  198. return null;
  199. }
  200. protected URL findResourceFromDependencies(String name) {
  201. log.trace("Search in dependencies for resource '{}'", name);
  202. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  203. for (PluginDependency dependency : dependencies) {
  204. PluginClassLoader classLoader = (PluginClassLoader) pluginManager.getPluginClassLoader(dependency.getPluginId());
  205. // If the dependency is marked as optional, its class loader might not be available.
  206. if (classLoader == null && dependency.isOptional()) {
  207. continue;
  208. }
  209. URL url = classLoader.findResource(name);
  210. if (Objects.nonNull(url)) {
  211. return url;
  212. }
  213. }
  214. return null;
  215. }
  216. protected Collection<URL> findResourcesFromDependencies(String name) throws IOException {
  217. log.trace("Search in dependencies for resources '{}'", name);
  218. List<URL> results = new ArrayList<>();
  219. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  220. for (PluginDependency dependency : dependencies) {
  221. PluginClassLoader classLoader = (PluginClassLoader) pluginManager.getPluginClassLoader(dependency.getPluginId());
  222. // If the dependency is marked as optional, its class loader might not be available.
  223. if (classLoader == null && dependency.isOptional()) {
  224. continue;
  225. }
  226. results.addAll(Collections.list(classLoader.findResources(name)));
  227. }
  228. return results;
  229. }
  230. }