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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2012 Decebal Suiu
  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. @Deprecated
  34. public DefaultPluginManager(File pluginsDir) {
  35. this(pluginsDir.toPath());
  36. }
  37. public DefaultPluginManager(Path pluginsRoot) {
  38. super(pluginsRoot);
  39. }
  40. /**
  41. * By default if {@link DefaultPluginManager#isDevelopment()} returns {@code true}
  42. * than a {@link PropertiesPluginDescriptorFinder} is returned
  43. * else this method returns {@link DefaultPluginDescriptorFinder}.
  44. */
  45. @Override
  46. protected PluginDescriptorFinder createPluginDescriptorFinder() {
  47. return isDevelopment() ? new PropertiesPluginDescriptorFinder() : new DefaultPluginDescriptorFinder(pluginClasspath);
  48. }
  49. @Override
  50. protected ExtensionFinder createExtensionFinder() {
  51. DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this);
  52. addPluginStateListener(extensionFinder);
  53. return extensionFinder;
  54. }
  55. @Override
  56. protected PluginFactory createPluginFactory() {
  57. return new DefaultPluginFactory();
  58. }
  59. @Override
  60. protected ExtensionFactory createExtensionFactory() {
  61. return new DefaultExtensionFactory();
  62. }
  63. @Override
  64. protected PluginStatusProvider createPluginStatusProvider() {
  65. return new DefaultPluginStatusProvider(getPluginsRoot());
  66. }
  67. @Override
  68. protected PluginRepository createPluginRepository() {
  69. return new DefaultPluginRepository(getPluginsRoot(), isDevelopment());
  70. }
  71. @Override
  72. protected PluginLoader createPluginLoader() {
  73. return new DefaultPluginLoader(this, pluginClasspath);
  74. }
  75. @Override
  76. protected VersionManager createVersionManager() {
  77. return new DefaultVersionManager();
  78. }
  79. /**
  80. * By default if {@link DefaultPluginManager#isDevelopment()} returns true
  81. * than a {@link DevelopmentPluginClasspath} is returned
  82. * else this method returns {@link DefaultPluginClasspath}.
  83. */
  84. protected PluginClasspath createPluginClasspath() {
  85. return isDevelopment() ? new DevelopmentPluginClasspath() : new DefaultPluginClasspath();
  86. }
  87. @Override
  88. protected void initialize() {
  89. pluginClasspath = createPluginClasspath();
  90. super.initialize();
  91. log.info("PF4J version {} in '{}' mode", getVersion(), getRuntimeMode());
  92. }
  93. /**
  94. * Load a plugin from disk. If the path is a zip file, first unpack
  95. * @param pluginPath plugin location on disk
  96. * @return PluginWrapper for the loaded plugin or null if not loaded
  97. * @throws PluginException if problems during load
  98. */
  99. @Override
  100. protected PluginWrapper loadPluginFromPath(Path pluginPath) throws PluginException {
  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. }