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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.lang.StringUtils;
  18. import org.pf4j.PluginManager;
  19. import org.pf4j.PluginWrapper;
  20. import org.pf4j.demo.api.Greeting;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import java.util.List;
  24. import java.util.Set;
  25. /**
  26. * A boot class that start the demo.
  27. *
  28. * @author Decebal Suiu
  29. */
  30. public class Boot {
  31. private static final Logger log = LoggerFactory.getLogger(Boot.class);
  32. public static void main(String[] args) {
  33. // print logo
  34. printLogo();
  35. // create the plugin manager
  36. PluginManager pluginManager = new DemoPluginManager();
  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. // retrieves the extensions for Greeting extension point
  44. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  45. log.info("Found {} extensions for extension point '{}'", greetings.size(), Greeting.class.getName());
  46. for (Greeting greeting : greetings) {
  47. log.info(">>> {}", greeting.getGreeting());
  48. }
  49. // print extensions from classpath (non plugin)
  50. log.info("Extensions added by classpath:");
  51. Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null);
  52. for (String extension : extensionClassNames) {
  53. log.info(" {}", extension);
  54. }
  55. log.info("Extension classes by classpath:");
  56. List<Class<? extends Greeting>> greetingsClasses = pluginManager.getExtensionClasses(Greeting.class);
  57. for (Class<? extends Greeting> greeting : greetingsClasses) {
  58. log.info(" Class: {}", greeting.getCanonicalName());
  59. }
  60. // print extensions ids for each started plugin
  61. List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
  62. for (PluginWrapper plugin : startedPlugins) {
  63. String pluginId = plugin.getDescriptor().getPluginId();
  64. log.info("Extensions added by plugin '{}}':", pluginId);
  65. extensionClassNames = pluginManager.getExtensionClassNames(pluginId);
  66. for (String extension : extensionClassNames) {
  67. log.info(" {}", extension);
  68. }
  69. }
  70. // print the extensions instances for Greeting extension point for each started plugin
  71. for (PluginWrapper plugin : startedPlugins) {
  72. String pluginId = plugin.getDescriptor().getPluginId();
  73. log.info("Extensions instances added by plugin '{}' for extension point '{}':", pluginId, Greeting.class.getName());
  74. List<Greeting> extensions = pluginManager.getExtensions(Greeting.class, pluginId);
  75. for (Object extension : extensions) {
  76. log.info(" {}", extension);
  77. }
  78. }
  79. // print extensions instances from classpath (non plugin)
  80. log.info("Extensions instances added by classpath:");
  81. List<?> extensions = pluginManager.getExtensions((String) null);
  82. for (Object extension : extensions) {
  83. log.info(" {}", extension);
  84. }
  85. // print extensions instances for each started plugin
  86. for (PluginWrapper plugin : startedPlugins) {
  87. String pluginId = plugin.getDescriptor().getPluginId();
  88. log.info("Extensions instances added by plugin '{}':", pluginId);
  89. extensions = pluginManager.getExtensions(pluginId);
  90. for (Object extension : extensions) {
  91. log.info(" {}", extension);
  92. }
  93. }
  94. // stop the plugins
  95. pluginManager.stopPlugins();
  96. /*
  97. Runtime.getRuntime().addShutdownHook(new Thread() {
  98. @Override
  99. public void run() {
  100. pluginManager.stopPlugins();
  101. }
  102. });
  103. */
  104. }
  105. private static void printLogo() {
  106. log.info(StringUtils.repeat("#", 40));
  107. log.info(StringUtils.center("PF4J-DEMO", 40));
  108. log.info(StringUtils.repeat("#", 40));
  109. }
  110. }