Browse Source

Use logger instead of System.out.println for demo

pull/469/merge
Decebal Suiu 3 years ago
parent
commit
aa07dfd91e
  1. 62
      demo/app/src/main/java/org/pf4j/demo/Boot.java
  2. 6
      demo/plugins/plugin1/src/main/java/org/pf4j/demo/welcome/WelcomePlugin.java
  3. 4
      demo/plugins/plugin2/src/main/java/org/pf4j/demo/hello/HelloPlugin.java

62
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<Greeting> 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<String> 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<Class<? extends Greeting>> greetingsClasses = pluginManager.getExtensionClasses(Greeting.class);
for (Class<? extends Greeting> greeting : greetingsClasses) {
System.out.println(" Class: " + greeting.getCanonicalName());
log.info(" Class: {}", greeting.getCanonicalName());
}
// print extensions ids for each started plugin
List<PluginWrapper> 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<Greeting> 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;
}
};
}
}

6
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

4
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)

Loading…
Cancel
Save