added first version of project 81 plugin

This commit is contained in:
2014-01-06 23:55:19 +01:00
parent d9156b87e8
commit 814cff94c8
4 changed files with 160 additions and 50 deletions

View File

@@ -9,22 +9,23 @@ import java.util.Map;
*/
public interface Plugin {
/**
* @return name
*/
String getName();
/**
* @return name
*/
String getName();
/**
* @return version
*/
String getVersion();
/**
* @return version
*/
String getVersion();
/**
* Run the main method of the plugin. All the logic is done in this method.
* Every run method will get a context map, with all the services inside,
* to use them.
* @param context services and data
*/
void run(Map context);
/**
* Run the main method of the plugin. All the logic is done in this method.
* Every run method will get a context map, with all the services inside,
* to use them.
*
* @param context services and data
*/
void run(Map context);
}

View File

@@ -9,10 +9,9 @@ import java.util.List;
*/
public interface PluginService {
List<Plugin> getAllPlugins();
void executePlugin(Plugin plugin);
List<Plugin> getAllPlugins();
void executePlugin(Plugin plugin);
}

View File

@@ -12,50 +12,50 @@ import java.util.List;
import java.util.Map;
/**
* TODO: class description
* This service find, load and executes plugins based on the plugin interface.
*
* @author abi
*/
public class PluginServiceImpl implements PluginService {
private final GroovyClassLoader gcl = new GroovyClassLoader();
private final ServiceManager serviceManager = ServiceManager.getInstance();
private final GroovyClassLoader gcl = new GroovyClassLoader();
private final ServiceManager serviceManager = ServiceManager.getInstance();
@Override
public List<Plugin> getAllPlugins() {
@Override
public List<Plugin> getAllPlugins() {
List<Plugin> plugins = new ArrayList<>();
List<Plugin> plugins = new ArrayList<>();
try {
File file = new File("./plugins");
if (!file.exists()) {
throw new IllegalArgumentException("plugins folder does not exit");
}
try {
File file = new File("./plugins");
if (!file.exists()) {
throw new IllegalArgumentException("plugins folder does not exist");
}
File[] pluginFiles = file.listFiles((dir, name) -> name.endsWith("Plugin.groovy"));
for (File pluginFile: pluginFiles) {
Class clazz = gcl.parseClass(pluginFile);
for (Class interf: clazz.getInterfaces()) {
if (interf.equals(Plugin.class)) {
plugins.add((Plugin)clazz.newInstance());
break;
}
File[] pluginFiles = file.listFiles((dir, name) -> name.endsWith("Plugin.groovy"));
for (File pluginFile : pluginFiles) {
Class clazz = gcl.parseClass(pluginFile);
for (Class interf : clazz.getInterfaces()) {
if (interf.equals(Plugin.class)) {
plugins.add((Plugin) clazz.newInstance());
break;
}
}
}
} catch (IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
} catch (IOException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return plugins;
}
return plugins;
}
@Override
public void executePlugin(final Plugin plugin) {
Map<String, Object> context = new HashMap<>();
context.put("sessionContext", SessionContext.getInstance());
context.put("cacheService", serviceManager.getCacheService());
plugin.run(context);
}
@Override
public void executePlugin(final Plugin plugin) {
Map<String, Object> context = new HashMap<>();
context.put("sessionContext", SessionContext.getInstance());
context.put("cacheService", serviceManager.getCacheService());
plugin.run(context);
}
}