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

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