+ startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol,
+ format);
+ getFeatures = checkDasArguments(aparser);
+ // extract groovy arguments before anything else.
+ }
+ // If the user has specified features to be retrieved,
+ // or a groovy script to be executed, do them if they
+ // haven't been done already
+ // fetch features for the default alignment
+ if (getFeatures != null)
+ {
+ if (startUpAlframe != null)
+ {
+ startFeatureFetching(getFeatures);
+ }
+ }
+ // Once all other stuff is done, execute any groovy scripts (in order)
+ if (groovyscript != null)
+ {
+ if (jalview.bin.Cache.groovyJarsPresent())
+ {
+ System.out.println("Executing script " + groovyscript);
+ executeGroovyScript(groovyscript, new Object[] { desktop, startUpAlframe});
+ }
+ else
+ {
+ System.err
+ .println("Sorry. Groovy Support is not available, so ignoring the provided groovy script "
+ + groovyscript);
+ }
+ }
+ // and finally, turn off batch mode indicator - if the desktop still exists
+ if (desktop != null)
+ {
+ if (progress!=-1) {
+ desktop.setProgressBar(null, progress);
+ }
+ desktop.setInBatchMode(false);
+ }
+ }
+
+ private static void startUsageStats(final Desktop desktop)
+ {
+ /**
+ * start a User Config prompt asking if we can log usage statistics.
+ */
+ jalview.gui.PromptUserConfig prompter = new jalview.gui.PromptUserConfig(
+ desktop.desktop,
+ "USAGESTATS",
+ "Jalview Usage Statistics",
+ "Do you want to help make Jalview better by enabling "
+ + "the collection of usage statistics with Google Analytics ?"
+ + "\n\n(you can enable or disable usage tracking in the preferences)",
+ new Runnable()
+ {
+ public void run()
+ {
+ Cache.log
+ .info("Initialising googletracker for usage stats.");
+ Cache.initGoogleTracker();
+ Cache.log.debug("Tracking enabled.");
+ }
+ }, new Runnable()
+ {
+ public void run()
+ {
+ Cache.log.info("Not enabling Google Tracking.");
+ }
+ }, null, true);
+ SwingUtilities.invokeLater(prompter);
+ }
+
+ /**
+ * Locate the given string as a file and pass it to the groovy interpreter.
+ *
+ * @param groovyscript
+ * the script to execute
+ * @param jalviewContext
+ * the Jalview Desktop object passed in to the groovy binding as the
+ * 'Jalview' object.
+ */
+ private static void executeGroovyScript(String groovyscript,
+ Object[] jalviewContext)
+ {
+ if (jalviewContext == null)
+ {
+ System.err
+ .println("Sorry. Groovy support is currently only available when running with the Jalview GUI enabled.");
+ }
+ /**
+ * for scripts contained in files
+ */
+ File tfile = null;
+ /**
+ * script's URI
+ */
+ URL sfile = null;
+ if (groovyscript.trim().equals("STDIN"))
+ {
+ // read from stdin into a tempfile and execute it
+ try
+ {
+ tfile = File.createTempFile("jalview", "groovy");
+ PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
+ new FileOutputStream(tfile)));
+ BufferedReader br = new BufferedReader(
+ new java.io.InputStreamReader(System.in));
+ String line = null;
+ while ((line = br.readLine()) != null)
+ {
+ outfile.write(line + "\n");
+ }
+ br.close();
+ outfile.flush();
+ outfile.close();
+
+ } catch (Exception ex)
+ {
+ System.err.println("Failed to read from STDIN into tempfile "
+ + ((tfile == null) ? "(tempfile wasn't created)" : tfile
+ .toString()));
+ ex.printStackTrace();
+ return;
+ }
+ try {
+ sfile = tfile.toURI().toURL();
+ } catch (Exception x)
+ {
+ System.err.println("Unexpected Malformed URL Exception for temporary file created from STDIN: "+tfile.toURI());
+ x.printStackTrace();
+ return;
+ }
+ }
+ else
+ {
+ try
+ {
+ sfile = new URI(groovyscript).toURL();
+ } catch (Exception x)
+ {
+ tfile = new File(groovyscript);
+ if (!tfile.exists())
+ {
+ System.err.println("File '" + groovyscript + "' does not exist.");
+ return;
+ }
+ if (!tfile.canRead())
+ {
+ System.err.println("File '" + groovyscript + "' cannot be read.");
+ return;
+ }
+ if (tfile.length() < 1)
+ {
+ System.err.println("File '" + groovyscript + "' is empty.");
+ return;
+ }
+ try {
+ sfile = tfile.getAbsoluteFile().toURI().toURL();
+ } catch (Exception ex)
+ {
+ System.err.println("Failed to create a file URL for "+tfile.getAbsoluteFile());
+ return;
+ }
+ }
+ }
+ boolean success = false;
+ 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;
+ } catch (Exception e)
+ {
+ System.err.println("Exception Whilst trying to execute file " + sfile
+ + " as a groovy script.");
+ e.printStackTrace(System.err);