diff --git a/src/zutil/log/CompactLogFormatter.java b/src/zutil/log/CompactLogFormatter.java index 9de9522..34c51f2 100644 --- a/src/zutil/log/CompactLogFormatter.java +++ b/src/zutil/log/CompactLogFormatter.java @@ -89,8 +89,9 @@ public class CompactLogFormatter extends Formatter{ ret.append( data ); ret.append( array[i] ); } + ret.append( '\n' ); } - ret.append( '\n' ); + if( record.getThrown() != null ){ StringOutputStream out = new StringOutputStream(); record.getThrown().printStackTrace(new PrintStream(out)); diff --git a/src/zutil/osal/HALLinuxImpl.java b/src/zutil/osal/HALLinuxImpl.java new file mode 100644 index 0000000..8ee2c88 --- /dev/null +++ b/src/zutil/osal/HALLinuxImpl.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 Ziver + * + * 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. + */ + +package zutil.osal; + +/** + * Created by Ziver on 2015-04-07. + */ +public class HALLinuxImpl implements HardwareAbstractionLayer{ + + protected HALLinuxImpl(){} +} diff --git a/src/zutil/osal/HardwareAbstractionLayer.java b/src/zutil/osal/HardwareAbstractionLayer.java new file mode 100644 index 0000000..a93ba7e --- /dev/null +++ b/src/zutil/osal/HardwareAbstractionLayer.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 Ziver + * + * 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. + */ + +package zutil.osal; + +/** + * Created by Ziver on 2015-04-07. + */ +public interface HardwareAbstractionLayer { +} diff --git a/src/zutil/osal/OSAbstractionLayer.java b/src/zutil/osal/OSAbstractionLayer.java index 91331c5..330e927 100644 --- a/src/zutil/osal/OSAbstractionLayer.java +++ b/src/zutil/osal/OSAbstractionLayer.java @@ -58,7 +58,7 @@ public abstract class OSAbstractionLayer { * @param cmd the command to run * @return first line of the command */ - protected String getFirstLineFromCommand(String cmd) { + protected static String getFirstLineFromCommand(String cmd) { String[] tmp = runCommand(cmd); if(tmp.length > 1) return tmp[0]; @@ -71,7 +71,7 @@ public abstract class OSAbstractionLayer { * @param cmd the command to run * @return a String list of the output of the command */ - public String[] runCommand(String cmd) { + public static String[] runCommand(String cmd) { ArrayList ret = new ArrayList(); try { Runtime runtime = Runtime.getRuntime(); @@ -125,4 +125,6 @@ public abstract class OSAbstractionLayer { * @return the path to the global configuration folder e.g Linux: "/etc */ public abstract File getGlobalConfigPath(); + + public abstract HardwareAbstractionLayer getHAL(); } diff --git a/src/zutil/osal/OsalLinuxImpl.java b/src/zutil/osal/OsalLinuxImpl.java index d02dfef..5411714 100644 --- a/src/zutil/osal/OsalLinuxImpl.java +++ b/src/zutil/osal/OsalLinuxImpl.java @@ -28,6 +28,8 @@ import java.io.File; * User: Ziver */ public class OsalLinuxImpl extends OSAbstractionLayer { + private static HALLinuxImpl hal; + @Override public OSType getOSType() { return OSType.Linux; @@ -72,4 +74,11 @@ public class OsalLinuxImpl extends OSAbstractionLayer { public File getGlobalConfigPath() { return new File("/etc"); } + + @Override + public HardwareAbstractionLayer getHAL() { + if(hal == null) + hal = new HALLinuxImpl(); + return hal; + } } diff --git a/src/zutil/osal/OsalWindowsImpl.java b/src/zutil/osal/OsalWindowsImpl.java index a2120b6..1253d53 100644 --- a/src/zutil/osal/OsalWindowsImpl.java +++ b/src/zutil/osal/OsalWindowsImpl.java @@ -30,17 +30,17 @@ import java.io.File; public class OsalWindowsImpl extends OSAbstractionLayer { @Override public OSType getOSType() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } @Override public String getOSName() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } @Override public String getOSVersion() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } @Override @@ -55,16 +55,21 @@ public class OsalWindowsImpl extends OSAbstractionLayer { @Override public String getUsername() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } @Override public File getUserConfigPath() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } @Override public File getGlobalConfigPath() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; + } + + @Override + public HardwareAbstractionLayer getHAL() { + return null; } } diff --git a/src/zutil/parser/Templator.java b/src/zutil/parser/Templator.java index 873dfb9..009a881 100644 --- a/src/zutil/parser/Templator.java +++ b/src/zutil/parser/Templator.java @@ -252,7 +252,7 @@ public class Templator { super.compile(str); } else if(obj instanceof Integer){ - if ((Integer) obj > 0) + if ((Integer) obj != 0) super.compile(str); } else if(obj instanceof Iterable){ @@ -273,7 +273,7 @@ public class Templator { // Reset map to parent object if(prevObj != null) - set(".", obj); + set(".", prevObj); else remove("."); } @@ -326,31 +326,29 @@ public class Templator { protected class TemplateDataAttribute implements TemplateEntity { private String tag; - private String key; - private String attrib; + private String[] keys; public TemplateDataAttribute(String tag){ this.tag = tag; - String[] s = tag.trim().split("\\.", 2); - this.key = s[0]; - if(this.key.isEmpty()) // tag starts with "." - this.key = "."; - if(s.length > 1) - this.attrib = s[1]; + this.keys = tag.trim().split("\\."); + if(this.keys.length == 0) + this.keys = new String[]{"."}; + if(this.keys[0].isEmpty()) // if tag starts with "." + this.keys[0] = "."; } public Object getObject(){ if (data.containsKey(tag)) return data.get(tag); - else if (data.containsKey(key) && data.get(key) != null) { - if (attrib != null) { - Object obj = getFieldValue(data.get(key), attrib); - if(obj != null) - return obj; + else if (data.containsKey(keys[0]) && data.get(keys[0]) != null) { + Object obj = data.get(keys[0]); + for(int i=1; i(); + classMap = new HashMap(); objectMap = new HashMap(); pluginVersion = data.getDouble("version"); pluginName = data.getString("name"); - log.fine("Plugin: "+this); + log.fine("Plugin: " + this); DataNode node = data.get("interfaces"); - Iterator 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 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 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; diff --git a/src/zutil/plugin/PluginManager.java b/src/zutil/plugin/PluginManager.java index 6af839a..c0a4e0b 100644 --- a/src/zutil/plugin/PluginManager.java +++ b/src/zutil/plugin/PluginManager.java @@ -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 implements Iterable{ public Iterator iterator() { return plugins.values().iterator(); } - public Iterator iterator(Class intf) { return new PluginInterfaceIterator(plugins.values().iterator(), intf); } + public ArrayList toArray() { + ArrayList list = new ArrayList(); + Iterator it = iterator(); + while(it.hasNext()) + list.add(it.next()); + return list; + } + public ArrayList toArray(Class intf) { + ArrayList list = new ArrayList(); + Iterator it = iterator(intf); + while(it.hasNext()) + list.add(it.next()); + return list; + } + + public class PluginInterfaceIterator implements Iterator { private Class intf; private Iterator it; @@ -129,7 +142,9 @@ public class PluginManager implements Iterable{ public T next() { if(!hasNext()) throw new NoSuchElementException(); - return next.getObject(intf); + T tmp = next.getObject(intf); + next = null; + return tmp; } @Override diff --git a/src/zutil/plugin/plugin.json.example b/src/zutil/plugin/plugin.json.example index 7c350cb..15b72cd 100644 --- a/src/zutil/plugin/plugin.json.example +++ b/src/zutil/plugin/plugin.json.example @@ -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"}, + ] } \ No newline at end of file