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

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