diff options
| author | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
|---|---|---|
| committer | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
| commit | 0ec8481c10cd8277d84c7c1a785483a0a739e5a0 (patch) | |
| tree | 5f6e5d9ae75193e67e6f3b3dfa488960c5cde1d5 /source/com/c2kernel/scripting | |
| parent | 036cbdba66f804743c4c838ed598d6972c4b3e17 (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')
| -rw-r--r-- | source/com/c2kernel/scripting/ErrorInfo.java | 14 | ||||
| -rw-r--r--[-rwxr-xr-x] | source/com/c2kernel/scripting/Parameter.java | 24 | ||||
| -rw-r--r-- | source/com/c2kernel/scripting/Script.java | 82 | ||||
| -rw-r--r-- | source/com/c2kernel/scripting/ScriptConsole.java | 29 | ||||
| -rw-r--r--[-rwxr-xr-x] | source/com/c2kernel/scripting/ScriptingEngineException.java | 2 |
5 files changed, 73 insertions, 78 deletions
diff --git a/source/com/c2kernel/scripting/ErrorInfo.java b/source/com/c2kernel/scripting/ErrorInfo.java index 1e813ec..f14038f 100644 --- a/source/com/c2kernel/scripting/ErrorInfo.java +++ b/source/com/c2kernel/scripting/ErrorInfo.java @@ -1,7 +1,6 @@ package com.c2kernel.scripting;
import java.util.ArrayList;
-import java.util.Iterator;
/**************************************************************************
*
@@ -14,29 +13,28 @@ import java.util.Iterator; public class ErrorInfo {
ArrayList<String> msg;
boolean fatal = false;
-
+
public ErrorInfo() {
super();
msg = new ArrayList<String>();
}
-
+
public void addError(String error) {
msg.add(error);
}
-
+
public String getErrors() {
StringBuffer err = new StringBuffer();
- for (Iterator iter = msg.iterator(); iter.hasNext();) {
- String element = (String)iter.next();
+ for (String element : msg) {
err.append(element+"\n");
}
return err.toString();
}
-
+
public void setFatal() {
fatal=true;
}
-
+
public boolean getFatal() {
return fatal;
}
diff --git a/source/com/c2kernel/scripting/Parameter.java b/source/com/c2kernel/scripting/Parameter.java index c51c9d4..a518c02 100755..100644 --- a/source/com/c2kernel/scripting/Parameter.java +++ b/source/com/c2kernel/scripting/Parameter.java @@ -5,43 +5,43 @@ package com.c2kernel.scripting; * Place holder for the Parameter details to be passed to the script.
**************************************************************************/
public class Parameter {
-
+
private String name;
- private Class type;
+ private Class<?> type;
private boolean initialised=false;
-
+
public Parameter(String name) {
this.name = name;
}
-
+
public void setName(String n)
{
name=n;
}
-
+
public String getName()
{
return name;
}
-
- public void setType(Class t)
+
+ public void setType(Class<?> t)
{
type=t;
}
-
- public Class getType()
+
+ public Class<?> getType()
{
return type;
}
-
+
public void setInitialised(boolean state)
{
initialised=state;
}
-
+
public boolean getInitialised()
{
return initialised;
}
-
+
}
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();
}
}
diff --git a/source/com/c2kernel/scripting/ScriptConsole.java b/source/com/c2kernel/scripting/ScriptConsole.java index d79cd5c..33d4b7f 100644 --- a/source/com/c2kernel/scripting/ScriptConsole.java +++ b/source/com/c2kernel/scripting/ScriptConsole.java @@ -4,7 +4,6 @@ import java.io.BufferedReader; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
-import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
@@ -47,7 +46,7 @@ public class ScriptConsole implements SocketHandler { static ArrayList<String> securityHosts = new ArrayList<String>();
public static final short NONE = 0;
public static final short ALLOW = 1;
- public static final short DENY = 2;
+ public static final short DENY = 2;
static short securityMode;
static {
@@ -75,18 +74,20 @@ public class ScriptConsole implements SocketHandler { public ScriptConsole() {
}
- public String getName() {
+ @Override
+ public String getName() {
return "Script Console";
}
- public boolean isBusy() {
+ @Override
+ public boolean isBusy() {
return (socket != null);
}
- public void setSocket(Socket newSocket) {
+ @Override
+ public void setSocket(Socket newSocket) {
try {
input = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));
- OutputStreamWriter ansi = new OutputStreamWriter(newSocket.getOutputStream(), "US-ASCII");
output = new PrintStream(newSocket.getOutputStream());
newSocket.setSoTimeout(0);
socket = newSocket;
@@ -100,7 +101,8 @@ public class ScriptConsole implements SocketHandler { }
}
- public void shutdown() {
+ @Override
+ public void shutdown() {
Socket closingSocket = socket;
socket = null;
if (closingSocket == null)
@@ -118,7 +120,8 @@ public class ScriptConsole implements SocketHandler { }
- public void run() {
+ @Override
+ public void run() {
// check permission
boolean allowed = true;
if (securityMode!=NONE) {
@@ -127,7 +130,7 @@ public class ScriptConsole implements SocketHandler { allowed = false;
}
else if (securityMode==ALLOW)
- allowed = false;
+ allowed = false;
}
if (!allowed) {
@@ -150,7 +153,7 @@ public class ScriptConsole implements SocketHandler { try {
manager.declareBean("system", Gateway.getProxyManager().getProxy(
Gateway.getLDAPLookup().getRoleManager().getAgentPath("system")), AgentProxy.class);
- } catch (Exception ex) {
+ } catch (Exception ex) {
output.println("System agent unavailable");
}
context = manager.loadScriptingEngine("javascript");
@@ -200,7 +203,7 @@ public class ScriptConsole implements SocketHandler { continue;
}
try {
- if (command.endsWith("\\")) {
+ if (command.endsWith("\\")) {
commandBuffer.append(command.substring(0,command.length()-1));
continue;
}
@@ -208,11 +211,11 @@ public class ScriptConsole implements SocketHandler { command = commandBuffer.toString();
commandBuffer = new StringBuffer();
Logger.msg("Console command from "+socket.getInetAddress()+": "+command);
-
+
// process control
if (command.equals("shutdown")) {
WrapperManager.stop(0);
- }
+ }
else {
Object response = context.eval("Command", 0, 0, command);
if (response instanceof org.mozilla.javascript.Undefined)
diff --git a/source/com/c2kernel/scripting/ScriptingEngineException.java b/source/com/c2kernel/scripting/ScriptingEngineException.java index 2c8128c..ab8383c 100755..100644 --- a/source/com/c2kernel/scripting/ScriptingEngineException.java +++ b/source/com/c2kernel/scripting/ScriptingEngineException.java @@ -1,7 +1,7 @@ package com.c2kernel.scripting;
public class ScriptingEngineException extends java.lang.Exception {
-
+
/**
* Creates new <code>sciptingEngineException</code> without detail message.
*/
|
