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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2012 Decebal Suiu
  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. * 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. *
  29. * @author Decebal Suiu
  30. */
  31. public class PluginClassLoader extends URLClassLoader {
  32. private static final Logger log = LoggerFactory.getLogger(PluginClassLoader.class);
  33. private static final String PLUGIN_PACKAGE_PREFIX = "org.pf4j.";
  34. private PluginManager pluginManager;
  35. private PluginDescriptor pluginDescriptor;
  36. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent) {
  37. super(new URL[0], parent);
  38. this.pluginManager = pluginManager;
  39. this.pluginDescriptor = pluginDescriptor;
  40. }
  41. @Override
  42. public void addURL(URL url) {
  43. log.debug("Add '{}'", url);
  44. super.addURL(url);
  45. }
  46. public void addFile(File file) {
  47. try {
  48. addURL(file.getCanonicalFile().toURI().toURL());
  49. } catch (IOException e) {
  50. // throw new RuntimeException(e);
  51. log.error(e.getMessage(), e);
  52. }
  53. }
  54. /**
  55. * Uses a child first delegation model rather than the standard parent first.
  56. * If the requested class cannot be found in this class loader, the parent class loader will be consulted
  57. * via the standard {@link ClassLoader#loadClass(String)} mechanism.
  58. */
  59. @Override
  60. public Class<?> loadClass(String className) throws ClassNotFoundException {
  61. synchronized (getClassLoadingLock(className)) {
  62. log.trace("Received request to load class '{}'", className);
  63. // if the class it's a part of the plugin engine use parent class loader
  64. if (className.startsWith(PLUGIN_PACKAGE_PREFIX)) {
  65. log.trace("Delegate the loading of class '{}' to parent", className);
  66. try {
  67. return getClass().getClassLoader().loadClass(className);
  68. } catch (ClassNotFoundException e) {
  69. // try next step
  70. }
  71. }
  72. // second check whether it's already been loaded
  73. Class<?> clazz = findLoadedClass(className);
  74. if (clazz != null) {
  75. log.trace("Found loaded class '{}'", className);
  76. return clazz;
  77. }
  78. // nope, try to load locally
  79. try {
  80. clazz = findClass(className);
  81. log.trace("Found class '{}' in plugin classpath", className);
  82. return clazz;
  83. } catch (ClassNotFoundException e) {
  84. // try next step
  85. }
  86. // look in dependencies
  87. log.trace("Search in dependencies for class '{}'", className);
  88. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  89. for (PluginDependency dependency : dependencies) {
  90. ClassLoader classLoader = pluginManager.getPluginClassLoader(dependency.getPluginId());
  91. try {
  92. return classLoader.loadClass(className);
  93. } catch (ClassNotFoundException e) {
  94. // try next dependency
  95. }
  96. }
  97. log.trace("Couldn't find class '{}' in plugin classpath. Delegating to parent", className);
  98. // use the standard ClassLoader (which follows normal parent delegation)
  99. return super.loadClass(className);
  100. }
  101. }
  102. /**
  103. * Load the named resource from this plugin.
  104. * This implementation checks the plugin's classpath first then delegates to the parent.
  105. *
  106. * @param name the name of the resource.
  107. * @return the URL to the resource, <code>null</code> if the resource was not found.
  108. */
  109. @Override
  110. public URL getResource(String name) {
  111. log.trace("Trying to find resource '{}' in plugin classpath", name);
  112. URL url = findResource(name);
  113. if (url != null) {
  114. log.trace("Found resource '{}' in plugin classpath", name);
  115. return url;
  116. }
  117. log.trace("Couldn't find resource '{}' in plugin classpath. Delegating to parent");
  118. return super.getResource(name);
  119. }
  120. @Override
  121. public URL findResource(String name) {
  122. return super.findResource(name);
  123. }
  124. }