Added suport for multiple equal plugin with same interfaces
This commit is contained in:
parent
ddaac6163a
commit
1455ec57f9
3 changed files with 80 additions and 41 deletions
|
|
@ -45,7 +45,7 @@ public class StringUtil {
|
|||
for(; value > 1024 ;total--) {
|
||||
value /= 1024;
|
||||
}
|
||||
|
||||
|
||||
value = (double)( (int)(value*10) )/10;
|
||||
return value+" "+sizes[total];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,7 @@ import javax.xml.crypto.Data;
|
|||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -47,12 +45,12 @@ public class PluginData {
|
|||
|
||||
private double pluginVersion;
|
||||
private String pluginName;
|
||||
private HashMap<Class, Class> classMap;
|
||||
private HashMap<Class, List<Class>> classMap;
|
||||
private HashMap<Class, Object> objectMap;
|
||||
|
||||
|
||||
protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException {
|
||||
classMap = new HashMap<Class, Class>();
|
||||
classMap = new HashMap<Class, List<Class>>();
|
||||
objectMap = new HashMap<Class, Object>();
|
||||
|
||||
pluginVersion = data.getDouble("version");
|
||||
|
|
@ -74,22 +72,25 @@ public class PluginData {
|
|||
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);
|
||||
}
|
||||
|
||||
Class intfClass = getClassByName(pluginIntf);
|
||||
Class pluginClass = getClassByName(className);
|
||||
log.finer("Plugin interface: " +
|
||||
(intfClass==null ? "(Not Available) " : "") + pluginIntf + " --> " +
|
||||
(pluginClass==null ? "(Not Available) " : "") + className);
|
||||
if(intfClass == null || pluginClass == null)
|
||||
continue;
|
||||
|
||||
if(!classMap.containsKey(intfClass))
|
||||
classMap.put(intfClass, new ArrayList<Class>());
|
||||
classMap.get(intfClass).add(pluginClass);
|
||||
}
|
||||
}
|
||||
private static Class getClassByName(String name) {
|
||||
try {
|
||||
return Class.forName(name);
|
||||
}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;
|
||||
}
|
||||
|
|
@ -102,16 +103,20 @@ public class PluginData {
|
|||
return pluginName;
|
||||
}
|
||||
|
||||
public <T> T getObject(Class<T> intf) {
|
||||
if(classMap.containsKey(intf)) {
|
||||
try {
|
||||
Class subClass = classMap.get(intf);
|
||||
if (!objectMap.containsKey(subClass))
|
||||
objectMap.put(subClass, subClass.newInstance());
|
||||
return (T) objectMap.get(subClass);
|
||||
} catch (Exception e) {
|
||||
log.log(Level.WARNING, null, e);
|
||||
}
|
||||
|
||||
public <T> Iterator<T> getIterator(Class<T> intf){
|
||||
if(!classMap.containsKey(intf))
|
||||
return Collections.emptyIterator();
|
||||
return new PluginObjectIterator<T>(classMap.get(intf).iterator());
|
||||
}
|
||||
|
||||
private <T> T getObject(Class<T> objClass) {
|
||||
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;
|
||||
}
|
||||
|
|
@ -124,4 +129,41 @@ public class PluginData {
|
|||
public String toString(){
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,15 +23,11 @@
|
|||
package zutil.plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.sun.xml.internal.stream.util.ReadOnlyIterator;
|
||||
import zutil.io.IOUtil;
|
||||
import zutil.io.file.FileSearch;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.json.JSONParser;
|
||||
|
|
@ -117,24 +113,27 @@ public class PluginManager<T> implements Iterable<PluginData>{
|
|||
|
||||
public class PluginInterfaceIterator<T> implements Iterator<T> {
|
||||
private Class<T> intf;
|
||||
private Iterator<PluginData> it;
|
||||
private PluginData next;
|
||||
private Iterator<PluginData> pluginIt;
|
||||
private Iterator<T> objectIt;
|
||||
|
||||
PluginInterfaceIterator(Iterator<PluginData> it, Class<T> intf){
|
||||
this.intf = intf;
|
||||
this.it = it;
|
||||
this.pluginIt = it;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if(next != null)
|
||||
if(pluginIt == null)
|
||||
return false;
|
||||
if(objectIt != null && objectIt.hasNext())
|
||||
return true;
|
||||
while(it.hasNext()) {
|
||||
next = it.next();
|
||||
if(next.contains(intf))
|
||||
|
||||
while(pluginIt.hasNext()) {
|
||||
objectIt = pluginIt.next().getIterator(intf);
|
||||
if(objectIt.hasNext())
|
||||
return true;
|
||||
}
|
||||
next = null;
|
||||
objectIt = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -142,9 +141,7 @@ public class PluginManager<T> implements Iterable<PluginData>{
|
|||
public T next() {
|
||||
if(!hasNext())
|
||||
throw new NoSuchElementException();
|
||||
T tmp = next.getObject(intf);
|
||||
next = null;
|
||||
return tmp;
|
||||
return objectIt.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue