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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. protected PluginClasspath pluginClasspath;
  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("pf4j.pluginsConfigDir");
  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 JarPluginRepository(getPluginsRoot()))
  70. .add(new DefaultPluginRepository(getPluginsRoot(), isDevelopment()));
  71. }
  72. @Override
  73. protected PluginLoader createPluginLoader() {
  74. return new CompoundPluginLoader()
  75. .add(new JarPluginLoader(this))
  76. .add(new DefaultPluginLoader(this, pluginClasspath));
  77. }
  78. @Override
  79. protected VersionManager createVersionManager() {
  80. return new DefaultVersionManager();
  81. }
  82. /**
  83. * By default if {@link DefaultPluginManager#isDevelopment()} returns true
  84. * than a {@link DevelopmentPluginClasspath} is returned
  85. * 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. }