Bug fixes
This commit is contained in:
parent
c22c866000
commit
a199165756
10 changed files with 171 additions and 55 deletions
|
|
@ -33,6 +33,7 @@ import java.net.URLClassLoader;
|
|||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -51,28 +52,49 @@ public class PluginData {
|
|||
|
||||
|
||||
protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException {
|
||||
classMap = new HashMap<Class, Class>();
|
||||
classMap = new HashMap<Class, Class>();
|
||||
objectMap = new HashMap<Class, Object>();
|
||||
|
||||
pluginVersion = data.getDouble("version");
|
||||
pluginName = data.getString("name");
|
||||
log.fine("Plugin: "+this);
|
||||
log.fine("Plugin: " + this);
|
||||
|
||||
DataNode node = data.get("interfaces");
|
||||
Iterator<String> intfIt = node.keyIterator();
|
||||
while (intfIt.hasNext()) {
|
||||
String intf = intfIt.next();
|
||||
log.finer("Plugin interface: "+ intf+" --> "+node.get(intf).getString());
|
||||
classMap.put(
|
||||
getClassByName(intf),
|
||||
getClassByName(node.get(intf).getString()));
|
||||
if(node.isMap())
|
||||
addInterfaces(node);
|
||||
else if(node.isList()) {
|
||||
Iterator<DataNode> intfs_it = node.iterator();
|
||||
while (intfs_it.hasNext()) {
|
||||
addInterfaces(intfs_it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Class getClassByName(String name) throws ClassNotFoundException, MalformedURLException {
|
||||
return Class.forName(name);
|
||||
private void addInterfaces(DataNode node){
|
||||
Iterator<String> intf_it = node.keyIterator();
|
||||
while (intf_it.hasNext()) {
|
||||
String pluginIntf = intf_it.next();
|
||||
String className = node.get(pluginIntf).getString();
|
||||
try {
|
||||
Class.forName(className); // Check if class is available, will throw exception if class is not found
|
||||
log.finer("Plugin interface: " + pluginIntf + " --> " + className);
|
||||
classMap.put(
|
||||
getClassByName(pluginIntf),
|
||||
getClassByName(className));
|
||||
}catch (Exception e){
|
||||
log.finer("Plugin interface: " + pluginIntf + " --> (Not Available) " + className);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Class getClassByName(String name) {
|
||||
try {
|
||||
return Class.forName(name);
|
||||
}catch (Exception e){
|
||||
log.log(Level.WARNING, null, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public double getVersion(){
|
||||
return pluginVersion;
|
||||
}
|
||||
|
|
@ -84,11 +106,11 @@ public class PluginData {
|
|||
if(classMap.containsKey(intf)) {
|
||||
try {
|
||||
Class subClass = classMap.get(intf);
|
||||
if (objectMap.containsKey(subClass))
|
||||
objectMap.put(intf, subClass.newInstance());
|
||||
if (!objectMap.containsKey(subClass))
|
||||
objectMap.put(subClass, subClass.newInstance());
|
||||
return (T) objectMap.get(subClass);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.log(Level.WARNING, null, e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -25,9 +25,7 @@ package zutil.plugin;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.sun.xml.internal.stream.util.ReadOnlyIterator;
|
||||
|
|
@ -97,11 +95,26 @@ public class PluginManager<T> implements Iterable<PluginData>{
|
|||
public Iterator<PluginData> iterator() {
|
||||
return plugins.values().iterator();
|
||||
}
|
||||
|
||||
public <T> Iterator<T> iterator(Class<T> intf) {
|
||||
return new PluginInterfaceIterator<T>(plugins.values().iterator(), intf);
|
||||
}
|
||||
|
||||
public ArrayList<PluginData> toArray() {
|
||||
ArrayList<PluginData> list = new ArrayList<PluginData>();
|
||||
Iterator<PluginData> it = iterator();
|
||||
while(it.hasNext())
|
||||
list.add(it.next());
|
||||
return list;
|
||||
}
|
||||
public <T> ArrayList<T> toArray(Class<T> intf) {
|
||||
ArrayList<T> list = new ArrayList<T>();
|
||||
Iterator<T> it = iterator(intf);
|
||||
while(it.hasNext())
|
||||
list.add(it.next());
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public class PluginInterfaceIterator<T> implements Iterator<T> {
|
||||
private Class<T> intf;
|
||||
private Iterator<PluginData> it;
|
||||
|
|
@ -129,7 +142,9 @@ public class PluginManager<T> implements Iterable<PluginData>{
|
|||
public T next() {
|
||||
if(!hasNext())
|
||||
throw new NoSuchElementException();
|
||||
return next.getObject(intf);
|
||||
T tmp = next.getObject(intf);
|
||||
next = null;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
version = 1.0,
|
||||
name = "Nice name of Plugin"
|
||||
interfaces = {
|
||||
"plugin.interface.class" = "plugin.implementation.class",
|
||||
"wa.server.plugin.WAFrontend" = "wa.server.plugin.apache.ApacheFrontend",
|
||||
}
|
||||
interfaces = [
|
||||
{"plugin.interface.class" = "plugin.implementation.class"},
|
||||
{"wa.server.plugin.WAFrontend" = "wa.server.plugin.apache.ApacheFrontend"},
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue