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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2012 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j.demo;
  14. import java.util.List;
  15. import java.util.Set;
  16. import org.apache.commons.lang3.StringUtils;
  17. import ro.fortsoft.pf4j.DefaultPluginManager;
  18. import ro.fortsoft.pf4j.PluginManager;
  19. import ro.fortsoft.pf4j.PluginWrapper;
  20. import ro.fortsoft.pf4j.demo.api.Greeting;
  21. /**
  22. * A boot class that start the demo.
  23. *
  24. * @author Decebal Suiu
  25. */
  26. public class Boot {
  27. public static void main(String[] args) {
  28. // print logo
  29. printLogo();
  30. // create the plugin manager
  31. final PluginManager pluginManager = new DefaultPluginManager();
  32. // load the plugins
  33. pluginManager.loadPlugins();
  34. // enable a disabled plugin
  35. // pluginManager.enablePlugin("welcome-plugin");
  36. // start (active/resolved) the plugins
  37. pluginManager.startPlugins();
  38. System.out.println("Plugindirectory: ");
  39. System.out.println("\t" + System.getProperty("pf4j.pluginsDir", "plugins") + "\n");
  40. // retrieves the extensions for Greeting extension point
  41. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  42. System.out.println(String.format("Found %d extensions for extension point '%s'", greetings.size(), Greeting.class.getName()));
  43. for (Greeting greeting : greetings) {
  44. System.out.println(">>> " + greeting.getGreeting());
  45. }
  46. // // print extensions from classpath (non plugin)
  47. // System.out.println(String.format("Extensions added by classpath:"));
  48. // Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null);
  49. // for (String extension : extensionClassNames) {
  50. // System.out.println(" " + extension);
  51. // }
  52. // print extensions for each started plugin
  53. List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
  54. for (PluginWrapper plugin : startedPlugins) {
  55. String pluginId = plugin.getDescriptor().getPluginId();
  56. System.out.println(String.format("Extensions added by plugin '%s':", pluginId));
  57. // extensionClassNames = pluginManager.getExtensionClassNames(pluginId);
  58. // for (String extension : extensionClassNames) {
  59. // System.out.println(" " + extension);
  60. // }
  61. }
  62. // stop the plugins
  63. pluginManager.stopPlugins();
  64. /*
  65. Runtime.getRuntime().addShutdownHook(new Thread() {
  66. @Override
  67. public void run() {
  68. pluginManager.stopPlugins();
  69. }
  70. });
  71. */
  72. }
  73. private static void printLogo() {
  74. System.out.println(StringUtils.repeat("#", 40));
  75. System.out.println(StringUtils.center("PF4J-DEMO", 40));
  76. System.out.println(StringUtils.repeat("#", 40));
  77. }
  78. }