ZEPPELIN-2176 comments from @AhyoungRyu

- Zeppelin follows Google Java code
- interpreter alphabetical order in _navigation.html
- direct link to MarkupBuilder in groovy help
This commit is contained in:
dlukyanov 2017-03-29 22:17:11 +03:00
parent fe08159e89
commit 3dd53e2586
4 changed files with 471 additions and 436 deletions

View file

@ -61,6 +61,7 @@
<li><a href="{{BASE_PATH}}/interpreter/elasticsearch.html">Elasticsearch</a></li>
<li><a href="{{BASE_PATH}}/interpreter/flink.html">Flink</a></li>
<li><a href="{{BASE_PATH}}/interpreter/geode.html">Geode</a></li>
<li><a href="{{BASE_PATH}}/interpreter/groovy.html">Groovy</a></li>
<li><a href="{{BASE_PATH}}/interpreter/hbase.html">HBase</a></li>
<li><a href="{{BASE_PATH}}/interpreter/hdfs.html">HDFS</a></li>
<li><a href="{{BASE_PATH}}/interpreter/hive.html">Hive</a></li>
@ -78,7 +79,6 @@
<li><a href="{{BASE_PATH}}/interpreter/scio.html">Scio</a></li>
<li><a href="{{BASE_PATH}}/interpreter/shell.html">Shell</a></li>
<li><a href="{{BASE_PATH}}/interpreter/spark.html">Spark</a></li>
<li><a href="{{BASE_PATH}}/interpreter/groovy.html">Groovy</a></li>
</ul>
</li>
<li>

View file

