This commit is contained in:
parent
e822a4b35c
commit
91cadbb301
20 changed files with 342 additions and 130 deletions
|
|
@ -22,9 +22,18 @@
|
|||
|
||||
package zutil.plugin;
|
||||
|
||||
import zutil.ClassUtil;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.DataNode;
|
||||
|
||||
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.logging.Logger;
|
||||
|
||||
/**
|
||||
* This class contains information about a plugin
|
||||
|
|
@ -32,18 +41,36 @@ import java.net.URLClassLoader;
|
|||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class PluginData<T> {
|
||||
public class PluginData {
|
||||
private static Logger log = LogUtil.getLogger();
|
||||
|
||||
private double pluginVersion;
|
||||
private String pluginName;
|
||||
private String pluginClass;
|
||||
private String pluginName;
|
||||
private HashMap<Class, Class> classMap;
|
||||
private HashMap<Class, Object> objectMap;
|
||||
|
||||
|
||||
private T obj;
|
||||
|
||||
|
||||
protected PluginData(String intf, DataNode data){
|
||||
protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException {
|
||||
classMap = new HashMap<Class, Class>();
|
||||
objectMap = new HashMap<Class, Object>();
|
||||
|
||||
pluginVersion = data.getDouble("version");
|
||||
pluginName = data.getString("name");
|
||||
pluginClass = data.get("interfaces").getString(intf);
|
||||
pluginName = data.getString("name");
|
||||
log.fine("Found plugin: "+pluginName);
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
private static Class getClassByName(String name) throws ClassNotFoundException, MalformedURLException {
|
||||
return Class.forName(name);
|
||||
}
|
||||
|
||||
public double getVersion(){
|
||||
|
|
@ -52,13 +79,23 @@ public class PluginData<T> {
|
|||
public String getName(){
|
||||
return pluginName;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T getObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
|
||||
//if(obj == null)
|
||||
// new URLClassLoader(pluginClass);
|
||||
// //obj = (T) Class.forName(pluginClass).newInstance();
|
||||
return obj;
|
||||
|
||||
public <T> T getObject(Class<T> intf) {
|
||||
if(classMap.containsKey(intf)) {
|
||||
try {
|
||||
Class subClass = classMap.get(intf);
|
||||
if (objectMap.containsKey(subClass))
|
||||
objectMap.put(intf, subClass.newInstance());
|
||||
return (T) objectMap.get(subClass);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean contains(Class<?> intf){
|
||||
return classMap.containsKey(intf);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue