/** * Simple testing harness for the GSE. Enter script roots as arguments and * then input script names to run them. * * @param urls an array of URLs * @throws Exception if something goes wrong */ public static void main(String[] urls) throws Exception { GroovyScriptEngine gse = new GroovyScriptEngine(urls); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while (true) { System.out.print("groovy> "); if ((line = br.readLine()) == null || line.equals("quit")) { break; } try { System.out.println(gse.run(line, new Binding())); } catch (Exception e) { e.printStackTrace(); } } }
/** * Creates a Script with a given scriptName and binding. * * @param scriptName name of the script to run * @param binding the binding to pass to the script * @return the script object * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Script createScript(String scriptName, Binding binding) throws ResourceException, ScriptException { return InvokerHelper.createScript(loadScriptByName(scriptName), binding); }
/** * Run a script identified by name with a given binding. * * @param scriptName name of the script to run * @param binding the binding to pass to the script * @return an object * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Object run(String scriptName, Binding binding) throws ResourceException, ScriptException { return createScript(scriptName, binding).run(); }
/** * Hook method to setup the GroovyScriptEngine to use.<br> * Subclasses may override this method to provide a custom engine. */ protected GroovyScriptEngine createGroovyScriptEngine(){ return new GroovyScriptEngine(this); } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
public GroovyMod(File directory, String name) { Throwable ex = null; try { URL groovyURL = GroovyMod.class.getResource(""); GroovyScriptEngine scriptEngine = new GroovyScriptEngine(new URL[]{groovyURL, directory.toURI().toURL()}); CompilerConfiguration config = new CompilerConfiguration(); scriptEngine.setConfig(config); script = new GroovyRunner(directory, name, scriptEngine.getGroovyClassLoader()); } catch (Exception | AssertionError e) { ex = e; } this.exception = ex; }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { try { groovyScriptEngine = new GroovyScriptEngine( new URL[] { getScriptSynchronizer().getFileSystemRoot().toURI().toURL() }); groovyScriptEngine.getConfig().setVerbose(isVerbose()); groovyScriptEngine.getConfig().setDebug(isDebug()); } catch (MalformedURLException e) { throw new SiteWhereException("Unable to create Groovy script engine.", e); } }
private static synchronized GroovyScriptEngine getScriptEngine() throws DataAccessException, IOException, CachingException { if (scriptEngine == null || !AssetCache.isLoaded()) { CodeTimer timer = new CodeTimer("Initialize script libraries", true); initializeScriptLibraries(); initializeDynamicJavaAssets(); String[] rootDirs = new String[]{getRootDir()}; if (PackageCache.getPackage(Package.MDW + ".base").getCloudClassLoader() != null) scriptEngine = new GroovyScriptEngine(rootDirs, PackageCache.getPackage(Package.MDW + ".base").getCloudClassLoader()); else scriptEngine = new GroovyScriptEngine(rootDirs); // clear the cached library versions scriptEngine.getGroovyClassLoader().clearCache(); timer.stopAndLogTiming(""); } return scriptEngine; }
/** * Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @param parentClassLoader the class loader to use when loading the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script * @deprecated */ public Class loadScriptByName(String scriptName, ClassLoader parentClassLoader) throws ResourceException, ScriptException { initGroovyLoader(parentClassLoader); return loadScriptByName(scriptName); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name == null) { throw new NullPointerException(); } Class<?> c; try { c = gse.loadScriptByName(name); } catch (Exception e) { try { c = gse.getGroovyClassLoader().loadClass(name); } catch (ClassNotFoundException cnfe) { throw new ClassNotFoundException(name, cnfe); } } if (c == null) { throw new ClassNotFoundException(name); } return c; }
public MojarraGroovyClassLoader(GroovyScriptEngine gse) { super(new URL[0], gse.getGroovyClassLoader()); this.gse = gse; }
public URL loadGroovySource(String className) throws MalformedURLException { String filename; for (String extension : getConfig().getScriptExtensions()) { filename = className.replace('.', File.separatorChar) + "." + extension; try { URLConnection dependentScriptConn = rc.getResourceConnection(filename); return dependentScriptConn.getURL(); } catch (ResourceException e) { //TODO: maybe do something here? } } return rl.loadGroovySource(className); } });
Class scriptClass = new GroovyScriptEngine( "." ).loadScriptByName( "test.groovy" ) ; Object scriptInstance = scriptClass.newInstance() ; scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
/** * Run a script identified by name with a single argument. * * @param scriptName name of the script to run * @param argument a single argument passed as a variable named <code>arg</code> in the binding * @return a <code>toString()</code> representation of the result of the execution of the script * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public String run(String scriptName, String argument) throws ResourceException, ScriptException { Binding binding = new Binding(); binding.setVariable("arg", argument); Object result = run(scriptName, binding); return result == null ? "" : result.toString(); }
public static GroovyScriptEngine createScriptEngine(File classpathFile, ClassLoader parent) { CompilerConfiguration cc = new CompilerConfiguration(CompilerConfiguration.DEFAULT); String classpath = classpathFile.getAbsolutePath(); cc.setClasspath(classpath); cc.setRecompileGroovySource(true); GroovyScriptEngine scriptEngine; try { scriptEngine = new GroovyScriptEngine(new URL[] { classpathFile.toURI().toURL() }, parent); } catch (IOException e) { throw new Error(e); } scriptEngine.setConfig(cc); scriptEngine.getGroovyClassLoader().setShouldRecompile(true); return scriptEngine; } }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { resourceConnector = new GlobalResourceConnector(); groovyScriptEngine = new GroovyScriptEngine(resourceConnector); groovyScriptEngine.getConfig().setVerbose(isVerbose()); groovyScriptEngine.getConfig().setDebug(isDebug()); LOGGER.info( "Global Groovy script engine configured with (verbose:" + isVerbose() + ") (debug:" + isDebug() + ")."); }
/** * Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @param parentClassLoader the class loader to use when loading the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script * @deprecated */ public Class loadScriptByName(String scriptName, ClassLoader parentClassLoader) throws ResourceException, ScriptException { initGroovyLoader(parentClassLoader); return loadScriptByName(scriptName); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name == null) { throw new NullPointerException(); } Class<?> c; try { c = gse.getGroovyClassLoader().getParent().loadClass(name); } catch (ClassNotFoundException cnfe) { try { c = gse.loadScriptByName(name); } catch (Exception e) { throw new ClassNotFoundException(name, e); } } if (c == null) { throw new ClassNotFoundException(name); } return c; }
public MojarraGroovyClassLoader(GroovyScriptEngine gse) { super(new URL[0], gse.getGroovyClassLoader()); gse.getGroovyClassLoader().setShouldRecompile(Boolean.TRUE); this.gse = gse; }
public URL loadGroovySource(String className) throws MalformedURLException { String filename; for (String extension : getConfig().getScriptExtensions()) { filename = className.replace('.', File.separatorChar) + "." + extension; try { URLConnection dependentScriptConn = rc.getResourceConnection(filename); return dependentScriptConn.getURL(); } catch (ResourceException e) { //TODO: maybe do something here? } } return rl.loadGroovySource(className); } });