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

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