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

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