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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.Collections;
  25. import java.util.Enumeration;
  26. import java.util.List;
  27. /**
  28. * One instance of this class should be created by plugin manager for every available plug-in.
  29. * By default, this class loader is a Parent Last ClassLoader - it loads the classes from the plugin's jars
  30. * before delegating to the parent class loader.
  31. * Use {@link #parentFirst} to change the loading strategy.
  32. *
  33. * @author Decebal Suiu
  34. */
  35. public class PluginClassLoader extends URLClassLoader {
  36. private static final Logger log = LoggerFactory.getLogger(PluginClassLoader.class);
  37. private static final String JAVA_PACKAGE_PREFIX = "java.";
  38. private static final String PLUGIN_PACKAGE_PREFIX = "org.pf4j.";
  39. private PluginManager pluginManager;
  40. private PluginDescriptor pluginDescriptor;
  41. private boolean parentFirst;
  42. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent) {
  43. this(pluginManager, pluginDescriptor, parent, false);
  44. }
  45. /**
  46. * If {@code parentFirst} is {@code true}, indicates that the parent {@link ClassLoader} should be consulted
  47. * before trying to load the a class through this loader.
  48. */
  49. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent, boolean parentFirst) {
  50. super(new URL[0], parent);
  51. this.pluginManager = pluginManager;
  52. this.pluginDescriptor = pluginDescriptor;
  53. this.parentFirst = parentFirst;
  54. }
  55. @Override
  56. public void addURL(URL url) {
  57. log.debug("Add '{}'", url);
  58. super.addURL(url);
  59. }
  60. public void addFile(File file) {
  61. try {
  62. addURL(file.getCanonicalFile().toURI().toURL());
  63. } catch (IOException e) {
  64. // throw new RuntimeException(e);
  65. log.error(e.getMessage(), e);
  66. }
  67. }
  68. /**
  69. * By default, it uses a child first delegation model rather than the standard parent first.
  70. * If the requested class cannot be found in this class loader, the parent class loader will be consulted
  71. * via the standard {@link ClassLoader#loadClass(String)} mechanism.
  72. * Use {@link #parentFirst} to change the loading strategy.
  73. */
  74. @Override
  75. public Class<?> loadClass(String className) throws ClassNotFoundException {
  76. synchronized (getClassLoadingLock(className)) {
  77. // first check whether it's a system class, delegate to the system loader
  78. if (className.startsWith(JAVA_PACKAGE_PREFIX)) {
  79. return findSystemClass(className);
  80. }
  81. // if the class is part of the plugin engine use parent class loader
  82. if (className.startsWith(PLUGIN_PACKAGE_PREFIX) && !className.startsWith("org.pf4j.demo")) {
  83. // log.trace("Delegate the loading of PF4J class '{}' to parent", className);
  84. return getParent().loadClass(className);
  85. }
  86. log.trace("Received request to load class '{}'", className);
  87. // second check whether it's already been loaded
  88. Class<?> loadedClass = findLoadedClass(className);
  89. if (loadedClass != null) {
  90. log.trace("Found loaded class '{}'", className);
  91. return loadedClass;
  92. }
  93. if (!parentFirst) {
  94. // nope, try to load locally
  95. try {
  96. loadedClass = findClass(className);
  97. log.trace("Found class '{}' in plugin classpath", className);
  98. return loadedClass;
  99. } catch (ClassNotFoundException e) {
  100. // try next step
  101. }
  102. // look in dependencies
  103. loadedClass = loadClassFromDependencies(className);
  104. if (loadedClass != null) {
  105. log.trace("Found class '{}' in dependencies", className);
  106. return loadedClass;
  107. }
  108. log.trace("Couldn't find class '{}' in plugin classpath. Delegating to parent", className);
  109. // use the standard ClassLoader (which follows normal parent delegation)
  110. return super.loadClass(className);
  111. } else {
  112. // try to load from parent
  113. try {
  114. return super.loadClass(className);
  115. } catch (ClassCastException e) {
  116. // try next step
  117. }
  118. log.trace("Couldn't find class '{}' in parent. Delegating to plugin classpath", className);
  119. // nope, try to load locally
  120. try {
  121. loadedClass = findClass(className);
  122. log.trace("Found class '{}' in plugin classpath", className);
  123. return loadedClass;
  124. } catch (ClassNotFoundException e) {
  125. // try next step
  126. }
  127. // look in dependencies
  128. loadedClass = loadClassFromDependencies(className);
  129. if (loadedClass != null) {
  130. log.trace("Found class '{}' in dependencies", className);
  131. return loadedClass;
  132. }
  133. throw new ClassNotFoundException(className);
  134. }
  135. }
  136. }
  137. /**
  138. * Load the named resource from this plugin.
  139. * By default, this implementation checks the plugin's classpath first then delegates to the parent.
  140. * Use {@link #parentFirst} to change the loading strategy.
  141. *
  142. * @param name the name of the resource.
  143. * @return the URL to the resource, {@code null} if the resource was not found.
  144. */
  145. @Override
  146. public URL getResource(String name) {
  147. log.trace("Received request to load resource '{}'", name);
  148. if (!parentFirst) {
  149. URL url = findResource(name);
  150. if (url != null) {
  151. log.trace("Found resource '{}' in plugin classpath", name);
  152. return url;
  153. }
  154. log.trace("Couldn't find resource '{}' in plugin classpath. Delegating to parent", name);
  155. return super.getResource(name);
  156. } else {
  157. URL url = super.getResource(name);
  158. if (url != null) {
  159. log.trace("Found resource '{}' in parent", name);
  160. return url;
  161. }
  162. log.trace("Couldn't find resource '{}' in parent", name);
  163. return findResource(name);
  164. }
  165. }
  166. @Override
  167. public Enumeration<URL> getResources(String name) throws IOException {
  168. if (!parentFirst) {
  169. List<URL> resources = new ArrayList<>();
  170. resources.addAll(Collections.list(findResources(name)));
  171. if (getParent() != null) {
  172. resources.addAll(Collections.list(getParent().getResources(name)));
  173. }
  174. return Collections.enumeration(resources);
  175. } else {
  176. return super.getResources(name);
  177. }
  178. }
  179. private Class<?> loadClassFromDependencies(String className) {
  180. log.trace("Search in dependencies for class '{}'", className);
  181. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  182. for (PluginDependency dependency : dependencies) {
  183. ClassLoader classLoader = pluginManager.getPluginClassLoader(dependency.getPluginId());
  184. // If the dependency is marked as optional, its class loader might not be available.
  185. if (classLoader == null && dependency.isOptional()) {
  186. continue;
  187. }
  188. try {
  189. return classLoader.loadClass(className);
  190. } catch (ClassNotFoundException e) {
  191. // try next dependency
  192. }
  193. }
  194. return null;
  195. }
  196. }