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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. protected PluginClasspath pluginClasspath;
  35. public DefaultPluginManager() {
  36. super();
  37. }
  38. public DefaultPluginManager(Path pluginsRoot) {
  39. super(pluginsRoot);
  40. }
  41. @Override
  42. protected PluginDescriptorFinder createPluginDescriptorFinder() {
  43. return new CompoundPluginDescriptorFinder()
  44. .add(new PropertiesPluginDescriptorFinder())
  45. .add(new ManifestPluginDescriptorFinder());
  46. }
  47. @Override
  48. protected ExtensionFinder createExtensionFinder() {
  49. DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this);
  50. addPluginStateListener(extensionFinder);
  51. return extensionFinder;
  52. }
  53. @Override
  54. protected PluginFactory createPluginFactory() {
  55. return new DefaultPluginFactory();
  56. }
  57. @Override
  58. protected ExtensionFactory createExtensionFactory() {
  59. return new DefaultExtensionFactory();
  60. }
  61. @Override
  62. protected PluginStatusProvider createPluginStatusProvider() {
  63. String configDir = System.getProperty(PLUGINS_DIR_CONFIG_PROPERTY_NAME);
  64. Path configPath = configDir != null ? Paths.get(configDir) : getPluginsRoot();
  65. return new DefaultPluginStatusProvider(configPath);
  66. }
  67. @Override
  68. protected PluginRepository createPluginRepository() {
  69. return new CompoundPluginRepository()
  70. .add(new JarPluginRepository(getPluginsRoot()))
  71. .add(new DefaultPluginRepository(getPluginsRoot(), isDevelopment()));
  72. }
  73. @Override
  74. protected PluginLoader createPluginLoader() {
  75. return new CompoundPluginLoader()
  76. .add(new JarPluginLoader(this))
  77. .add(new DefaultPluginLoader(this, pluginClasspath));
  78. }
  79. @Override
  80. protected VersionManager createVersionManager() {
  81. return new DefaultVersionManager();
  82. }
  83. /**
  84. * By default if {@link #isDevelopment()} returns {@code true} than a {@link DevelopmentPluginClasspath}
  85. * is returned, else this method returns {@link DefaultPluginClasspath}.
  86. */
  87. protected PluginClasspath createPluginClasspath() {
  88. return isDevelopment() ? new DevelopmentPluginClasspath() : new DefaultPluginClasspath();
  89. }
  90. @Override
  91. protected void initialize() {
  92. pluginClasspath = createPluginClasspath();
  93. super.initialize();
  94. if (isDevelopment()) {
  95. addPluginStateListener(new LoggingPluginStateListener());
  96. }
  97. log.info("PF4J version {} in '{}' mode", getVersion(), getRuntimeMode());
  98. }
  99. /**
  100. * Load a plugin from disk. If the path is a zip file, first unpack
  101. * @param pluginPath plugin location on disk
  102. * @return PluginWrapper for the loaded plugin or null if not loaded
  103. * @throws PluginException if problems during load
  104. */
  105. @Override
  106. protected PluginWrapper loadPluginFromPath(Path pluginPath) throws PluginException {
  107. // First unzip any ZIP files
  108. try {
  109. pluginPath = FileUtils.expandIfZip(pluginPath);
  110. } catch (Exception e) {
  111. log.warn("Failed to unzip " + pluginPath, e);
  112. return null;
  113. }
  114. return super.loadPluginFromPath(pluginPath);
  115. }
  116. }