summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/scripting/Script.java
diff options
context:
space:
mode:
authorabranson <andrew.branson@cern.ch>2011-08-04 00:42:34 +0200
committerabranson <andrew.branson@cern.ch>2011-08-04 00:42:34 +0200
commit0ec8481c10cd8277d84c7c1a785483a0a739e5a0 (patch)
tree5f6e5d9ae75193e67e6f3b3dfa488960c5cde1d5 /source/com/c2kernel/scripting/Script.java
parent036cbdba66f804743c4c838ed598d6972c4b3e17 (diff)
More code cleanup:
Refactored Entity Proxy Subscription to handle generics better Rewrote RemoteMap to use TreeMap instead of the internal array for order. It now sorts its keys by number if they parse, else as strings. Removed a no-longer-in-progress outcome form class
Diffstat (limited to 'source/com/c2kernel/scripting/Script.java')
-rw-r--r--source/com/c2kernel/scripting/Script.java82
1 files changed, 38 insertions, 44 deletions
diff --git a/source/com/c2kernel/scripting/Script.java b/source/com/c2kernel/scripting/Script.java
index 4abb5cf..0820c26 100644
--- a/source/com/c2kernel/scripting/Script.java
+++ b/source/com/c2kernel/scripting/Script.java
@@ -3,7 +3,6 @@ package com.c2kernel.scripting;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -33,7 +32,7 @@ import com.ibm.bsf.BSFManager;
**************************************************************************/
public class Script
{
- Class mOutputClass;
+ Class<?> mOutputClass;
String mOutputName;
String mScript = "";
String mName;
@@ -48,26 +47,26 @@ public class Script
* Loads script xml and parses it for script source, parameters and output specifications.
* First tries to load the script from resource path /scriptFiles/scriptName_scriptVersion.xml
* If not found tries to find item at /desc/ScriptDesc/scriptName and load Viewpoint scriptVersion from it.
- *
+ *
* For the specification of script xml, see the Script schema from resources.
- *
+ *
* @param scriptName - name of the script
* @param scriptVersion - named version of the script (must be numbered viewpoint)
- * @throws ScriptParsingException - when script not found (ScriptLoadingException) or xml is invalid (ScriptParsingException)
+ * @throws ScriptParsingException - when script not found (ScriptLoadingException) or xml is invalid (ScriptParsingException)
*/
- public Script(String scriptName, int scriptVersion) throws ScriptingEngineException
+ public Script(String scriptName, int scriptVersion) throws ScriptingEngineException
{
this(scriptName, scriptVersion, new BSFManager());
}
-
+
public Script(String scriptName, int scriptVersion, BSFManager scriptManager) throws ScriptingEngineException
{
- mName = scriptName;
+ mName = scriptName;
mVersion = String.valueOf(scriptVersion);
if (scriptName.equals("")) return;
setScriptEnv(scriptManager);
setScript(mName, mVersion);
- }
+ }
/**
* Creates a script executor for the supplied expression, bypassing the xml parsing bit
@@ -82,12 +81,12 @@ public class Script
mOutputClass = Object.class;
mScript = expr;
}
-
+
public Script(String lang, String expr) throws ScriptingEngineException
{
this(lang, expr, new BSFManager());
}
-
+
public Script(ItemProxy object, AgentProxy subject, Job job) throws ScriptingEngineException
{
this(job.getActPropString("ScriptName"), job.getActPropString("ScriptVersion") == null ? -1 : Integer.parseInt(job.getActPropString("ScriptVersion")));
@@ -103,7 +102,7 @@ public class Script
setOutput("errors", "com.c2kernel.scripting.ErrorInfo");
}
-
+
public void setScript(String scriptName, String scriptVersion) throws ScriptingEngineException
{
try
@@ -121,10 +120,10 @@ public class Script
public void setScriptEnv(BSFManager manager) {
this.scriptManager = manager;
}
-
+
/**
* Extracts script data from script xml.
- *
+ *
* @param scriptXML
* @throws ScriptParsingException - when script is invalid
*/
@@ -189,10 +188,9 @@ public class Script
String includeName = currentParam.getAttribute("name");
String includeVersion = currentParam.getAttribute("version");
try {
- Script includedScript = new Script(includeName, Integer.parseInt(includeVersion), scriptManager);
+ Script includedScript = new Script(includeName, Integer.parseInt(includeVersion), scriptManager);
mIncludes.add(includedScript);
- for (Iterator iter = includedScript.getInputParams().values().iterator(); iter.hasNext();) {
- Parameter includeParam = (Parameter) iter.next();
+ for (Parameter includeParam : includedScript.getInputParams().values()) {
addIncludedInputParam(includeParam.getName(), includeParam.getType());
}
} catch (NumberFormatException e) {
@@ -200,8 +198,8 @@ public class Script
} catch (ScriptingEngineException e) {
throw new ScriptParsingException("Error parsing imported script "+includeName+"_"+includeVersion+": "+e.getMessage());
}
-
-
+
+
}
//load Script
else if (paramName.equals("script"))
@@ -243,12 +241,12 @@ public class Script
mAllInputParams.put(inputParam.getName(), inputParam);
}
-
- protected void addIncludedInputParam(String name, Class type) throws ParameterException
+
+ protected void addIncludedInputParam(String name, Class<?> type) throws ParameterException
{
// check if we already have it
if (mAllInputParams.containsKey(name)) {
- Parameter existingParam = (Parameter)mAllInputParams.get(name);
+ Parameter existingParam = mAllInputParams.get(name);
// check the types match
if (existingParam.getType() == type)
return; // matches
@@ -256,7 +254,7 @@ public class Script
throw new ParameterException("Parameter conflict. Parameter'"+name+"' is declared as "
+existingParam.getType().getName()+" is declared in another script as "+type.getName());
}
-
+
Parameter inputParam = new Parameter(name);
inputParam.setType(type);
@@ -264,7 +262,7 @@ public class Script
mAllInputParams.put(inputParam.getName(), inputParam);
}
-
+
protected void setOutput(String name, String type) throws ScriptParsingException
{
@@ -306,7 +304,7 @@ public class Script
* @return HashMap of String (name), com.c2kernel.scripting.Parameter (param)
* @see com.c2kernel.scripting.Parameter
*/
- public HashMap getInputParams()
+ public HashMap<String, Parameter> getInputParams()
{
return mInputParams;
}
@@ -316,21 +314,21 @@ public class Script
* @return HashMap of String (name), com.c2kernel.scripting.Parameter (param)
* @see com.c2kernel.scripting.Parameter
*/
- public HashMap getAllInputParams()
+ public HashMap<String, Parameter> getAllInputParams()
{
return mAllInputParams;
}
/**
* Submits an input parameter to the script. Must be declared by name and type in the script XML.
- *
+ *
* @param name - input parameter name from the script xml
* @param value - object to use for this parameter
* @throws ParameterException - name not found or wrong type
*/
public void setInputParamValue(String name, Object value) throws ParameterException
{
- Parameter param = (Parameter) mInputParams.get(name);
+ Parameter param = mInputParams.get(name);
if (!mAllInputParams.containsKey(name))
throw new ParameterException("Parameter " + name + " not found in parameter list");
@@ -347,22 +345,21 @@ public class Script
throw new ParameterException("Error initialising parameter '"+name+"' - "+ex.getMessage());
}
}
-
+
// pass param down to child scripts
- for (Iterator iter = mIncludes.iterator(); iter.hasNext();) {
- Script importScript = (Script) iter.next();
+ for (Script importScript : mIncludes) {
importScript.setInputParamValue(name, value);
}
}
/**
* Executes the script with the submitted parameters. All declared input parametes should have been set first.
- *
- * @return The return value depends on the way the output type was declared in the script xml.
+ *
+ * @return The return value depends on the way the output type was declared in the script xml.
* <ul><li>If there was no output class declared then null is returned
* <li>If a class was declared, but not named, then the object returned by the script is checked
* to be of that type, then returned.
- * <li>If the output value was named and typed, then an object of that class is created and
+ * <li>If the output value was named and typed, then an object of that class is created and
* passed to the script as an input parameter. The script should set this before it returns.
* </ul>
* @throws ScriptingEngineException - input parameters weren't set, there was an error executing the script, or the output was invalid
@@ -374,9 +371,7 @@ public class Script
// check input params
StringBuffer missingParams = new StringBuffer();
- for (Iterator iter = mInputParams.values().iterator(); iter.hasNext();)
- {
- Parameter thisParam = (Parameter) iter.next();
+ for (Parameter thisParam : mInputParams.values()) {
if (!thisParam.getInitialised())
missingParams.append(thisParam.getName()).append("\n");
}
@@ -385,11 +380,10 @@ public class Script
throw new ScriptingEngineException("Execution aborted, the following declared parameters were not set: \n" + missingParams.toString());
// execute the child scripts
- for (Iterator iter = mIncludes.iterator(); iter.hasNext();) {
- Script importScript = (Script) iter.next();
+ for (Script importScript : mIncludes) {
importScript.execute();
}
-
+
// run the script
try
@@ -400,13 +394,13 @@ public class Script
Logger.msg(8, "Script.execute() - script returned \"" + returnValue + "\"");
if (mOutputName != null)
{
- // retrieve the value from the registered output bean
+ // retrieve the value from the registered output bean
outputValue = scriptManager.lookupBean(mOutputName);
Logger.msg(8, "Script.execute() - output bean value: \"" + outputValue + "\"");
}
}
catch (Exception ex)
- {
+ {
throw new ScriptingEngineException("Error executing script: " + ex.getMessage());
}
@@ -431,8 +425,8 @@ public class Script
*/
public void reset()
{
- for (Iterator iter = mInputParams.values().iterator(); iter.hasNext();)
- ((Parameter) iter.next()).setInitialised(false);
+ for (Parameter parameter : mInputParams.values())
+ parameter.setInitialised(false);
scriptManager = new BSFManager();
}
}