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.2KB

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