|
|
@ -22,6 +22,8 @@ import org.pf4j.ExtensionFinder; |
|
|
|
import org.pf4j.PluginManager; |
|
|
|
import org.pf4j.PluginManager; |
|
|
|
import org.pf4j.PluginWrapper; |
|
|
|
import org.pf4j.PluginWrapper; |
|
|
|
import org.pf4j.demo.api.Greeting; |
|
|
|
import org.pf4j.demo.api.Greeting; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
@ -33,21 +35,14 @@ import java.util.Set; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class Boot { |
|
|
|
public class Boot { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(Boot.class); |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) { |
|
|
|
// print logo
|
|
|
|
// print logo
|
|
|
|
printLogo(); |
|
|
|
printLogo(); |
|
|
|
|
|
|
|
|
|
|
|
// create the plugin manager
|
|
|
|
// create the plugin manager
|
|
|
|
final PluginManager pluginManager = new DefaultPluginManager() { |
|
|
|
PluginManager pluginManager = createPluginManager(); |
|
|
|
|
|
|
|
|
|
|
|
protected ExtensionFinder createExtensionFinder() { |
|
|
|
|
|
|
|
DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder(); |
|
|
|
|
|
|
|
extensionFinder.addServiceProviderExtensionFinder(); // to activate "HowdyGreeting" extension
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return extensionFinder; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// load the plugins
|
|
|
|
// load the plugins
|
|
|
|
pluginManager.loadPlugins(); |
|
|
|
pluginManager.loadPlugins(); |
|
|
@ -60,59 +55,59 @@ public class Boot { |
|
|
|
|
|
|
|
|
|
|
|
// retrieves the extensions for Greeting extension point
|
|
|
|
// retrieves the extensions for Greeting extension point
|
|
|
|
List<Greeting> greetings = pluginManager.getExtensions(Greeting.class); |
|
|
|
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) { |
|
|
|
for (Greeting greeting : greetings) { |
|
|
|
System.out.println(">>> " + greeting.getGreeting()); |
|
|
|
log.info(">>> {}", greeting.getGreeting()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// print extensions from classpath (non plugin)
|
|
|
|
// 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); |
|
|
|
Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null); |
|
|
|
for (String extension : extensionClassNames) { |
|
|
|
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); |
|
|
|
List<Class<? extends Greeting>> greetingsClasses = pluginManager.getExtensionClasses(Greeting.class); |
|
|
|
for (Class<? extends Greeting> greeting : greetingsClasses) { |
|
|
|
for (Class<? extends Greeting> greeting : greetingsClasses) { |
|
|
|
System.out.println(" Class: " + greeting.getCanonicalName()); |
|
|
|
log.info(" Class: {}", greeting.getCanonicalName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// print extensions ids for each started plugin
|
|
|
|
// print extensions ids for each started plugin
|
|
|
|
List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins(); |
|
|
|
List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins(); |
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
String pluginId = plugin.getDescriptor().getPluginId(); |
|
|
|
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); |
|
|
|
extensionClassNames = pluginManager.getExtensionClassNames(pluginId); |
|
|
|
for (String extension : extensionClassNames) { |
|
|
|
for (String extension : extensionClassNames) { |
|
|
|
System.out.println(" " + extension); |
|
|
|
log.info(" {}", extension); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// print the extensions instances for Greeting extension point for each started plugin
|
|
|
|
// print the extensions instances for Greeting extension point for each started plugin
|
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
String pluginId = plugin.getDescriptor().getPluginId(); |
|
|
|
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); |
|
|
|
List<Greeting> extensions = pluginManager.getExtensions(Greeting.class, pluginId); |
|
|
|
for (Object extension : extensions) { |
|
|
|
for (Object extension : extensions) { |
|
|
|
System.out.println(" " + extension); |
|
|
|
log.info(" {}", extension); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// print extensions instances from classpath (non plugin)
|
|
|
|
// 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); |
|
|
|
List extensions = pluginManager.getExtensions((String) null); |
|
|
|
for (Object extension : extensions) { |
|
|
|
for (Object extension : extensions) { |
|
|
|
System.out.println(" " + extension); |
|
|
|
log.info(" {}", extension); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// print extensions instances for each started plugin
|
|
|
|
// print extensions instances for each started plugin
|
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
for (PluginWrapper plugin : startedPlugins) { |
|
|
|
String pluginId = plugin.getDescriptor().getPluginId(); |
|
|
|
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); |
|
|
|
extensions = pluginManager.getExtensions(pluginId); |
|
|
|
for (Object extension : extensions) { |
|
|
|
for (Object extension : extensions) { |
|
|
|
System.out.println(" " + extension); |
|
|
|
log.info(" {}", extension); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -131,9 +126,22 @@ public class Boot { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void printLogo() { |
|
|
|
private static void printLogo() { |
|
|
|
System.out.println(StringUtils.repeat("#", 40)); |
|
|
|
log.info(StringUtils.repeat("#", 40)); |
|
|
|
System.out.println(StringUtils.center("PF4J-DEMO", 40)); |
|
|
|
log.info(StringUtils.center("PF4J-DEMO", 40)); |
|
|
|
System.out.println(StringUtils.repeat("#", 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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|