2015-05-27 13:13:19 +00:00
|
|
|
/*
|
2015-10-01 15:23:40 +00:00
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
2014-04-18 23:18:16 +00:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
2015-05-27 13:13:19 +00:00
|
|
|
*/
|
2014-04-18 23:18:16 +00:00
|
|
|
|
|
|
|
|
package zutil.plugin;
|
|
|
|
|
|
2015-03-23 21:05:51 +00:00
|
|
|
import zutil.log.LogUtil;
|
2014-04-18 23:18:16 +00:00
|
|
|
import zutil.parser.DataNode;
|
|
|
|
|
|
2015-03-23 21:05:51 +00:00
|
|
|
import java.net.MalformedURLException;
|
2015-04-19 21:06:01 +00:00
|
|
|
import java.util.*;
|
2015-04-09 21:14:25 +00:00
|
|
|
import java.util.logging.Level;
|
2015-03-23 21:05:51 +00:00
|
|
|
import java.util.logging.Logger;
|
2014-11-06 20:21:29 +00:00
|
|
|
|
2014-04-18 23:18:16 +00:00
|
|
|
/**
|
|
|
|
|
* This class contains information about a plugin
|
|
|
|
|
* and implementation instances of the plugin interfaces
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2015-03-23 21:05:51 +00:00
|
|
|
public class PluginData {
|
|
|
|
|
private static Logger log = LogUtil.getLogger();
|
|
|
|
|
|
2014-04-18 23:18:16 +00:00
|
|
|
private double pluginVersion;
|
2015-03-23 21:05:51 +00:00
|
|
|
private String pluginName;
|
2015-12-23 00:33:26 +01:00
|
|
|
private HashMap<Class<?>, List<Class<?>>> classMap;
|
2015-03-23 21:05:51 +00:00
|
|
|
private HashMap<Class, Object> objectMap;
|
|
|
|
|
|
2014-04-18 23:18:16 +00:00
|
|
|
|
2015-03-23 21:05:51 +00:00
|
|
|
protected PluginData(DataNode data) throws ClassNotFoundException, MalformedURLException {
|
2015-12-23 00:33:26 +01:00
|
|
|
classMap = new HashMap<>();
|
2015-03-23 21:05:51 +00:00
|
|
|
objectMap = new HashMap<Class, Object>();
|
|
|
|
|
|
2014-04-18 23:18:16 +00:00
|
|
|
pluginVersion = data.getDouble("version");
|
2015-03-23 21:05:51 +00:00
|
|
|
pluginName = data.getString("name");
|
2015-04-09 21:14:25 +00:00
|
|
|
log.fine("Plugin: " + this);
|
2015-03-23 21:05:51 +00:00
|
|
|
|
|
|
|
|
DataNode node = data.get("interfaces");
|
2015-04-09 21:14:25 +00:00
|
|
|
if(node.isMap())
|
|
|
|
|
addInterfaces(node);
|
|
|
|
|
else if(node.isList()) {
|
|
|
|
|
Iterator<DataNode> intfs_it = node.iterator();
|
|
|
|
|
while (intfs_it.hasNext()) {
|
|
|
|
|
addInterfaces(intfs_it.next());
|
|
|
|
|
}
|
2015-03-23 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-09 21:14:25 +00:00
|
|
|
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();
|
2015-04-19 21:06:01 +00:00
|
|
|
|
|
|
|
|
Class intfClass = getClassByName(pluginIntf);
|
|
|
|
|
Class pluginClass = getClassByName(className);
|
2016-01-04 22:50:52 +01:00
|
|
|
if (intfClass == null || pluginClass == null)
|
|
|
|
|
log.warning("Plugin interface: " +
|
|
|
|
|
(intfClass==null ? "(Not Available) " : "") + pluginIntf + " --> " +
|
|
|
|
|
(pluginClass==null ? "(Not Available) " : "") + className);
|
|
|
|
|
else
|
|
|
|
|
log.finer("Plugin interface: "+ pluginIntf +" --> "+ className);
|
|
|
|
|
|
|
|
|
|
if (intfClass == null || pluginClass == null)
|
2015-04-19 21:06:01 +00:00
|
|
|
continue;
|
|
|
|
|
|
2016-01-04 22:50:52 +01:00
|
|
|
if (!classMap.containsKey(intfClass))
|
2015-12-23 00:33:26 +01:00
|
|
|
classMap.put(intfClass, new ArrayList<Class<?>>());
|
2015-04-19 21:06:01 +00:00
|
|
|
classMap.get(intfClass).add(pluginClass);
|
2015-04-09 21:14:25 +00:00
|
|
|
}
|
2014-04-18 23:18:16 +00:00
|
|
|
}
|
2015-04-09 21:14:25 +00:00
|
|
|
private static Class getClassByName(String name) {
|
|
|
|
|
try {
|
|
|
|
|
return Class.forName(name);
|
|
|
|
|
}catch (Exception e){
|
2015-04-19 21:06:01 +00:00
|
|
|
//log.log(Level.WARNING, null, e); // No need to log, we are handling it
|
2015-04-09 21:14:25 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-18 23:18:16 +00:00
|
|
|
public double getVersion(){
|
|
|
|
|
return pluginVersion;
|
|
|
|
|
}
|
|
|
|
|
public String getName(){
|
|
|
|
|
return pluginName;
|
|
|
|
|
}
|
2015-03-23 21:05:51 +00:00
|
|
|
|
2015-04-19 21:06:01 +00:00
|
|
|
|
2015-12-23 00:33:26 +01:00
|
|
|
public <T> Iterator<T> getObjectIterator(Class<T> intf){
|
2015-04-19 21:06:01 +00:00
|
|
|
if(!classMap.containsKey(intf))
|
|
|
|
|
return Collections.emptyIterator();
|
|
|
|
|
return new PluginObjectIterator<T>(classMap.get(intf).iterator());
|
|
|
|
|
}
|
2015-12-23 00:33:26 +01:00
|
|
|
public Iterator<Class<?>> getClassIterator(Class<?> intf){
|
|
|
|
|
if(!classMap.containsKey(intf))
|
|
|
|
|
return Collections.emptyIterator();
|
|
|
|
|
return classMap.get(intf).iterator();
|
|
|
|
|
}
|
2015-04-19 21:06:01 +00:00
|
|
|
|
|
|
|
|
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);
|
2015-03-23 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
2014-04-18 23:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-23 21:05:51 +00:00
|
|
|
|
|
|
|
|
public boolean contains(Class<?> intf){
|
|
|
|
|
return classMap.containsKey(intf);
|
|
|
|
|
}
|
2015-03-30 01:25:36 +00:00
|
|
|
|
|
|
|
|
public String toString(){
|
|
|
|
|
return getName()+"(ver: "+getVersion()+")";
|
|
|
|
|
}
|
2015-04-19 21:06:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class PluginObjectIterator<T> implements Iterator<T>{
|
2015-12-23 00:33:26 +01:00
|
|
|
private Iterator<Class<?>> classIt;
|
2015-04-19 21:06:01 +00:00
|
|
|
private T currentObj;
|
|
|
|
|
|
2015-12-23 00:33:26 +01:00
|
|
|
public PluginObjectIterator(Iterator<Class<?>> it) {
|
2015-04-19 21:06:01 +00:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-18 23:18:16 +00:00
|
|
|
}
|