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.

Boot.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.demo;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.pf4j.CompoundPluginDescriptorFinder;
  19. import org.pf4j.ManifestPluginDescriptorFinder;
  20. import org.pf4j.PropertiesPluginDescriptorFinder;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.pf4j.DefaultPluginManager;
  24. import org.pf4j.PluginManager;
  25. import org.pf4j.PluginWrapper;
  26. import org.pf4j.demo.api.Greeting;
  27. import java.util.List;
  28. /**
  29. * A boot class that start the demo.
  30. *
  31. * @author Decebal Suiu
  32. */
  33. public class Boot {
  34. private static final Logger logger = LoggerFactory.getLogger(Boot.class);
  35. public static void main(String[] args) {
  36. // print logo
  37. printLogo();
  38. // create the plugin manager
  39. final PluginManager pluginManager = new DefaultPluginManager() {
  40. @Override
  41. protected CompoundPluginDescriptorFinder createPluginDescriptorFinder() {
  42. return new CompoundPluginDescriptorFinder()
  43. // Demo is using the Manifest file
  44. // PropertiesPluginDescriptorFinder is commented out just to avoid error log
  45. //.add(new PropertiesPluginDescriptorFinder())
  46. .add(new ManifestPluginDescriptorFinder());
  47. }
  48. };
  49. // load the plugins
  50. pluginManager.loadPlugins();
  51. // enable a disabled plugin
  52. // pluginManager.enablePlugin("welcome-plugin");
  53. // start (active/resolved) the plugins
  54. pluginManager.startPlugins();
  55. logger.info("Plugindirectory: ");
  56. logger.info("\t" + System.getProperty("pf4j.pluginsDir", "plugins") + "\n");
  57. // retrieves the extensions for Greeting extension point
  58. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  59. logger.info(String.format("Found %d extensions for extension point '%s'", greetings.size(), Greeting.class.getName()));
  60. for (Greeting greeting : greetings) {
  61. logger.info(">>> " + greeting.getGreeting());
  62. }
  63. // // print extensions from classpath (non plugin)
  64. // logger.info(String.format("Extensions added by classpath:"));
  65. // Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null);
  66. // for (String extension : extensionClassNames) {
  67. // logger.info(" " + extension);
  68. // }
  69. // print extensions for each started plugin
  70. List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
  71. for (PluginWrapper plugin : startedPlugins) {
  72. String pluginId = plugin.getDescriptor().getPluginId();
  73. logger.info(String.format("Extensions added by plugin '%s':", pluginId));
  74. // extensionClassNames = pluginManager.getExtensionClassNames(pluginId);
  75. // for (String extension : extensionClassNames) {
  76. // logger.info(" " + extension);
  77. // }
  78. }
  79. // stop the plugins
  80. pluginManager.stopPlugins();
  81. /*
  82. Runtime.getRuntime().addShutdownHook(new Thread() {
  83. @Override
  84. public void run() {
  85. pluginManager.stopPlugins();
  86. }
  87. });
  88. */
  89. }
  90. private static void printLogo() {
  91. logger.info(StringUtils.repeat("#", 40));
  92. logger.info(StringUtils.center("PF4J-DEMO", 40));
  93. logger.info(StringUtils.repeat("#", 40));
  94. }
  95. }