Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PluginClassLoader.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2012 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j;
  14. import java.net.URL;
  15. import java.net.URLClassLoader;
  16. import java.util.List;
  17. /**
  18. * One instance of this class should be created by plugin manager for every available plug-in.
  19. *
  20. * @author Decebal Suiu
  21. */
  22. class PluginClassLoader extends URLClassLoader {
  23. private static final String JAVA_PACKAGE_PREFIX = "java.";
  24. private static final String JAVAX_PACKAGE_PREFIX = "javax.";
  25. private static final String PLUGIN_PACKAGE_PREFIX = "ro.fortsoft.pf4j.";
  26. private PluginManager pluginManager;
  27. private PluginDescriptor pluginDescriptor;
  28. public PluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, ClassLoader parent) {
  29. super(new URL[0], parent);
  30. this.pluginManager = pluginManager;
  31. this.pluginDescriptor = pluginDescriptor;
  32. }
  33. @Override
  34. public void addURL(URL url) {
  35. super.addURL(url);
  36. }
  37. @Override
  38. public Class<?> loadClass(String className) throws ClassNotFoundException {
  39. // System.out.println(">>>" + className);
  40. // first check whether it's a system class, delegate to the system loader
  41. if (className.startsWith(JAVA_PACKAGE_PREFIX) || className.startsWith(JAVAX_PACKAGE_PREFIX)) {
  42. return findSystemClass(className);
  43. }
  44. // second check whether it's already been loaded
  45. Class<?> loadedClass = findLoadedClass(className);
  46. if (loadedClass != null) {
  47. return loadedClass;
  48. }
  49. // nope, try to load locally
  50. try {
  51. return findClass(className);
  52. } catch (ClassNotFoundException e) {
  53. // try next step
  54. }
  55. // if the class it's a part of the plugin engine use parent class loader
  56. if (className.startsWith(PLUGIN_PACKAGE_PREFIX)) {
  57. try {
  58. return PluginClassLoader.class.getClassLoader().loadClass(className);
  59. } catch (ClassNotFoundException e) {
  60. // try next step
  61. }
  62. }
  63. // look in dependencies
  64. List<PluginDependency> dependencies = pluginDescriptor.getDependencies();
  65. for (PluginDependency dependency : dependencies) {
  66. PluginClassLoader classLoader = pluginManager.getPluginClassLoader(dependency.getPluginId());
  67. try {
  68. return classLoader.loadClass(className);
  69. } catch (ClassNotFoundException e) {
  70. // try next dependency
  71. }
  72. }
  73. // use the standard URLClassLoader (which follows normal parent delegation)
  74. return super.loadClass(className);
  75. }
  76. }