From 2ad15a19742e19a4d49449e3c7ae725b48aec68d Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Thu, 16 May 2019 21:30:27 +0300 Subject: [PATCH] Resolve #306 --- maven-archetypes/quickstart/pom.xml | 68 ++++++++++++ .../META-INF/maven/archetype-metadata.xml | 86 +++++++++++++++ .../resources/archetype-resources/app/pom.xml | 86 +++++++++++++++ .../app/src/main/assembly/assembly.xml | 30 ++++++ .../app/src/main/java/Boot.java | 102 ++++++++++++++++++ .../app/src/main/java/Greeting.java | 9 ++ .../app/src/main/java/WhazzupGreeting.java | 13 +++ .../app/src/main/resources/log4j.properties | 20 ++++ .../archetype-resources/plugins/disabled.txt | 6 ++ .../archetype-resources/plugins/enabled.txt | 6 ++ .../plugins/hello/plugin.properties | 5 + .../archetype-resources/plugins/hello/pom.xml | 24 +++++ .../src/main/java/hello/HelloPlugin.java | 37 +++++++ .../archetype-resources/plugins/pom.xml | 86 +++++++++++++++ .../plugins/welcome/plugin.properties | 5 + .../plugins/welcome/pom.xml | 32 ++++++ .../src/main/java/welcome/WelcomePlugin.java | 41 +++++++ .../resources/archetype-resources/pom.xml | 48 +++++++++ .../main/resources/archetype-resources/run.sh | 23 ++++ pom.xml | 1 + 20 files changed, 728 insertions(+) create mode 100755 maven-archetypes/quickstart/pom.xml create mode 100644 maven-archetypes/quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml create mode 100644 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/pom.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/assembly/assembly.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Boot.java create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Greeting.java create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/WhazzupGreeting.java create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/resources/log4j.properties create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/disabled.txt create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/enabled.txt create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/plugin.properties create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/pom.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/src/main/java/hello/HelloPlugin.java create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/pom.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/plugin.properties create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/pom.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/src/main/java/welcome/WelcomePlugin.java create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/pom.xml create mode 100755 maven-archetypes/quickstart/src/main/resources/archetype-resources/run.sh diff --git a/maven-archetypes/quickstart/pom.xml b/maven-archetypes/quickstart/pom.xml new file mode 100755 index 0000000..783bed1 --- /dev/null +++ b/maven-archetypes/quickstart/pom.xml @@ -0,0 +1,68 @@ + + + + + org.pf4j + pf4j-parent + 3.0.0-SNAPSHOT + ../../pom.xml + + + 4.0.0 + pf4j-quickstart + maven-archetype + Quickstart Archetype + + + + + + true + src/main/resources + + archetype-resources/**/pom.xml + + + + + false + src/main/resources + + archetype-resources/**/pom.xml + + + + + + + org.apache.maven.archetype + archetype-packaging + 2.3 + + + + + + + org.apache.maven.plugins + maven-archetype-plugin + 2.3 + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + {{*}} + + false + + + + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml b/maven-archetypes/quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 0000000..a6f40bd --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,86 @@ + + + + + + + + run.sh + + + + + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.properties + + + + src/main/assembly + + **/*.xml + + + + + + + + + + + enabled.txt + disabled.txt + + + + + + + + + src/main/java + + **/*.java + + + + + + plugin.properties + + + + + + + + + src/main/java + + **/*.java + + + + + + plugin.properties + + + + + + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/pom.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/pom.xml new file mode 100644 index 0000000..986c0a3 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/pom.xml @@ -0,0 +1,86 @@ + + + + + ${groupId} + ${rootArtifactId} + ${version} + + + 4.0.0 + ${artifactId} + ${version} + jar + App + + + ${package}.Boot + + + + + + maven-assembly-plugin + 2.3 + + + src/main/assembly/assembly.xml + + false + + + + make-assembly + package + + attached + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + + + true + lib/ + ${main.class} + + + + + + + + + + org.pf4j + pf4j + ${pf4j.version} + + + + + log4j + log4j + 1.2.16 + + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + + commons-lang + commons-lang + 2.4 + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/assembly/assembly.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/assembly/assembly.xml new file mode 100755 index 0000000..b88a245 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/assembly/assembly.xml @@ -0,0 +1,30 @@ + + app + + dir + zip + + false + + + false + lib + + *:jar:* + + + + + + ${project.build.directory} + + + *.jar + + + *-javadoc.jar + *-sources.jar + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Boot.java b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Boot.java new file mode 100755 index 0000000..2853b08 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Boot.java @@ -0,0 +1,102 @@ +package ${package}; + +import org.apache.commons.lang.StringUtils; +import org.pf4j.DefaultPluginManager; +import org.pf4j.ExtensionFinder; +import org.pf4j.PluginManager; +import org.pf4j.PluginWrapper; + +import java.util.List; +import java.util.Set; + +/** + * A boot class that start the application. + */ +public class Boot { + + public static void main(String[] args) { + // create the plugin manager + PluginManager pluginManager = new DefaultPluginManager(); + + // load the plugins + pluginManager.loadPlugins(); + + // enable a disabled plugin +// pluginManager.enablePlugin("welcome-plugin"); + + // start (active/resolved) the plugins + pluginManager.startPlugins(); + + // 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())); + for (Greeting greeting : greetings) { + System.out.println(">>> " + greeting.getGreeting()); + } + + // print extensions from classpath (non plugin) + System.out.println("Extensions added by classpath:"); + Set extensionClassNames = pluginManager.getExtensionClassNames(null); + for (String extension : extensionClassNames) { + System.out.println(" " + extension); + } + + System.out.println("Extension classes by classpath:"); + List> greetingsClasses = pluginManager.getExtensionClasses(Greeting.class); + for (Class greeting : greetingsClasses) { + System.out.println(" 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)); + extensionClassNames = pluginManager.getExtensionClassNames(pluginId); + for (String extension : extensionClassNames) { + System.out.println(" " + extension); + } + } + + // print 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())); + List extensions = pluginManager.getExtensions(Greeting.class, pluginId); + for (Object extension : extensions) { + System.out.println(" " + extension); + } + } + + // print extensions instances from classpath (non plugin) + System.out.println("Extensions instances added by classpath:"); + List extensions = pluginManager.getExtensions((String) null); + for (Object extension : extensions) { + System.out.println(" " + 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)); + extensions = pluginManager.getExtensions(pluginId); + for (Object extension : extensions) { + System.out.println(" " + extension); + } + } + + // stop the plugins + pluginManager.stopPlugins(); + /* + Runtime.getRuntime().addShutdownHook(new Thread() { + + @Override + public void run() { + pluginManager.stopPlugins(); + } + + }); + */ + } + +} diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Greeting.java b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Greeting.java new file mode 100755 index 0000000..d64d5fe --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/Greeting.java @@ -0,0 +1,9 @@ +package ${package}; + +import org.pf4j.ExtensionPoint; + +public interface Greeting extends ExtensionPoint { + + String getGreeting(); + +} diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/WhazzupGreeting.java b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/WhazzupGreeting.java new file mode 100755 index 0000000..bb747f5 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/java/WhazzupGreeting.java @@ -0,0 +1,13 @@ +package ${package}; + +import org.pf4j.Extension; + +@Extension +public class WhazzupGreeting implements Greeting { + + @Override + public String getGreeting() { + return "Whazzup"; + } + +} diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/resources/log4j.properties b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/resources/log4j.properties new file mode 100755 index 0000000..a05d9eb --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/app/src/main/resources/log4j.properties @@ -0,0 +1,20 @@ +log4j.rootLogger=DEBUG, Console + +# +# PF4J log +# +log4j.logger.org.pf4j=DEBUG, Console +# !!! Put the bellow classes on level TRACE when you are in trouble +log4j.logger.org.pf4j.PluginClassLoader=DEBUG, Console +log4j.logger.org.pf4j.AbstractExtensionFinder=DEBUG, Console +log4j.additivity.org.pf4j=false +log4j.additivity.org.pf4j.PluginClassLoader=false +log4j.additivity.org.pf4j.AbstractExtensionFinder=false + +# +# Appenders +# +log4j.appender.Console=org.apache.log4j.ConsoleAppender +log4j.appender.Console.layout=org.apache.log4j.PatternLayout +#log4j.appender.Console.layout.conversionPattern=%-5p - %-32.32c{1} - %m\n +log4j.appender.Console.layout.ConversionPattern=%d %p %c - %m%n diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/disabled.txt b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/disabled.txt new file mode 100755 index 0000000..45f1801 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/disabled.txt @@ -0,0 +1,6 @@ +######################################## +# - load all plugins except these +# - add one plugin id on each line +# - put this file in plugins folder +######################################## +#welcome-plugin diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/enabled.txt b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/enabled.txt new file mode 100755 index 0000000..3d76b2f --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/enabled.txt @@ -0,0 +1,6 @@ +######################################## +# - load only these plugins +# - add one plugin id on each line +# - put this file in plugins folder +######################################## +#welcome-plugin diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/plugin.properties b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/plugin.properties new file mode 100755 index 0000000..9ed49b1 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/plugin.properties @@ -0,0 +1,5 @@ +plugin.id=hello-plugin +plugin.class=${package}.hello.HelloPlugin +plugin.version=${version} +plugin.provider= +plugin.dependencies= diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/pom.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/pom.xml new file mode 100755 index 0000000..10e8262 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/pom.xml @@ -0,0 +1,24 @@ + + + + + ${groupId} + ${rootArtifactId}-plugins + ${version} + + + 4.0.0 + hello-plugin + ${version} + jar + Hello Plugin + + + hello-plugin + ${package}.hello.HelloPlugin + ${version} + + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/src/main/java/hello/HelloPlugin.java b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/src/main/java/hello/HelloPlugin.java new file mode 100755 index 0000000..48604d6 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/hello/src/main/java/hello/HelloPlugin.java @@ -0,0 +1,37 @@ +package ${package}.hello; + +import org.pf4j.Extension; +import org.pf4j.Plugin; +import org.pf4j.PluginWrapper; +import ${package}.Greeting; + +/** + * A very simple plugin. + */ +public class HelloPlugin extends Plugin { + + public HelloPlugin(PluginWrapper wrapper) { + super(wrapper); + } + + @Override + public void start() { + System.out.println("HelloPlugin.start()"); + } + + @Override + public void stop() { + System.out.println("HelloPlugin.stop()"); + } + + @Extension(ordinal=1) + public static class HelloGreeting implements Greeting { + + @Override + public String getGreeting() { + return "Hello"; + } + + } + +} diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/pom.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/pom.xml new file mode 100755 index 0000000..b144ca0 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/pom.xml @@ -0,0 +1,86 @@ + + + + + ${groupId} + ${rootArtifactId} + ${version} + + + 4.0.0 + ${artifactId} + ${version} + pom + Plugins Parent + + + UTF-8 + UTF-8 + + + + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + jar-with-dependencies + + ${project.artifactId}-${project.version}-all + false + false + + + true + true + + + ${plugin.id} + ${plugin.version} + ${plugin.provider} + ${plugin.class} + ${plugin.dependencies} + + + + + + make-assembly + package + + single + + + + + + + + + + org.pf4j + pf4j + ${pf4j.version} + + provided + + + + ${groupId} + ${rootArtifactId}-app + ${version} + + provided + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/plugin.properties b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/plugin.properties new file mode 100755 index 0000000..80edaab --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/plugin.properties @@ -0,0 +1,5 @@ +plugin.id=welcome-plugin +plugin.class=${package}.welcome.WelcomePlugin +plugin.version=${version} +plugin.provider= +plugin.dependencies= diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/pom.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/pom.xml new file mode 100755 index 0000000..9101abb --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/pom.xml @@ -0,0 +1,32 @@ + + + + + ${groupId} + ${rootArtifactId}-plugins + ${version} + + + 4.0.0 + welcome-plugin + ${version} + jar + Welcom Plugin + + + welcome-plugin + ${package}.welcome.WelcomePlugin + ${version} + + + + + + + commons-lang + commons-lang + 2.6 + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/src/main/java/welcome/WelcomePlugin.java b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/src/main/java/welcome/WelcomePlugin.java new file mode 100755 index 0000000..a82d13c --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/plugins/welcome/src/main/java/welcome/WelcomePlugin.java @@ -0,0 +1,41 @@ +package ${package}.welcome; + +import org.apache.commons.lang.StringUtils; + +import org.pf4j.PluginWrapper; +import org.pf4j.RuntimeMode; +import org.pf4j.Extension; +import org.pf4j.Plugin; +import ${package}.Greeting; + +public class WelcomePlugin extends Plugin { + + public WelcomePlugin(PluginWrapper wrapper) { + super(wrapper); + } + + @Override + public void start() { + System.out.println("WelcomePlugin.start()"); + // for testing the development mode + if (RuntimeMode.DEVELOPMENT.equals(wrapper.getRuntimeMode())) { + System.out.println(StringUtils.upperCase("WelcomePlugin")); + } + } + + @Override + public void stop() { + System.out.println("WelcomePlugin.stop()"); + } + + @Extension + public static class WelcomeGreeting implements Greeting { + + @Override + public String getGreeting() { + return "Welcome"; + } + + } + +} diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/pom.xml b/maven-archetypes/quickstart/src/main/resources/archetype-resources/pom.xml new file mode 100755 index 0000000..13ed127 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + ${groupId} + ${artifactId} + ${version} + pom + PF4J Quickstart + + + + sonatype-nexus-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + UTF-8 + 8 + + {{project.version}} + 1.7.7 + + + + + + false + src/main/java + + **/*.java + + + + + src/main/resources + + + + + diff --git a/maven-archetypes/quickstart/src/main/resources/archetype-resources/run.sh b/maven-archetypes/quickstart/src/main/resources/archetype-resources/run.sh new file mode 100755 index 0000000..6214823 --- /dev/null +++ b/maven-archetypes/quickstart/src/main/resources/archetype-resources/run.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# create artifacts using Maven +mvn clean package -DskipTests + +# create "dist" directory +rm -fr dist +mkdir -p dist/plugins + +# copy plugins to "dist" directory +cp plugins/*/target/*-all.jar dist/plugins/ +cp plugins/enabled.txt dist/plugins/ +cp plugins/disabled.txt dist/plugins/ + +cd dist + +# unzip app to "dist" directory +jar xf ../app/target/*.zip + +# run app +java -jar *.jar + +cd - diff --git a/pom.xml b/pom.xml index 46a432d..add6723 100644 --- a/pom.xml +++ b/pom.xml @@ -160,6 +160,7 @@ pf4j demo + maven-archetypes/quickstart -- 2.39.5