allow groovy scripts to be run when in headless mode - pass startupAlFrame as context
authorjprocter <Jim Procter>
Fri, 8 Apr 2011 10:57:44 +0000 (10:57 +0000)
committerjprocter <Jim Procter>
Fri, 8 Apr 2011 10:57:44 +0000 (10:57 +0000)
src/jalview/bin/Jalview.java

index 80f3722..9383656 100755 (executable)
@@ -670,15 +670,36 @@ public class Jalview
        * = new Binding(); binding.setVariable("input", "world");
        * gse.run("hello.groovy", binding); </code>
        */
-      ClassLoader cl = (jalviewContext[0]!=null ? jalviewContext[0] : jalviewContext[1]).getClass().getClassLoader();
+      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;
+      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];
+         blen++;
+        }
+      }
       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[0],"startupAlFrame",jalviewContext[1] });
+              bspec);
+      
+      setvar.invoke(gbinding, binding);
       Class gsec = cl.loadClass("groovy.util.GroovyScriptEngine");
       Constructor gseccons = gsec.getConstructor(new Class[]
       { URL[].class }); // String[].class });