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.

DefaultPluginLoader.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2016 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.pf4j.util.FileUtils;
  18. import java.io.File;
  19. import java.nio.file.Path;
  20. import java.util.List;
  21. /**
  22. * Load all information needed by a plugin.
  23. * This means add to classpath all jar files from {@code lib} directory
  24. * and all class files from {@code classes}.
  25. *
  26. * @author Decebal Suiu
  27. */
  28. public class DefaultPluginLoader implements PluginLoader {
  29. protected PluginManager pluginManager;
  30. protected PluginClasspath pluginClasspath;
  31. public DefaultPluginLoader(PluginManager pluginManager, PluginClasspath pluginClasspath) {
  32. this.pluginManager = pluginManager;
  33. this.pluginClasspath = pluginClasspath;
  34. }
  35. @Override
  36. public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) {
  37. PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginPath, pluginDescriptor);
  38. loadClasses(pluginPath, pluginClassLoader);
  39. loadJars(pluginPath, pluginClassLoader);
  40. return pluginClassLoader;
  41. }
  42. protected PluginClassLoader createPluginClassLoader(Path pluginPath, PluginDescriptor pluginDescriptor) {
  43. return new PluginClassLoader(pluginManager, pluginDescriptor, getClass().getClassLoader());
  44. }
  45. /**
  46. * Add all {@code *.class} files from {@code classes} directories to plugin class loader.
  47. */
  48. protected void loadClasses(Path pluginPath, PluginClassLoader pluginClassLoader) {
  49. for (String directory : pluginClasspath.getClassesDirectories()) {
  50. File file = pluginPath.resolve(directory).toFile();
  51. if (file.exists() && file.isDirectory()) {
  52. pluginClassLoader.addFile(file);
  53. }
  54. }
  55. }
  56. /**
  57. * Add all {@code *.jar} files from {@code lib} directories to plugin class loader.
  58. */
  59. protected void loadJars(Path pluginPath, PluginClassLoader pluginClassLoader) {
  60. for (String libDirectory : pluginClasspath.getLibDirectories()) {
  61. Path file = pluginPath.resolve(libDirectory);
  62. List<File> jars = FileUtils.getJars(file);
  63. for (File jar : jars) {
  64. pluginClassLoader.addFile(jar);
  65. }
  66. }
  67. }
  68. }