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.

BasePluginLoader.java 3.0KB

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