- try {
- /* The following code performs the GroovyScriptEngine invocation using reflection,
- * and is equivalent to this fragment from the embedding groovy documentation on
- * the groovy site:
- * <code>
- import groovy.lang.Binding;
- import groovy.util.GroovyScriptEngine;
-
- String[] roots = new String[] { "/my/groovy/script/path" };
- GroovyScriptEngine gse = new GroovyScriptEngine(roots);
- Binding binding = new Binding();
- binding.setVariable("input", "world");
- gse.run("hello.groovy", binding);
- * </code>
- */
- ClassLoader cl = jalviewContext.getClass().getClassLoader();
- Class gbindingc = cl.loadClass("groovy.lang.Binding");
- Constructor gbcons = gbindingc.getConstructor(null);
- Object gbinding = gbcons.newInstance(null);
- java.lang.reflect.Method setvar = gbindingc.getMethod("setVariable", new Class[] { String.class, Object.class} );
- setvar.invoke(gbinding, new Object[] { "Jalview", jalviewContext});
- Class gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
- Constructor gseccons = gsec.getConstructor(new Class[] { String[].class });
- Object gse = gseccons.newInstance(new Object[] { new String[] { sfile.toURL().toString() } });
- java.lang.reflect.Method run = gsec.getMethod("run", new Class[]{ String.class, gbindingc });
- run.invoke(gse, new Object[] { sfile.toURL().toString(), gbinding });
- success = true;
+ try
+ {
+ /*
+ * The following code performs the GroovyScriptEngine invocation using
+ * reflection, and is equivalent to this fragment from the embedding
+ * groovy documentation on the groovy site: <code> import
+ * groovy.lang.Binding; import groovy.util.GroovyScriptEngine;
+ *
+ * String[] roots = new String[] { "/my/groovy/script/path" };
+ * GroovyScriptEngine gse = new GroovyScriptEngine(roots); Binding binding
+ * = new Binding(); binding.setVariable("input", "world");
+ * gse.run("hello.groovy", binding); </code>
+ */
+ Class[] bspec;
+ Object[] binding;
+ int blen = ((jalviewContext[0] == null) ? 0 : 1)
+ + ((jalviewContext[1] == null) ? 0 : 1);
+ String cnames[] = new String[]
+ { "Jalview", "currentAlFrame" };
+ bspec = new Class[blen * 2];
+ binding = new Object[blen * 2];
+ blen = 0;
+ ClassLoader cl = null;
+ Map vbinding = new Hashtable();
+ for (int jc = 0; jc < jalviewContext.length; jc++)
+ {
+ if (jalviewContext[jc] != null)
+ {
+ if (cl == null)
+ {
+ cl = jalviewContext[jc].getClass().getClassLoader();
+ }
+ bspec[blen * 2] = String.class;
+ bspec[blen * 2 + 1] = Object.class;
+ binding[blen * 2] = cnames[jc];
+ binding[blen * 2 + 1] = jalviewContext[jc];
+ vbinding.put(cnames[jc], jalviewContext[jc]);
+ blen++;
+ }
+ }
+ Class gbindingc = cl.loadClass("groovy.lang.Binding");
+ Constructor gbcons;
+ Object gbinding;
+ try
+ {
+ gbcons = gbindingc.getConstructor(Map.class);
+ gbinding = gbcons.newInstance(vbinding);
+ } catch (NoSuchMethodException x)
+ {
+ // old style binding config - using series of string/object values to
+ // setVariable.
+ gbcons = gbindingc.getConstructor(null);
+ gbinding = gbcons.newInstance(null);
+ java.lang.reflect.Method setvar = gbindingc.getMethod(
+ "setVariable", bspec);
+ setvar.invoke(gbinding, binding);
+ }
+ ;
+ Class gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
+ Constructor gseccons = gsec.getConstructor(new Class[]
+ { URL[].class }); // String[].class });
+ Object gse = gseccons.newInstance(new Object[]
+ { new URL[]
+ { sfile } }); // .toString() } });
+ java.lang.reflect.Method run = gsec.getMethod("run", new Class[]
+ { String.class, gbindingc });
+ run.invoke(gse, new Object[]
+ { sfile.toString(), gbinding });
+ success = true;