I use GroovyShell as an "evaluator / expression engine" inside my program. It accepts two inputs: (a) one or more initialization scripts (b) a user script. Both of them are then concatenated at runtime as a large piece of script (text) and passed to the shell.
String initScripts = getFromDB() String userScript = getFromUser() def shell = new GroovyShell() output = shell.evaluate(initScripts + userScript)
The above code will run in a loop where the contents of userScript will change.
So far, initScripts only contains variable definitions (for example, def $yyyy = new Date().format('yyyy') ) that can be referenced in userScript (for example, print "$yyyy 001" ).
Is there a more efficient approach for this? (For example, shell reuse, how?) Because now it is very slow.
Edit: Groovy is a must. Using a different scripting engine is not recommended.
Edit: I think GroovyShell can do this (pseudocode):
def shell = new GroovyShell() shell.evaluate(initScripts) for each userScript in DB { shell.put(userScript ) def result = shell.evaluateThat() println "Result is $result" }
Is it possible? (The last time I googled it was not possible, but I hope I am wrong)
wiradikusuma
source share