Skip to content

Instantly share code, notes, and snippets.

@thieux
Last active November 21, 2019 16:07
Show Gist options
  • Save thieux/910ab9b1122998f424dc98e8b8525bce to your computer and use it in GitHub Desktop.
Save thieux/910ab9b1122998f424dc98e8b8525bce to your computer and use it in GitHub Desktop.
Démontration d'un système de plugin en Java avec deux styles : interface et annotation
package com.mathieupauly.javacoursesamples.plugin;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SocleDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
List<Plugin> plugins = loadPlugins(Arrays.asList(args));
init(plugins);
list(plugins);
shutdown(plugins);
}
private static List<Plugin> loadPlugins(List<String> pluginNames) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
List<Plugin> plugins = new ArrayList<>();
for (String pluginName : pluginNames) {
plugins.add(loadPlugin(pluginName));
}
return plugins;
}
private static Plugin loadPlugin(String pluginName) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class<Plugin> pluginImpl = (Class<Plugin>) Class.forName(pluginName);
Constructor<Plugin> constructor = pluginImpl.getConstructor();
return constructor.newInstance();
}
private static void shutdown(List<Plugin> plugins) throws InvocationTargetException, IllegalAccessException {
for (Plugin plugin : plugins) {
Method[] declaredMethods = plugin.getClass().getDeclaredMethods();
Method stopMethod = Arrays.stream(declaredMethods)
.filter(method -> method.getAnnotation(Stop.class) != null)
.findFirst()
.orElseThrow(() -> new RuntimeException("Plugin should define a stop method"));
stopMethod.invoke(plugin);
}
}
private static void init(List<Plugin> plugins) {
for (Plugin plugin : plugins) {
plugin.initialize();
}
}
private static void list(List<Plugin> plugins) {
for (Plugin plugin : plugins) {
System.out.println(plugin.version());
}
}
}
interface Plugin {
Object version();
void initialize();
}
class PluginImpl implements Plugin {
public PluginImpl() {
}
@Override
public Object version() {
return "1.0";
}
@Override
public void initialize() {
System.out.println("Plugin impl init...");
}
@Stop
public void foo() {
System.out.println("Plugin impl stop...");
}
}
class PluginImpl2 implements Plugin {
public PluginImpl2() {
}
@Override
public Object version() {
return "2.0";
}
@Override
public void initialize() {
System.out.println("Plugin impl2 init...");
}
@Stop
public void bar() {
System.out.println("Plugin impl2 stop...");
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface Stop {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment