Added suport for multiple equal plugin with same interfaces

This commit is contained in:
Ziver Koc 2015-04-19 21:06:01 +00:00
parent ddaac6163a
commit 1455ec57f9
3 changed files with 80 additions and 41 deletions

View file

@ -45,7 +45,7 @@ public class StringUtil {
for(; value > 1024 ;total--) { for(; value > 1024 ;total--) {
value /= 1024; value /= 1024;
} }
value = (double)( (int)(value*10) )/10; value = (double)( (int)(value*10) )/10;
return value+" "+sizes[total]; return value+" "+sizes[total];
} }

View file

@ -30,9 +30,7 @@ import javax.xml.crypto.Data;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -47,12 +45,12 @@ public class PluginData {
private double pluginVersion; private double pluginVersion;
private String pluginName; private String pluginName;
private HashMap<Class, Class> classMap; private HashMap<Class, List<Class>> classMap;
private HashMap<Class, Object> objectMap; private HashMap<Class, Object> objectMap;
protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException { protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException {
classMap = new HashMap<Class, Class>(); classMap = new HashMap<Class, List<Class>>();
objectMap = new HashMap<Class, Object>(); objectMap = new HashMap<Class, Object>();
pluginVersion = data.getDouble("version"); pluginVersion = data.getDouble("version");
@ -74,22 +72,25 @@ public class PluginData {
while (intf_it.hasNext()) { while (intf_it.hasNext()) {
String pluginIntf = intf_it.next(); String pluginIntf = intf_it.next();
String className = node.get(pluginIntf).getString(); String className = node.get(pluginIntf).getString();
try {
Class.forName(className); // Check if class is available, will throw exception if class is not found Class intfClass = getClassByName(pluginIntf);
log.finer("Plugin interface: " + pluginIntf + " --> " + className); Class pluginClass = getClassByName(className);
classMap.put( log.finer("Plugin interface: " +
getClassByName(pluginIntf), (intfClass==null ? "(Not Available) " : "") + pluginIntf + " --> " +
getClassByName(className)); (pluginClass==null ? "(Not Available) " : "") + className);
}catch (Exception e){ if(intfClass == null || pluginClass == null)
log.finer("Plugin interface: " + pluginIntf + " --> (Not Available) " + className); continue;
}
if(!classMap.containsKey(intfClass))
classMap.put(intfClass, new ArrayList<Class>());
classMap.get(intfClass).add(pluginClass);
} }
} }
private static Class getClassByName(String name) { private static Class getClassByName(String name) {
try { try {
return Class.forName(name); return Class.forName(name);
}catch (Exception e){ }catch (Exception e){
log.log(Level.WARNING, null, e); //log.log(Level.WARNING, null, e); // No need to log, we are handling it
} }
return null; return null;
} }
@ -102,16 +103,20 @@ public class PluginData {
return pluginName; return pluginName;
} }
public <T> T getObject(Class<T> intf) {
if(classMap.containsKey(intf)) { public <T> Iterator<T> getIterator(Class<T> intf){
try { if(!classMap.containsKey(intf))
Class subClass = classMap.get(intf); return Collections.emptyIterator();
if (!objectMap.containsKey(subClass)) return new PluginObjectIterator<T>(classMap.get(intf).iterator());
objectMap.put(subClass, subClass.newInstance()); }
return (T) objectMap.get(subClass);
} catch (Exception e) { private <T> T getObject(Class<T> objClass) {
log.log(Level.WARNING, null, e); try {
} if (!objectMap.containsKey(objClass))
objectMap.put(objClass, objClass.newInstance());
return (T) objectMap.get(objClass);
} catch (Exception e) {
log.log(Level.WARNING, null, e);
} }
return null; return null;
} }
@ -124,4 +129,41 @@ public class PluginData {
public String toString(){ public String toString(){
return getName()+"(ver: "+getVersion()+")"; return getName()+"(ver: "+getVersion()+")";
} }
private class PluginObjectIterator<T> implements Iterator<T>{
private Iterator<Class> classIt;
private T currentObj;
public PluginObjectIterator(Iterator<Class> it) {
classIt = it;
}
@Override
public boolean hasNext() {
if(currentObj != null)
return true;
while (classIt.hasNext()){
currentObj = (T)getObject(classIt.next());
if(currentObj != null)
return true;
}
return false;
}
@Override
public T next() {
if(!hasNext())
throw new NoSuchElementException();
T tmp = currentObj;
currentObj = null;
return tmp;
}
@Override
public void remove() {
throw new RuntimeException("Iterator is ReadOnly");
}
}
} }

View file

@ -23,15 +23,11 @@
package zutil.plugin; package zutil.plugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.*; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.sun.xml.internal.stream.util.ReadOnlyIterator;
import zutil.io.IOUtil; import zutil.io.IOUtil;
import zutil.io.file.FileSearch; import zutil.io.file.FileSearch;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import zutil.parser.json.JSONParser; import zutil.parser.json.JSONParser;
@ -117,24 +113,27 @@ public class PluginManager<T> implements Iterable<PluginData>{
public class PluginInterfaceIterator<T> implements Iterator<T> { public class PluginInterfaceIterator<T> implements Iterator<T> {
private Class<T> intf; private Class<T> intf;
private Iterator<PluginData> it; private Iterator<PluginData> pluginIt;
private PluginData next; private Iterator<T> objectIt;
PluginInterfaceIterator(Iterator<PluginData> it, Class<T> intf){ PluginInterfaceIterator(Iterator<PluginData> it, Class<T> intf){
this.intf = intf; this.intf = intf;
this.it = it; this.pluginIt = it;
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
if(next != null) if(pluginIt == null)
return false;
if(objectIt != null && objectIt.hasNext())
return true; return true;
while(it.hasNext()) {
next = it.next(); while(pluginIt.hasNext()) {
if(next.contains(intf)) objectIt = pluginIt.next().getIterator(intf);
if(objectIt.hasNext())
return true; return true;
} }
next = null; objectIt = null;
return false; return false;
} }
@ -142,9 +141,7 @@ public class PluginManager<T> implements Iterable<PluginData>{
public T next() { public T next() {
if(!hasNext()) if(!hasNext())
throw new NoSuchElementException(); throw new NoSuchElementException();
T tmp = next.getObject(intf); return objectIt.next();
next = null;
return tmp;
} }
@Override @Override