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.

DefaultPluginManager.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import java.nio.file.Path;
  21. import java.nio.file.Paths;
  22. import java.util.List;
  23. /**
  24. * Default implementation of the {@link PluginManager} interface.
  25. * In essence it is a {@link ZipPluginManager} plus a {@link JarPluginManager}.
  26. * So, it can load plugins from jar and zip, simultaneous.
  27. *
  28. * <p>This class is not thread-safe.
  29. *
  30. * @author Decebal Suiu
  31. */
  32. public class DefaultPluginManager extends AbstractPluginManager {
  33. private static final Logger log = LoggerFactory.getLogger(DefaultPluginManager.class);
  34. public static final String PLUGINS_DIR_CONFIG_PROPERTY_NAME = "pf4j.pluginsConfigDir";
  35. public DefaultPluginManager() {
  36. super();
  37. }
  38. public DefaultPluginManager(Path... pluginsRoots) {
  39. super(pluginsRoots);
  40. }
  41. public DefaultPluginManager(List<Path> pluginsRoots) {
  42. super(pluginsRoots);
  43. }
  44. @Override
  45. protected PluginDescriptorFinder createPluginDescriptorFinder() {
  46. return new CompoundPluginDescriptorFinder()
  47. .add(new PropertiesPluginDescriptorFinder())
  48. .add(new ManifestPluginDescriptorFinder());
  49. }
  50. @Override
  51. protected ExtensionFinder createExtensionFinder() {
  52. DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this);
  53. addPluginStateListener(extensionFinder);
  54. return extensionFinder;
  55. }
  56. @Override
  57. protected PluginFactory createPluginFactory() {
  58. return new DefaultPluginFactory();
  59. }
  60. @Override
  61. protected ExtensionFactory createExtensionFactory() {
  62. return new DefaultExtensionFactory();
  63. }
  64. @Override
  65. protected PluginStatusProvider createPluginStatusProvider() {
  66. String configDir = System.getProperty(PLUGINS_DIR_CONFIG_PROPERTY_NAME);
  67. Path configPath = configDir != null
  68. ? Paths.get(configDir)
  69. : getPluginsRoots().stream()
  70. .findFirst()
  71. .orElseThrow(() -> new IllegalArgumentException("No pluginsRoot configured"));
  72. return new DefaultPluginStatusProvider(configPath);
  73. }
  74. @Override
  75. protected PluginRepository createPluginRepository() {
  76. return new CompoundPluginRepository()
  77. .add(new DevelopmentPluginRepository(getPluginsRoots()), this::isDevelopment)
  78. .add(new JarPluginRepository(getPluginsRoots()), this::isNotDevelopment)
  79. .add(new DefaultPluginRepository(getPluginsRoots()), this::isNotDevelopment);
  80. }
  81. @Override
  82. protected PluginLoader createPluginLoader() {
  83. return new CompoundPluginLoader()
  84. .add(new DevelopmentPluginLoader(this), this::isDevelopment)
  85. .add(new JarPluginLoader(this), this::isNotDevelopment)
  86. .add(new DefaultPluginLoader(this), this::isNotDevelopment);
  87. }
  88. @Override
  89. protected VersionManager createVersionManager() {
  90. return new DefaultVersionManager();
  91. }
  92. @Override
  93. protected void initialize() {
  94. super.initialize();
  95. if (isDevelopment()) {
  96. addPluginStateListener(new LoggingPluginStateListener());
  97. }
  98. log.info("PF4J version {} in '{}' mode", getVersion(), getRuntimeMode());
  99. }
  100. /**
  101. * Load a plugin from disk. If the path is a zip file, first unpack.
  102. *
  103. * @param pluginPath plugin location on disk
  104. * @return PluginWrapper for the loaded plugin or null if not loaded
  105. * @throws PluginRuntimeException if problems during load
  106. */
  107. @Override
  108. protected PluginWrapper loadPluginFromPath(Path pluginPath) {
  109. // First unzip any ZIP files
  110. try {
  111. pluginPath = FileUtils.expandIfZip(pluginPath);
  112. } catch (Exception e) {
  113. log.warn("Failed to unzip " + pluginPath, e);
  114. return null;
  115. }
  116. return super.loadPluginFromPath(pluginPath);
  117. }
  118. }