diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2014-07-28 12:23:52 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2014-07-28 12:23:52 +0200 |
| commit | 7234fd04b63e69756670e3605a4e778cbe6cf871 (patch) | |
| tree | c1a37a9b40378c041bcba4b39db57e06be10f94f | |
| parent | 7b3b3c6d26742fc6c2bd9ce4f9b0df1c19686e66 (diff) | |
Support pre-compilation of scripts if supported by the engine. Also does
this during testing to catch script syntax errors during build. Client
shell detects these error and declares them pre-execution.
| -rw-r--r-- | src/main/java/com/c2kernel/process/ClientShell.java | 5 | ||||
| -rw-r--r-- | src/main/java/com/c2kernel/scripting/Script.java | 35 | ||||
| -rw-r--r-- | src/test/java/MainTest.java | 13 |
3 files changed, 37 insertions, 16 deletions
diff --git a/src/main/java/com/c2kernel/process/ClientShell.java b/src/main/java/com/c2kernel/process/ClientShell.java index f109155..7456ba7 100644 --- a/src/main/java/com/c2kernel/process/ClientShell.java +++ b/src/main/java/com/c2kernel/process/ClientShell.java @@ -5,6 +5,7 @@ import java.util.Scanner; import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.scripting.Script;
+import com.c2kernel.scripting.ScriptParsingException;
public class ClientShell extends StandardClient {
@@ -21,13 +22,15 @@ public class ClientShell extends StandardClient { System.out.print("> ");
while (scan.hasNextLine()) {
String command = scan.nextLine();
- console.setScript(command);
try {
+ console.setScript(command);
Object response = console.execute();
if (response == null)
System.out.println("Ok");
else
System.out.println(response);
+ } catch (ScriptParsingException e) {
+ System.err.println("Syntax error: "+e.getMessage());
} catch (Throwable ex) {
ex.printStackTrace();
}
diff --git a/src/main/java/com/c2kernel/scripting/Script.java b/src/main/java/com/c2kernel/scripting/Script.java index 349a1a4..dfdd477 100644 --- a/src/main/java/com/c2kernel/scripting/Script.java +++ b/src/main/java/com/c2kernel/scripting/Script.java @@ -7,6 +7,8 @@ import java.util.ArrayList; import java.util.HashMap;
import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
@@ -42,8 +44,9 @@ import com.c2kernel.utils.Resource; public class Script
{
String mScript = "";
+ CompiledScript mCompScript = null;
String mName;
- String mVersion;
+ int 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>();
@@ -56,7 +59,8 @@ public class Script * @throws ScriptParsingException
* @throws ParameterException
*/
- public Script(String xml) throws ScriptParsingException, ParameterException {
+ public Script(String name, int version, String xml) throws ScriptParsingException, ParameterException {
+ mName = name; mVersion = version;
parseScriptXML(xml);
}
/**
@@ -79,7 +83,7 @@ public class Script public Script(String scriptName, int scriptVersion) throws ScriptingEngineException
{
mName = scriptName;
- mVersion = String.valueOf(scriptVersion);
+ mVersion = scriptVersion;
if (!scriptName.equals(""))
loadScript(mName, mVersion);
}
@@ -92,9 +96,9 @@ public class Script {
mName = "<expr>";
setScriptEngine(lang);
- mVersion = "";
+ mVersion = 0;
addOutput(null, returnType);
- mScript = expr;
+ setScript(expr);
}
/**
@@ -183,13 +187,13 @@ public class Script engine.setContext(context);
}
- private void loadScript(String scriptName, String scriptVersion) throws ScriptingEngineException
+ private void loadScript(String scriptName, int scriptVersion) throws ScriptingEngineException
{
try
{
mName = scriptName;
mVersion = scriptVersion;
- parseScriptXML(LocalObjectLoader.getScript(scriptName, scriptVersion));
+ parseScriptXML(LocalObjectLoader.getScript(scriptName, String.valueOf(scriptVersion)));
}
catch (ObjectNotFoundException e)
{
@@ -293,7 +297,7 @@ public class Script if (scriptChildNodes.getLength() != 1)
throw new ScriptParsingException("More than one child element found under script tag. Script characters may need escaping - suggest convert to CDATA section");
if (scriptChildNodes.item(0) instanceof Text)
- mScript = ((Text) scriptChildNodes.item(0)).getData();
+ setScript(((Text) scriptChildNodes.item(0)).getData());
else
throw new ScriptParsingException("Child element of script tag was not text");
Logger.msg(6, "Script.parseScriptXML() - script:" + mScript);
@@ -476,7 +480,10 @@ public class Script if (engine == null)
throw new ScriptingEngineException("Script engine not set. Cannot execute scripts.");
engine.put(ScriptEngine.FILENAME, mName);
- returnValue = engine.eval(mScript);
+ if (mCompScript != null)
+ returnValue = mCompScript.eval();
+ else
+ returnValue = engine.eval(mScript);
Logger.msg(7, "Script.execute() - script returned \"" + returnValue + "\"");
}
catch (Throwable ex)
@@ -517,8 +524,16 @@ public class Script return outputs;
}
- public void setScript(String script) {
+ public void setScript(String script) throws ScriptParsingException {
mScript = script;
+ if (engine instanceof Compilable) {
+ try {
+ Logger.debug("Compiling script "+mName);
+ mCompScript = ((Compilable)engine).compile(mScript);
+ } catch (ScriptException e) {
+ throw new ScriptParsingException(e.getMessage());
+ }
+ }
}
static public void main(String[] args) {
diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java index 0d35cd6..7809c25 100644 --- a/src/test/java/MainTest.java +++ b/src/test/java/MainTest.java @@ -10,6 +10,7 @@ import com.c2kernel.persistency.outcome.Schema; import com.c2kernel.persistency.outcome.SchemaValidator;
import com.c2kernel.process.Gateway;
import com.c2kernel.scripting.Script;
+import com.c2kernel.scripting.ScriptParsingException;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;
@@ -36,7 +37,6 @@ public class MainTest { String bootItems = FileStringUtility.url2String(Resource.getKernelResourceURL("boot/allbootitems.txt"));
StringTokenizer str = new StringTokenizer(bootItems, "\n\r");
- long castorTime=0;
while (str.hasMoreTokens()) {
String thisItem = str.nextToken();
Logger.msg(1, "Validating "+thisItem);
@@ -59,7 +59,6 @@ public class MainTest { String remarshalled = Gateway.getMarshaller().marshall(unmarshalled);
long now = System.currentTimeMillis();
Logger.msg("Marshall/remarshall of "+thisItem+" took "+(now-then)+"ms");
- castorTime+=(now-then);
errors = validator.validate(remarshalled);
if (errors.length() > 0) {
Logger.error("Remarshalled resource "+thisItem+" has errors :"+errors);
@@ -74,8 +73,12 @@ public class MainTest { // }
// assert xmlDiff.identical();
}
- }
- Logger.msg("Total Castor marshall time: "+castorTime+"ms");
+
+ if (itemType.equals("SC")) {
+ Logger.msg(1, "Parsing script "+thisItem);
+ new Script(thisItem, 0, data);
+ }
+ }
}
private static Schema getSchema(String name, int version, String resPath) throws ObjectNotFoundException {
@@ -91,7 +94,7 @@ public class MainTest { }
assert errors.length()==0;
- Script testScript = new Script(testScriptString);
+ Script testScript = new Script("TestScript", 0, testScriptString);
assert testScript.getInputParams().size()==1;
assert testScript.getInputParams().get("test")!=null;
|
