From aa07dfd91e82c90b8cc0d6b0ab6f2ae56c077f05 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Thu, 30 Jun 2022 00:40:40 +0300 Subject: [PATCH] Use logger instead of System.out.println for demo --- .../app/src/main/java/org/pf4j/demo/Boot.java | 62 +++++++++++-------- .../org/pf4j/demo/welcome/WelcomePlugin.java | 6 +- .../java/org/pf4j/demo/hello/HelloPlugin.java | 4 +- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/demo/app/src/main/java/org/pf4j/demo/Boot.java b/demo/app/src/main/java/org/pf4j/demo/Boot.java index 066a207..dfbc2ed 100644 --- a/demo/app/src/main/java/org/pf4j/demo/Boot.java +++ b/demo/app/src/main/java/org/pf4j/demo/Boot.java @@ -22,6 +22,8 @@ import org.pf4j.ExtensionFinder; import org.pf4j.PluginManager; import org.pf4j.PluginWrapper; import org.pf4j.demo.api.Greeting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Set; @@ -33,21 +35,14 @@ import java.util.Set; */ public class Boot { + private static final Logger log = LoggerFactory.getLogger(Boot.class); + public static void main(String[] args) { // print logo printLogo(); // create the plugin manager - final PluginManager pluginManager = new DefaultPluginManager() { - - protected ExtensionFinder createExtensionFinder() { - DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder(); - extensionFinder.addServiceProviderExtensionFinder(); // to activate "HowdyGreeting" extension - - return extensionFinder; - } - - }; + PluginManager pluginManager = createPluginManager(); // load the plugins pluginManager.loadPlugins(); @@ -60,59 +55,59 @@ public class Boot { // retrieves the extensions for Greeting extension point List greetings = pluginManager.getExtensions(Greeting.class); - System.out.println(String.format("Found %d extensions for extension point '%s'", greetings.size(), Greeting.class.getName())); + log.info("Found {} extensions for extension point '{}'", greetings.size(), Greeting.class.getName()); for (Greeting greeting : greetings) { - System.out.println(">>> " + greeting.getGreeting()); + log.info(">>> {}", greeting.getGreeting()); } // print extensions from classpath (non plugin) - System.out.println("Extensions added by classpath:"); + log.info("Extensions added by classpath:"); Set extensionClassNames = pluginManager.getExtensionClassNames(null); for (String extension : extensionClassNames) { - System.out.println(" " + extension); + log.info(" {}", extension); } - System.out.println("Extension classes by classpath:"); + log.info("Extension classes by classpath:"); List> greetingsClasses = pluginManager.getExtensionClasses(Greeting.class); for (Class greeting : greetingsClasses) { - System.out.println(" Class: " + greeting.getCanonicalName()); + log.info(" Class: {}", greeting.getCanonicalName()); } // print extensions ids for each started plugin List startedPlugins = pluginManager.getStartedPlugins(); for (PluginWrapper plugin : startedPlugins) { String pluginId = plugin.getDescriptor().getPluginId(); - System.out.println(String.format("Extensions added by plugin '%s':", pluginId)); + log.info("Extensions added by plugin '{}}':", pluginId); extensionClassNames = pluginManager.getExtensionClassNames(pluginId); for (String extension : extensionClassNames) { - System.out.println(" " + extension); + log.info(" {}", extension); } } // print the extensions instances for Greeting extension point for each started plugin for (PluginWrapper plugin : startedPlugins) { String pluginId = plugin.getDescriptor().getPluginId(); - System.out.println(String.format("Extensions instances added by plugin '%s' for extension point '%s':", pluginId, Greeting.class.getName())); + log.info("Extensions instances added by plugin '{}' for extension point '{}':", pluginId, Greeting.class.getName()); List extensions = pluginManager.getExtensions(Greeting.class, pluginId); for (Object extension : extensions) { - System.out.println(" " + extension); + log.info(" {}", extension); } } // print extensions instances from classpath (non plugin) - System.out.println("Extensions instances added by classpath:"); + log.info("Extensions instances added by classpath:"); List extensions = pluginManager.getExtensions((String) null); for (Object extension : extensions) { - System.out.println(" " + extension); + log.info(" {}", extension); } // print extensions instances for each started plugin for (PluginWrapper plugin : startedPlugins) { String pluginId = plugin.getDescriptor().getPluginId(); - System.out.println(String.format("Extensions instances added by plugin '%s':", pluginId)); + log.info("Extensions instances added by plugin '{}':", pluginId); extensions = pluginManager.getExtensions(pluginId); for (Object extension : extensions) { - System.out.println(" " + extension); + log.info(" {}", extension); } } @@ -131,9 +126,22 @@ public class Boot { } private static void printLogo() { - System.out.println(StringUtils.repeat("#", 40)); - System.out.println(StringUtils.center("PF4J-DEMO", 40)); - System.out.println(StringUtils.repeat("#", 40)); + log.info(StringUtils.repeat("#", 40)); + log.info(StringUtils.center("PF4J-DEMO", 40)); + log.info(StringUtils.repeat("#", 40)); + } + + private static PluginManager createPluginManager() { + return new DefaultPluginManager() { + + protected ExtensionFinder createExtensionFinder() { + DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder(); + extensionFinder.addServiceProviderExtensionFinder(); // to activate "HowdyGreeting" extension + + return extensionFinder; + } + + }; } } diff --git a/demo/plugins/plugin1/src/main/java/org/pf4j/demo/welcome/WelcomePlugin.java b/demo/plugins/plugin1/src/main/java/org/pf4j/demo/welcome/WelcomePlugin.java index 8dce434..a0cecab 100644 --- a/demo/plugins/plugin1/src/main/java/org/pf4j/demo/welcome/WelcomePlugin.java +++ b/demo/plugins/plugin1/src/main/java/org/pf4j/demo/welcome/WelcomePlugin.java @@ -34,16 +34,16 @@ public class WelcomePlugin extends Plugin { @Override public void start() { - System.out.println("WelcomePlugin.start()"); + log.info("WelcomePlugin.start()"); // for testing the development mode if (RuntimeMode.DEVELOPMENT.equals(wrapper.getRuntimeMode())) { - System.out.println(StringUtils.upperCase("WelcomePlugin")); + log.info(StringUtils.upperCase("WelcomePlugin")); } } @Override public void stop() { - System.out.println("WelcomePlugin.stop()"); + log.info("WelcomePlugin.stop()"); } @Extension diff --git a/demo/plugins/plugin2/src/main/java/org/pf4j/demo/hello/HelloPlugin.java b/demo/plugins/plugin2/src/main/java/org/pf4j/demo/hello/HelloPlugin.java index 0f02a08..3b7c9ce 100644 --- a/demo/plugins/plugin2/src/main/java/org/pf4j/demo/hello/HelloPlugin.java +++ b/demo/plugins/plugin2/src/main/java/org/pf4j/demo/hello/HelloPlugin.java @@ -33,12 +33,12 @@ public class HelloPlugin extends Plugin { @Override public void start() { - System.out.println("HelloPlugin.start()"); + log.info("HelloPlugin.start()"); } @Override public void stop() { - System.out.println("HelloPlugin.stop()"); + log.info("HelloPlugin.stop()"); } @Extension(ordinal=1) -- 2.39.5