Hi all,
Just thought I'd share a very simple example of scripting service, using the Beanshell java interpreter (BeanShell - Lightweight Scripting for Java) .
Basically the idea is: write a line of java code on the client, and have it sent as a Symphony Task, and executed on the node.
Output looks like this:
C:\Work\dev\scriptapp>java -classpath "C:\Work\Tools\dev\SymphonyDE\DE32\3.2\win32-vc7\lib\JavaSoamApi.jar;.\bin" SyncClient
connection ID=ddd85c58-ffff-ffff-c000-005056c00008-5516-276
Session ID:16436
> s="hector";
task submitted with ID : 1
> s.substring(1,4);
task submitted with ID : 2
> 8+2
task submitted with ID : 3
Receiving results
1> hector
2> ect
3> 10
Session closed
Connection closed
All Done !!
What happened is: a java String object (named s, containing 'hector') was created in the interpreter on the remote Symphony node, and a string derived from it by calling the substring method on the 's' object (again, remotely on the symphony node). On the third one, the interpreter just summed the two integers (which btw also means that this code may be used as a remote calculator
)
Output of interpreted lines was read and displayed by our client.
Mechanism is simple:
The client just send strings taken from the prompt, as Symphony tasks (you'll notice here: nothing to do in Symphony to send String objects, just as for any Serializable object)
Code:
String scriptLine = stdin.readLine();
// read lines of text and send them as symphony tasks
while(scriptLine.compareTo("")!=0) { // Send a string via Symphony
session.sendTaskInput(scriptLine);
scriptLine = stdin.readLine();
}
And all the service has to do is get that string, hand it over to the beanshell interpreter and send the result back as a String.
Code:
public void onInvoke (TaskContext taskContext) throws SoamException
{// get the input
String input = (String)taskContext.getTaskInput();
// create a beanshell interpreter and give it the input to evaluate
bsh.Interpreter bsh = new bsh.Interpreter();
String result =bsh.eval(input);
// and send the result of Beanshell's evaluation as the task output
taskContext.setTaskOutput(result);
}
The result strings are processed synchronously (the effect would be more striking with Asynchronous messages, but I'll leave that to proper programmers
) and just displayed on the screen.
The code I'm attaching is a tad more convoluted, as I'm making a bit more use of Beanshell, but the essence is the same.
Also please note there would be loads to do on proper error treatment....
What could that be used for ? Well that just means that by extension, any use you could have for a scripting environment (testing, for once), you retrieve in Symphony by such a simple mechanism.
Or, as a remote calculator.
Cheers,
Guillaume
P.S.: The attachment should contain everything you need (ant build file, service descriptor, and java files & libs) let me know if sthg's missing. Also note the suffix is '.gz' but it's really a RAR file.