Is it possible to execute groovy code dynamically loaded in java application.
For example there is a database table which contains small pieces of groovy code, like:
def test(${val_to_insert_from_java}){
if (${val_to_insert_from_java} > 10){
return true;
}
return false;
}
Where ${val_to_insert_from_java} is a placeholder for some real value which gonna be inserted during execution of java code, like:
String groovyFuncSource = getFromDb();
groovyFuncSource.replace(${val_to_insert_from_java}, 9);
Object result = <evaluate somehow groovyFuncSource>;
Is there is a way to evaluate such piece of Groovy code? Or may be you advise me some other approach how to implemnt this.
Yes, you can do this.
See the link http://groovy.codehaus.org/Embedding+Groovy
You may want to use caution with this approach, because executing code stored in a database will introduce security flaws in your application. For example, some code maliciously inserted as text in your database could do … almost anything unless you’re checking it.