diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2012-04-26 00:33:53 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2012-04-26 00:33:53 +0200 |
| commit | fbd58d26a45c6efc9de89553c49465d8bef5f5d9 (patch) | |
| tree | 2e48cdb9b58c40edbcbefc6c0859b8c2bce88e07 /source | |
| parent | dacd1dc403149c6322edbb4d2402ef121bde6f2b (diff) | |
Script fixes
Diffstat (limited to 'source')
| -rw-r--r-- | source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java | 4 | ||||
| -rw-r--r-- | source/com/c2kernel/scripting/Parameter.java | 5 | ||||
| -rw-r--r-- | source/com/c2kernel/scripting/Script.java | 157 | ||||
| -rw-r--r-- | source/com/c2kernel/scripting/ScriptConsole.java | 4 |
4 files changed, 100 insertions, 70 deletions
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java b/source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java index 7809480..566d7c5 100644 --- a/source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java +++ b/source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java @@ -67,7 +67,7 @@ class OutcomeEditor extends JFrame implements ActionListener { }
try {
- schemaURL = schemaFile.toURL();
+ schemaURL = schemaFile.toURI().toURL();
} catch (Exception e) {
System.out.println("Invalid schema URL");
System.exit(1);
@@ -78,7 +78,7 @@ class OutcomeEditor extends JFrame implements ActionListener { }
try {
- instanceURL = instanceFile.toURL();
+ instanceURL = instanceFile.toURI().toURL();
} catch (Exception e) { }
try {
diff --git a/source/com/c2kernel/scripting/Parameter.java b/source/com/c2kernel/scripting/Parameter.java index a518c02..ed9a963 100644 --- a/source/com/c2kernel/scripting/Parameter.java +++ b/source/com/c2kernel/scripting/Parameter.java @@ -13,6 +13,11 @@ public class Parameter { public Parameter(String name) {
this.name = name;
}
+
+ public Parameter(String name, Class<?> type) {
+ this.name = name;
+ this.type = type;
+ }
public void setName(String n)
{
diff --git a/source/com/c2kernel/scripting/Script.java b/source/com/c2kernel/scripting/Script.java index ad98eb4..7e40003 100644 --- a/source/com/c2kernel/scripting/Script.java +++ b/source/com/c2kernel/scripting/Script.java @@ -6,6 +6,7 @@ import java.util.HashMap; import javax.script.Bindings;
import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -33,13 +34,12 @@ import com.c2kernel.utils.Logger; **************************************************************************/
public class Script
{
- Class<?> mOutputClass;
- String mOutputName;
String mScript = "";
String mName;
String mVersion;
HashMap<String, Parameter> mInputParams = new HashMap<String, Parameter>();
HashMap<String, Parameter> mAllInputParams = new HashMap<String, Parameter>();
+ HashMap<String, Parameter> mOutputParams = new HashMap<String, Parameter>();
ArrayList<Script> mIncludes = new ArrayList<Script>();
ScriptEngine engine;
Bindings beans;
@@ -79,7 +79,7 @@ public class Script mName = "<expr>";
setScriptEngine(lang);
mVersion = "";
- mOutputClass = returnType;
+ addOutput(null, returnType);
mScript = expr;
}
@@ -92,16 +92,20 @@ public class Script {
this(job.getActPropString("ScriptName"), job.getActPropString("ScriptVersion") == null ? -1 : Integer.parseInt(job.getActPropString("ScriptVersion")));
// set enviroment - this needs to be well documented for script developers
- addInputParam("item", "com.c2kernel.entity.proxy.ItemProxy");
+ if (!mInputParams.containsKey("item"))
+ addInputParam("item", ItemProxy.class);
setInputParamValue("item", object);
- addInputParam("agent", "com.c2kernel.entity.proxy.AgentProxy");
+ if (!mInputParams.containsKey("agent"))
+ addInputParam("agent", AgentProxy.class);
setInputParamValue("agent", subject);
- addInputParam("job", "com.c2kernel.entity.agent.Job");
+ if (!mInputParams.containsKey("job"))
+ addInputParam("job", Job.class);
setInputParamValue("job", job);
- setOutput("errors", "com.c2kernel.scripting.ErrorInfo");
+ if (!mOutputParams.containsKey("errors"))
+ addOutput("errors", ErrorInfo.class);
}
public void setScriptEngine(String lang) {
@@ -166,7 +170,7 @@ public class Script // Process script parameters
- // New input parameter
+ // input parameter
if (paramName.equals("param"))
{
if (!(currentParam.hasAttribute("name") && currentParam.hasAttribute("type")))
@@ -179,7 +183,7 @@ public class Script {
if (!currentParam.hasAttribute("type"))
throw new ScriptParsingException("Script Output declaration incomplete, must have type");
- setOutput(currentParam.getAttribute("name"), currentParam.getAttribute("type"));
+ addOutput(currentParam.getAttribute("name"), currentParam.getAttribute("type"));
}
//load any included scripts
@@ -207,7 +211,7 @@ public class Script else if (paramName.equals("script"))
{
if (!currentParam.hasAttribute("language"))
- throw new ScriptParsingException("Script data incomplete, must have language");
+ throw new ScriptParsingException("Script data incomplete, must specify scripting language");
Logger.msg(6, "Script.parseScriptXML() - Script Language: " + currentParam.getAttribute("language"));
setScriptEngine(currentParam.getAttribute("language"));
@@ -226,16 +230,21 @@ public class Script protected void addInputParam(String name, String type) throws ParameterException
{
- Parameter inputParam = new Parameter(name);
-
- try
+ try
{
- inputParam.setType(Class.forName(type));
+ addInputParam(name, Class.forName(type));
}
catch (ClassNotFoundException ex)
{
- throw new ParameterException("Input parameter " + inputParam.getName() + " specifies class " + type + " which was not found.");
+ throw new ParameterException("Input parameter " + name + " specifies class " + type + " which was not found.");
}
+ }
+
+ protected void addInputParam(String name, Class<?> type) throws ParameterException
+ {
+ Parameter inputParam = new Parameter(name, type);
+
+
Logger.msg(6, "ScriptExecutor.parseScriptXML() - declared parameter " + name + " (" + type + ")");
//add parameter to hashtable
@@ -265,40 +274,28 @@ public class Script }
-
- protected void setOutput(String name, String type) throws ScriptParsingException
+ protected void addOutput(String name, String type) throws ParameterException
{
- mOutputName = name;
-
- // set name to null if empty
- if (mOutputName != null && mOutputName.equals(""))
- mOutputName = null;
-
try
{
- Logger.msg(6, "Script.setOutput() - Output: " + name + " (" + type + ")");
- mOutputClass = Class.forName(type);
+ addOutput(name, Class.forName(type));
}
- catch (ClassNotFoundException ex)
- {
- throw new ScriptParsingException("Output class "+type+" not found");
+ catch (ClassNotFoundException ex) {
+ throw new ParameterException("Output parameter " + name + " specifies class " + type + " which was not found.");
}
+ }
- // set up the output object if named
- // we declare this now so imported scripts using the same object will not overwrite each other during execution
- if (mOutputName!=null)
- try
- {
- Logger.msg(8, "Script.setOutput() - Initialising output bean '" + mOutputName + "'");
- Object emptyObject = mOutputClass.newInstance();
- beans.put(mOutputName, emptyObject);
- }
- catch (Exception ex)
- {
- Logger.error(ex);
- throw new ScriptParsingException("Error initialising output bean: "+ex.getMessage());
- }
+ protected void addOutput(String name, Class<?> type) throws ParameterException
+ {
+ String outputName = name;
+
+ Parameter outputParam = new Parameter(name, type);
+ if (mOutputParams.containsKey(outputName))
+ throw new ParameterException("Output parameter '"+outputName+"' declared more than once.");
+
+ mOutputParams.put(outputName, outputParam);
+
}
/**
@@ -364,8 +361,6 @@ public class Script */
public Object execute() throws ScriptingEngineException
{
- Object returnValue = null;
- Object outputValue = null;
// check input params
StringBuffer missingParams = new StringBuffer();
@@ -377,13 +372,26 @@ public class Script if (missingParams.length() > 0)
throw new ScriptingEngineException("Execution aborted, the following declared parameters were not set: \n" + missingParams.toString());
+ for (Parameter outputParam : mOutputParams.values()) {
+ if (outputParam.getName() == null) continue; // If the name is null then it's the return type. don't pre-register it
+ Logger.msg(8, "Script.setOutput() - Initialising output bean '" + outputParam.getName() + "'");
+ Object emptyObject;
+ try {
+ emptyObject = outputParam.getType().newInstance();
+ } catch (Exception e) {
+ emptyObject = null;
+ }
+ beans.put(outputParam.getName(), emptyObject);
+
+ }
+
// execute the child scripts
for (Script importScript : mIncludes) {
importScript.execute();
}
-
// run the script
+ Object returnValue = null;
try
{
Logger.msg(7, "Script.execute() - Executing script");
@@ -391,32 +399,43 @@ public class Script throw new ScriptingEngineException("Script engine not set. Cannot execute scripts.");
returnValue = engine.eval(mScript, beans);
Logger.msg(8, "Script.execute() - script returned \"" + returnValue + "\"");
- if (mOutputName != null)
- {
- // retrieve the value from the registered output bean
- outputValue = engine.get(mOutputName);
- Logger.msg(8, "Script.execute() - output bean value: \"" + outputValue + "\"");
- }
}
catch (Exception ex)
{
throw new ScriptingEngineException("Error executing script: " + ex.getMessage());
}
-
- // if no output class specified, return null
- if (mOutputClass == null)
- return null;
-
- // if output name not specified, then check the return value against the output class
- if (mOutputName == null && returnValue != null && !(mOutputClass.isInstance(returnValue)))
- throw new ScriptingEngineException(
- "Script return value was not null or instance of " + mOutputClass.getName() + ", it was " + returnValue.getClass().getName());
-
- // return the output, or the return value if output name isn't defined
- if (mOutputName != null)
- return outputValue;
- else
- return returnValue;
+
+ // if no outputs are defined, return null
+ if (mOutputParams.size() == 0) {
+ Logger.msg(4, "Script.execute() - No output params. Returning null.");
+ return null;
+ }
+
+ HashMap<String, Object> outputs = new HashMap<String, Object>();
+
+ for (Parameter outputParam : mOutputParams.values()) {
+ String outputName = outputParam.getName();
+ Object outputValue;
+ if (outputName == null)
+ outputValue = returnValue;
+ else
+ outputValue = beans.get(outputParam.getName());
+ Logger.msg(4, "Script.execute() - Output parameter "+outputName+"="+(outputValue==null?"null":outputValue.toString()));
+
+ // check the class
+ if (outputValue!=null && !(outputParam.getType().isInstance(outputValue)))
+ throw new ScriptingEngineException(
+ "Script output "+outputName+" was not null or instance of " + outputParam.getType().getName() + ", it was a " + outputValue.getClass().getName());
+
+ Logger.msg(8, "Script.execute() - output "+outputValue);
+ if (mOutputParams.size() == 1) {
+ Logger.msg(6, "Script.execute() - only one parameter, returning "+(outputValue==null?"null":outputValue.toString()));
+ return outputValue;
+ }
+ outputs.put(outputParam.getName(), outputValue);
+ }
+
+ return outputs;
}
/**
@@ -428,4 +447,10 @@ public class Script parameter.setInitialised(false);
beans = engine.createBindings();
}
+
+ static public void main(String[] args) {
+ for(ScriptEngineFactory sef: new ScriptEngineManager().getEngineFactories()) {
+ System.out.println(sef.getEngineName()+" v"+sef.getEngineVersion()+" using "+sef.getLanguageName()+" v"+sef.getLanguageVersion());
+ }
+ }
}
diff --git a/source/com/c2kernel/scripting/ScriptConsole.java b/source/com/c2kernel/scripting/ScriptConsole.java index 2876f4c..be3f79f 100644 --- a/source/com/c2kernel/scripting/ScriptConsole.java +++ b/source/com/c2kernel/scripting/ScriptConsole.java @@ -210,7 +210,7 @@ public class ScriptConsole implements SocketHandler { }
else {
Object response = engine.eval(command, beans);
- if (response instanceof org.mozilla.javascript.Undefined)
+ if (response instanceof sun.org.mozilla.javascript.internal.Undefined)
output.println("Ok");
else
output.println(response);
@@ -226,4 +226,4 @@ public class ScriptConsole implements SocketHandler { shutdown();
}
}
- }
+}
|