@ -103,7 +103,7 @@ In this case with name `PROPERTY_NAME`
* `groovy.xml.MarkupBuilder g.html()`
Starts or continues rendering of `%angular` to output and returns [groovy.xml.MarkupBuilder](https://www.google.com/search?q=groovy.xml.MarkupBuilder)
Starts or continues rendering of `%angular` to output and returns [groovy.xml.MarkupBuilder](http://groovy-lang.org/processing-xml.html#_markupbuilder)
MarkupBuilder is usefull to generate html (xml)
* `void g.table(obj)`

View file

@ -18,7 +18,9 @@ package org.apache.zeppelin.groovy;
import java.io.StringWriter;
import org.slf4j.Logger;
import java.util.Properties;
import java.util.Collection;
import java.util.Map;
@ -41,308 +43,328 @@ import org.apache.zeppelin.interpreter.InterpreterException;
/**
* Groovy interpreter for Zeppelin.
* @author dlukyanov@ukr.net / dmitry lukyanov
*/
public class GObject extends groovy.lang.GroovyObjectSupport {
Logger log;
StringWriter out;
Properties props;
InterpreterContext interpreterContext;
Map<String,Object> bindings;
public GObject(Logger log, StringWriter out, Properties p, InterpreterContext ctx, Map<String,Object> bindings){
this.log=log;
this.out=out;
this.interpreterContext=ctx;
this.props=p;
this.bindings=bindings;
}
public Object getProperty(String key){
if("log".equals(key))return log;
return props.getProperty(key);
}
public void setProperty(String key, Object value){
throw new RuntimeException("Set properties not supported: "+key+"="+value);
}
public Properties getProperties(){
return props;
}
private void startOutputType(String type){
StringBuffer sb=out.getBuffer();
if( sb.length()>0 ){
if( sb.length()<type.length() || !type.equals(sb.substring(0,type.length())) ){
log.error("try to start output `"+type+"` after non-"+type+" started");
}
}else{
out.append(type);
out.append('\n');
}
}
/**
* returns gui object
*/
public GUI getGui(){
return interpreterContext.getGui();
}
@ZeppelinApi
public Object input(String name) {
return input(name, "");
}
Logger log;
StringWriter out;
Properties props;
InterpreterContext interpreterContext;
Map<String, Object> bindings;
@ZeppelinApi
public Object input(String name, Object defaultValue) {
return getGui().input(name, defaultValue);
}
private ParamOption[] toParamOptions(Map<Object, String> options){
ParamOption[] paramOptions = new ParamOption[options.size()];
int i = 0;
for(Map.Entry<Object,String> e : options.entrySet()){
paramOptions[i++] = new ParamOption(e.getKey(), e.getValue());
}
return paramOptions;
}
@ZeppelinApi
public Object select(String name, Map<Object, String> options) {
return select( name, "", options );
}
@ZeppelinApi
public Object select(String name, Object defaultValue, Map<Object, String> options) {
return getGui().select( name, defaultValue, toParamOptions(options) );
}
public GObject(Logger log, StringWriter out, Properties p, InterpreterContext ctx,
Map<String, Object> bindings) {
this.log = log;
this.out = out;
this.interpreterContext = ctx;
this.props = p;
this.bindings = bindings;
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Map<Object, String> options) {
return checkbox(name, options.keySet(), options);
}
public Object getProperty(String key) {
if ("log".equals(key)) {
return log;
}
return props.getProperty(key);
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Collection<Object> defaultChecked, Map<Object, String> options) {
return getGui().checkbox(name, defaultChecked, toParamOptions(options));
}
/**
* Returns shared variable if it was previously set.
* The same as getting groovy script variables but this method will return null if script variable not assigned.
* To understand groovy script variables see groovy.transform.Field annotation for more information.
* @see #put
*/
public Object get(String varName){
return bindings.get(varName);
}
/**
* Returns script (shared) variable value but if value was not set returns default value.
* The same as getting groovy script variables but this method will return default value if script variable not assigned.
* To understand groovy script variables see groovy.transform.Field annotation for more information.
* @see #put
*/
public Object get(String varName, Object defValue){
return bindings.containsKey(varName) ? bindings.get(varName) : defValue;
}
/**
* Sets a ne value to interpreter's shared variables.
* Could be set by <code>put('varName', newValue )</code>
* or by just assigning <code>varName = value</code> without declaring a variable.
*/
public Object put(String varName, Object newValue){
return bindings.put(varName, newValue);
}
/**
* starts or continues rendering html/angular and returns MarkupBuilder to build html.
* <pre> g.html().with{
* h1("hello")
* h2("world")
* }</pre>
*/
public MarkupBuilder html(){
startOutputType("%angular");
return new MarkupBuilder(out);
}
/**
* starts or continues rendering table rows
* @param obj:
* 1. List(rows) of List(columns) where first line is a header
*/
public void table(Object obj){
if(obj==null)return;
StringBuffer sb=out.getBuffer();
startOutputType("%table");
if(obj instanceof groovy.lang.Closure){
//if closure run and get result collection
obj = ((Closure)obj).call();
}
if(obj instanceof Collection){
int count = 0;
for(Object row : ((Collection)obj)){
count++;
boolean rowStarted = false;
if(row instanceof Collection){
for( Object field: ((Collection)row) ){
if(rowStarted)sb.append('\t');
sb.append(field);
rowStarted=true;
}
}else{
sb.append(row);
}
sb.append('\n');
}
}else{
throw new RuntimeException("Not supported table value :"+obj.getClass());
}
}
private AngularObject getAngularObject(String name) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
String noteId = interpreterContext.getNoteId();
// try get local object
AngularObject paragraphAo = registry.get(name, noteId, interpreterContext.getParagraphId());
AngularObject noteAo = registry.get(name, noteId, null);
public void setProperty(String key, Object value) {
throw new RuntimeException("Set properties not supported: " + key + "=" + value);
}
AngularObject ao = paragraphAo != null ? paragraphAo : noteAo;
public Properties getProperties() {
return props;
}
if (ao == null) {
// then global object
ao = registry.get(name, null, null);
}
return ao;
}
/**
* Get angular object. Look up notebook scope first and then global scope
* @param name variable name
* @return value
*/
public Object angular(String name) {
AngularObject ao = getAngularObject(name);
if (ao == null) {
return null;
} else {
return ao.get();
}
}
private void startOutputType(String type) {
StringBuffer sb = out.getBuffer();
if (sb.length() > 0) {
if (sb.length() < type.length() || !type.equals(sb.substring(0, type.length()))) {
log.error("try to start output `" + type + "` after non-" + type + " started");
}
} else {
out.append(type);
out.append('\n');
}
}
@SuppressWarnings("unchecked")
public void angularBind(String name, Object o, String noteId) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
/**
* returns gui object
*/
public GUI getGui() {
return interpreterContext.getGui();
}
if (registry.get(name, noteId, null) == null) {
registry.add(name, o, noteId, null);
} else {
registry.get(name, noteId, null).set(o);
}
}
@ZeppelinApi
public Object input(String name) {
return input(name, "");
}
/**
* Create angular variable in notebook scope and bind with front end Angular display system.
* If variable exists, it'll be overwritten.
* @param name name of the variable
* @param o value
*/
public void angularBind(String name, Object o) {
angularBind(name, o, interpreterContext.getNoteId());
}
@ZeppelinApi
public Object input(String name, Object defaultValue) {
return getGui().input(name, defaultValue);
}
/*------------------------------------------RUN----------------------------------------*/
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId, String paragraphId, InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId, paragraphId);
}
return new LinkedList<InterpreterContextRunner>();
}
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId, InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId);
}
return new LinkedList<InterpreterContextRunner>();
}
/**
* Run paragraph by id
* @param noteId
* @param paragraphId
*/
@ZeppelinApi
public void run(String noteId, String paragraphId) {
run(noteId, paragraphId, interpreterContext);
}
private ParamOption[] toParamOptions(Map<Object, String> options) {
ParamOption[] paramOptions = new ParamOption[options.size()];
int i = 0;
for (Map.Entry<Object, String> e : options.entrySet()) {
paramOptions[i++] = new ParamOption(e.getKey(), e.getValue());
}
return paramOptions;
}
/**
* Run paragraph by id
* @param paragraphId
*/
@ZeppelinApi
public void run(String paragraphId) {
String noteId = interpreterContext.getNoteId();
run(noteId, paragraphId, interpreterContext);
}
/**
* Run paragraph by id
* @param noteId
* @param context
*/
@ZeppelinApi
public void run(String noteId, String paragraphId, InterpreterContext context) {
if (paragraphId.equals(context.getParagraphId())) {
throw new InterpreterException("Can not run current Paragraph");
}
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, paragraphId, context);
if (runners.size() <= 0) {
throw new InterpreterException("Paragraph " + paragraphId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
r.run();
}
}
public void runNote(String noteId) {
runNote(noteId, interpreterContext);
}
@ZeppelinApi
public Object select(String name, Map<Object, String> options) {
return select(name, "", options);
}
public void runNote(String noteId, InterpreterContext context) {
String runningNoteId = context.getNoteId();
String runningParagraphId = context.getParagraphId();
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, context);
@ZeppelinApi
public Object select(String name, Object defaultValue, Map<Object, String> options) {
return getGui().select(name, defaultValue, toParamOptions(options));
}
if (runners.size() <= 0) {
throw new InterpreterException("Note " + noteId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
if (r.getNoteId().equals(runningNoteId) && r.getParagraphId().equals(runningParagraphId)) {
continue;
}
r.run();
}
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll() {
runAll(interpreterContext);
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Map<Object, String> options) {
return checkbox(name, options.keySet(), options);
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll(InterpreterContext context) {
runNote(context.getNoteId());
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Collection<Object> defaultChecked,
Map<Object, String> options) {
return getGui().checkbox(name, defaultChecked, toParamOptions(options));
}
/**
* Returns shared variable if it was previously set. The same as getting groovy script variables
* but this method will return null if script variable not assigned. To understand groovy script
* variables see groovy.transform.Field annotation for more information.
*
* @see #put
*/
public Object get(String varName) {
return bindings.get(varName);
}
/**
* Returns script (shared) variable value but if value was not set returns default value. The same
* as getting groovy script variables but this method will return default value if script variable
* not assigned. To understand groovy script variables see groovy.transform.Field annotation for
* more information.
*
* @see #put
*/
public Object get(String varName, Object defValue) {
return bindings.containsKey(varName) ? bindings.get(varName) : defValue;
}
/**
* Sets a new value to interpreter's shared variables.
* Could be set by <code>put('varName', newValue )</code>
* or by just assigning <code>varName = value</code> without declaring a variable.
*/
public Object put(String varName, Object newValue) {
return bindings.put(varName, newValue);
}
/**
* starts or continues rendering html/angular and returns MarkupBuilder to build html.
* <pre> g.html().with{
* h1("hello")
* h2("world")
* }</pre>
*/
public MarkupBuilder html() {
startOutputType("%angular");
return new MarkupBuilder(out);
}
/**
* starts or continues rendering table rows
*
* @param obj: 1. List(rows) of List(columns) where first line is a header
*/
public void table(Object obj) {
if (obj == null) {
return;
}
StringBuffer sb = out.getBuffer();
startOutputType("%table");
if (obj instanceof groovy.lang.Closure) {
//if closure run and get result collection
obj = ((Closure) obj).call();
}
if (obj instanceof Collection) {
int count = 0;
for (Object row : ((Collection) obj)) {
count++;
boolean rowStarted = false;
if (row instanceof Collection) {
for (Object field : ((Collection) row)) {
if (rowStarted) {
sb.append('\t');
}
sb.append(field);
rowStarted = true;
}
} else {
sb.append(row);
}
sb.append('\n');
}
} else {
throw new RuntimeException("Not supported table value :" + obj.getClass());
}
}
private AngularObject getAngularObject(String name) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
String noteId = interpreterContext.getNoteId();
// try get local object
AngularObject paragraphAo = registry.get(name, noteId, interpreterContext.getParagraphId());
AngularObject noteAo = registry.get(name, noteId, null);
AngularObject ao = paragraphAo != null ? paragraphAo : noteAo;
if (ao == null) {
// then global object
ao = registry.get(name, null, null);
}
return ao;
}
/**
* Get angular object. Look up notebook scope first and then global scope
*
* @param name variable name
* @return value
*/
public Object angular(String name) {
AngularObject ao = getAngularObject(name);
if (ao == null) {
return null;
} else {
return ao.get();
}
}
@SuppressWarnings("unchecked")
public void angularBind(String name, Object o, String noteId) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
if (registry.get(name, noteId, null) == null) {
registry.add(name, o, noteId, null);
} else {
registry.get(name, noteId, null).set(o);
}
}
/**
* Create angular variable in notebook scope and bind with front end Angular display system.
* If variable exists, it'll be overwritten.
*
* @param name name of the variable
* @param o value
*/
public void angularBind(String name, Object o) {
angularBind(name, o, interpreterContext.getNoteId());
}
/*------------------------------------------RUN----------------------------------------*/
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId,
String paragraphId, InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId, paragraphId);
}
return new LinkedList<InterpreterContextRunner>();
}
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId,
InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId);
}
return new LinkedList<InterpreterContextRunner>();
}
/**
* Run paragraph by id
*/
@ZeppelinApi
public void run(String noteId, String paragraphId) {
run(noteId, paragraphId, interpreterContext);
}
/**
* Run paragraph by id
*/
@ZeppelinApi
public void run(String paragraphId) {
String noteId = interpreterContext.getNoteId();
run(noteId, paragraphId, interpreterContext);
}
/**
* Run paragraph by id
*/
@ZeppelinApi
public void run(String noteId, String paragraphId, InterpreterContext context) {
if (paragraphId.equals(context.getParagraphId())) {
throw new InterpreterException("Can not run current Paragraph");
}
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, paragraphId,
context);
if (runners.size() <= 0) {
throw new InterpreterException("Paragraph " + paragraphId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
r.run();
}
}
public void runNote(String noteId) {
runNote(noteId, interpreterContext);
}
public void runNote(String noteId, InterpreterContext context) {
String runningNoteId = context.getNoteId();
String runningParagraphId = context.getParagraphId();
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, context);
if (runners.size() <= 0) {
throw new InterpreterException("Note " + noteId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
if (r.getNoteId().equals(runningNoteId) && r.getParagraphId().equals(runningParagraphId)) {
continue;
}
r.run();
}
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll() {
runAll(interpreterContext);
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll(InterpreterContext context) {
runNote(context.getNoteId());
}
}

View file

@ -55,159 +55,172 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Groovy interpreter for Zeppelin.
* @author dlukyanov@ukr.net / dmitry lukyanov
*/
public class GroovyInterpreter extends Interpreter {
Logger log = LoggerFactory.getLogger(GroovyInterpreter.class);
GroovyShell shell = null; //new GroovyShell();
//here we will store shared variables. concurrent just in case.
Map<String,Object> sharedBindings = new ConcurrentHashMap<String,Object>();
//cache for groovy compiled scripts
Map<String,Class<Script>> scriptCache = Collections.synchronizedMap( new WeakHashMap<String,Class<Script>>(1000) );
public GroovyInterpreter(Properties property) {
super(property);
}
@Override
public void open() {
CompilerConfiguration conf = new CompilerConfiguration();
conf.setDebug(true);
shell = new GroovyShell(conf);
String classes = getProperty("GROOVY_CLASSES");
if(classes==null || classes.length()==0){
try {
File jar = new File(GroovyInterpreter.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
classes = new File(jar.getParentFile(),"classes").toString();
}catch(Exception e){}
Logger log = LoggerFactory.getLogger(GroovyInterpreter.class);
GroovyShell shell = null; //new GroovyShell();
//here we will store Interpreters shared variables. concurrent just in case.
Map<String, Object> sharedBindings = new ConcurrentHashMap<String, Object>();
//cache for groovy compiled scripts
Map<String, Class<Script>> scriptCache = Collections
.synchronizedMap(new WeakHashMap<String, Class<Script>>(100));
public GroovyInterpreter(Properties property) {
super(property);
}
@Override
public void open() {
CompilerConfiguration conf = new CompilerConfiguration();
conf.setDebug(true);
shell = new GroovyShell(conf);
String classes = getProperty("GROOVY_CLASSES");
if (classes == null || classes.length() == 0) {
try {
File jar = new File(
GroovyInterpreter.class.getProtectionDomain().getCodeSource().getLocation().toURI()
.getPath());
classes = new File(jar.getParentFile(), "classes").toString();
} catch (Exception e) {
}
}
log.info("groovy classes classpath: " + classes);
if (classes != null && classes.length() > 0) {
File fClasses = new File(classes);
if (!fClasses.exists()) {
fClasses.mkdirs();
}
shell.getClassLoader().addClasspath(classes);
}
}
@Override
public void close() {
shell = null;
}
@Override
public FormType getFormType() {
return FormType.NATIVE;
//return FormType.NONE;
}
@Override
public int getProgress(InterpreterContext context) {
return 0;
}
@Override
public Scheduler getScheduler() {
return SchedulerFactory.singleton()
.createOrGetParallelScheduler(GroovyInterpreter.class.getName() + this.hashCode(), 10);
}
private Job getRunningJob(String paragraphId) {
Job foundJob = null;
Collection<Job> jobsRunning = getScheduler().getJobsRunning();
for (Job job : jobsRunning) {
if (job.getId().equals(paragraphId)) {
foundJob = job;
}
}
return foundJob;
}
@Override
public List<InterpreterCompletion> completion(String buf, int cursor) {
return null;
}
@SuppressWarnings("unchecked")
Script getGroovyScript(String id, String scriptText) /*throws SQLException*/ {
if (shell == null) {
throw new RuntimeException("Groovy Shell is not initialized: null");
}
try {
Class<Script> clazz = scriptCache.get(scriptText);
if (clazz == null) {
String scriptName = id + "_" + Long.toHexString(scriptText.hashCode()) + ".groovy";
clazz = (Class<Script>) shell.parse(scriptText, scriptName).getClass();
scriptCache.put(scriptText, clazz);
}
Script script = (Script) clazz.newInstance();
return script;
} catch (Throwable t) {
throw new RuntimeException("Failed to parse groovy script: " + t, t);
}
}
private static Set<String> predefinedBindings = new HashSet<String>();
static {
predefinedBindings.add("g");
predefinedBindings.add("out");
}
@Override
@SuppressWarnings("unchecked")
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
try {
Script script = getGroovyScript(contextInterpreter.getParagraphId(), cmd);
Job runningJob = getRunningJob(contextInterpreter.getParagraphId());
runningJob.info()
.put("CURRENT_THREAD", Thread.currentThread()); //to be able to terminate thread
Map<String, Object> bindings = script.getBinding().getVariables();
bindings.clear();
StringWriter out = new StringWriter((int) (cmd.length() * 1.75));
//put shared bindings evaluated in this interpreter
bindings.putAll(sharedBindings);
//put predefined bindings
bindings.put("g", new GObject(log, out, this.getProperty(), contextInterpreter, bindings));
bindings.put("out", new PrintWriter(out, true));
script.run();
//let's get shared variables defined in current script and store them in shared map
for (Map.Entry<String, Object> e : bindings.entrySet()) {
if (!predefinedBindings.contains(e.getKey())) {
if (log.isTraceEnabled()) {
log.trace("groovy script variable " + e); //let's see what we have...
}
sharedBindings.put(e.getKey(), e.getValue());
}
log.info("groovy classes classpath: "+classes);
if(classes!=null && classes.length()>0 ){
File fClasses = new File(classes);
if(!fClasses.exists())fClasses.mkdirs();
shell.getClassLoader().addClasspath(classes);
}
bindings.clear();
InterpreterResult result = new InterpreterResult(Code.SUCCESS, out.toString());
//log.info("RESULT: "+result);
return result;
} catch (Throwable t) {
t = StackTraceUtils.deepSanitize(t);
String msg = t.toString() + "\n at " + t.getStackTrace()[0];
log.error("Failed to run script: " + t + "\n" + cmd + "\n", t);
return new InterpreterResult(Code.ERROR, msg);
}
}
@Override
public void cancel(InterpreterContext context) {
Job runningJob = getRunningJob(context.getParagraphId());
if (runningJob != null) {
Map<String, Object> info = runningJob.info();
Object object = info.get("CURRENT_THREAD");
if (object instanceof Thread) {
try {
Thread t = (Thread) object;
t.dumpStack();
t.interrupt();
//t.stop();
} catch (Throwable t) {
log.error("Failed to cancel script: " + t, t);
}
}
@Override
public void close() {
shell = null;
}
@Override
public FormType getFormType() {
return FormType.NATIVE;
//return FormType.NONE;
}
}
}
}
@Override
public int getProgress(InterpreterContext context) {
return 0;
}
@Override
public Scheduler getScheduler() {
return SchedulerFactory.singleton().createOrGetParallelScheduler(GroovyInterpreter.class.getName() + this.hashCode(), 10);
}
private Job getRunningJob(String paragraphId) {
Job foundJob = null;
Collection<Job> jobsRunning = getScheduler().getJobsRunning();
for (Job job : jobsRunning) {
if (job.getId().equals(paragraphId)) {
foundJob = job;
}
}
return foundJob;
}
@Override
public List<InterpreterCompletion> completion(String buf, int cursor) {
return null;
}
@SuppressWarnings("unchecked")
Script getGroovyScript(String id, String scriptText) /*throws SQLException*/ {
if(shell==null){
throw new RuntimeException("Groovy Shell is not initialized: null");
}
try{
Class<Script> clazz = scriptCache.get(scriptText);
if(clazz==null){
String scriptName=id+"_"+Long.toHexString(scriptText.hashCode())+".groovy";
clazz = (Class<Script>) shell.parse(scriptText, scriptName).getClass();
scriptCache.put(scriptText,clazz);
}
Script script=(Script)clazz.newInstance();
return script;
}catch(Throwable t){
throw new RuntimeException("Failed to parse groovy script: "+t,t);
}
}
private static Set<String> predefinedBindings = new HashSet<String>();
static{
predefinedBindings.add("g");
predefinedBindings.add("out");
}
@Override
@SuppressWarnings("unchecked")
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
try {
Script script = getGroovyScript(contextInterpreter.getParagraphId(), cmd);
Job runningJob = getRunningJob(contextInterpreter.getParagraphId());
runningJob.info().put("CURRENT_THREAD", Thread.currentThread()); //to be able to terminate thread
Map<String,Object> bindings = script.getBinding().getVariables();
bindings.clear();
StringWriter out = new StringWriter( (int) (cmd.length()*1.75) );
//put shared bindings evaluated in this interpreter
bindings.putAll(sharedBindings);
//put predefined bindings
bindings.put("g", new GObject(log, out, this.getProperty(), contextInterpreter, bindings) );
bindings.put("out", new PrintWriter(out, true));
script.run();
//let's get shared variables defined in current script and store them in shared map
for(Map.Entry<String,Object> e : bindings.entrySet()){
if( !predefinedBindings.contains(e.getKey()) ){
if(log.isTraceEnabled())log.trace("groovy script variable "+e); //let's see what we have...
sharedBindings.put( e.getKey(), e.getValue() );
}
}
bindings.clear();
InterpreterResult result = new InterpreterResult(Code.SUCCESS, out.toString());
//log.info("RESULT: "+result);
return result;
}catch(Throwable t){
t = StackTraceUtils.deepSanitize(t);
String msg = t.toString()+"\n at "+t.getStackTrace()[0];
log.error("Failed to run script: "+t+"\n" + cmd+"\n", t);
return new InterpreterResult(Code.ERROR, msg);
}
}
@Override
public void cancel(InterpreterContext context) {
Job runningJob = getRunningJob(context.getParagraphId());
if (runningJob != null) {
Map<String, Object> info = runningJob.info();
Object object = info.get("CURRENT_THREAD");
if (object instanceof Thread) {
try {
Thread t = (Thread) object;
t.dumpStack();
t.interrupt();
//t.stop();
}catch(Throwable t){
log.error("Failed to cancel script: "+t, t);
}
}
}
}
}