Java Code Examples for org.codehaus.groovy.runtime.InvokerHelper#createScript()
The following examples show how to use
org.codehaus.groovy.runtime.InvokerHelper#createScript() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachingGroovyEngine.java From groovy with Apache License 2.0 | 6 votes |
/** * Evaluate an expression. */ public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException { try { Class scriptClass = evalScripts.get(script); if (scriptClass == null) { scriptClass = loader.parseClass(script.toString(), source); evalScripts.put(script, scriptClass); } else { LOG.fine("eval() - Using cached script..."); } //can't cache the script because the context may be different. //but don't bother loading parsing the class again Script s = InvokerHelper.createScript(scriptClass, context); return s.run(); } catch (Exception e) { throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e); } }
Example 2
Source File: ScriptLauncher.java From groovy with Apache License 2.0 | 6 votes |
public void run() { final long id = Thread.currentThread().getId(); // run the script numIter times for (int i = 0; i < numIter; i++) { Builder builder = new Builder(); Binding binding = new Binding(); binding.setVariable("builder", builder); script = InvokerHelper.createScript(scriptClass, binding); script.run(); } latch.countDown(); }
Example 3
Source File: SecurityTestSupport.java From groovy with Apache License 2.0 | 6 votes |
protected void executeScript(Class scriptClass, Permission missingPermission) { try { Script script = InvokerHelper.createScript(scriptClass, new Binding()); script.run(); //InvokerHelper.runScript(scriptClass, null); } catch (AccessControlException ace) { if (missingPermission != null && missingPermission.implies(ace.getPermission())) { return; } else { fail(ace.toString()); } } if (missingPermission != null) { fail("Should catch an AccessControlException"); } }
Example 4
Source File: GroovyUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException { if (!baseScriptInitialized) { initBaseScript(); } // SCIPIO Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context)); Object result = null; if (UtilValidate.isEmpty(methodName)) { result = script.run(); } else { result = script.invokeMethod(methodName, new Object[] { context }); } return result; }
Example 5
Source File: XmlTemplateEngine.java From groovy with Apache License 2.0 | 5 votes |
public Writer writeTo(Writer out) { Script scriptObject = InvokerHelper.createScript(script.getClass(), binding); PrintWriter pw = new PrintWriter(out); scriptObject.setProperty("out", pw); scriptObject.run(); pw.flush(); return out; }
Example 6
Source File: FactoryBuilderSupport.java From groovy with Apache License 2.0 | 5 votes |
public Object build(Class viewClass) { if (Script.class.isAssignableFrom(viewClass)) { Script script = InvokerHelper.createScript(viewClass, this); return build(script); } else { throw new RuntimeException("Only scripts can be executed via build(Class)"); } }
Example 7
Source File: TestSupport.java From groovy with Apache License 2.0 | 5 votes |
protected void assertScript(final String text, final String scriptName) throws Exception { log.info("About to execute script"); log.info(text); GroovyCodeSource gcs = AccessController.doPrivileged( (PrivilegedAction<GroovyCodeSource>) () -> new GroovyCodeSource(text, scriptName, "/groovy/testSupport") ); Class groovyClass = loader.parseClass(gcs); Script script = InvokerHelper.createScript(groovyClass, new Binding()); script.run(); }
Example 8
Source File: GroovyEngine.java From scipio-erp with Apache License 2.0 | 4 votes |
private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException { if (UtilValidate.isEmpty(modelService.location)) { throw new GenericServiceException("Cannot run Groovy service with empty location"); } Map<String, Object> params = new HashMap<>(); params.putAll(context); Map<String, Object> gContext = new HashMap<>(); gContext.putAll(context); gContext.put(ScriptUtil.PARAMETERS_KEY, params); DispatchContext dctx = dispatcher.getLocalContext(localName); gContext.put("dctx", dctx); gContext.put("security", dctx.getSecurity()); gContext.put("dispatcher", dctx.getDispatcher()); gContext.put("delegator", dispatcher.getDelegator()); try { ScriptContext scriptContext = ScriptUtil.createScriptContext(gContext, protectedKeys); ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY); if (scriptHelper != null) { gContext.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper); } Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService)), GroovyUtil.getBinding(gContext)); Object resultObj = null; if (UtilValidate.isEmpty(modelService.invoke)) { resultObj = script.run(); } else { resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS); } if (resultObj == null) { resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY); } if (resultObj != null && resultObj instanceof Map<?, ?>) { return cast(resultObj); } Map<String, Object> result = ServiceUtil.returnSuccess(); result.putAll(modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE), ModelService.OUT_PARAM)); return result; } catch (GeneralException ge) { throw new GenericServiceException(ge); } catch (Exception e) { // detailMessage can be null. If it is null, the exception won't be properly returned and logged, and that will // make spotting problems very difficult. Disabling this for now in favor of returning a proper exception. throw new GenericServiceException("Error running Groovy method [" + modelService.invoke + "] in Groovy file [" + modelService.location + "]: ", e); } }
Example 9
Source File: TestSupport.java From groovy with Apache License 2.0 | 4 votes |
protected void assertScriptFile(String fileName) throws Exception { log.info("About to execute script: " + fileName); Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName))); Script script = InvokerHelper.createScript(groovyClass, new Binding()); script.run(); }
Example 10
Source File: GroovyUtil.java From scipio-erp with Apache License 2.0 | 2 votes |
/** * SCIPIO: Returns a {@link groovy.lang.Script} instance for the script groovy class at the given location, * initialized with the given {@link groovy.lang.Binding}. * <p> * Similar to calling (in Groovy code): * <pre> * {@code * def inst = GroovyUtil.getScriptClassFromLocation(location).newInstance(); * inst.setBinding(binding); * } * </pre> * <p> * NOTE: Although this can handle groovy classes that do not extend <code>groovy.lang.Script</code>, * in such cases you may want to call {@link #getScriptClassFromLocation} * together with <code>.newInstance()</code> instead of this. * <p> * Added 2017-01-26. * @see #getScriptClassFromLocation */ public static Script getScriptFromLocation(String location, Binding binding) throws GeneralException { if (!baseScriptInitialized) { initBaseScript(); } // SCIPIO return InvokerHelper.createScript(getScriptClassFromLocation(location), binding); }
Example 11
Source File: GroovyUtil.java From scipio-erp with Apache License 2.0 | 2 votes |
/** * SCIPIO: Returns a {@link groovy.lang.Script} instance for the script groovy class at the given location, * initialized with binding built from the given context. * <p> * Added 2017-01-26. * @see #getScriptFromLocation(String, Binding) */ public static Script getScriptFromLocation(String location, Map<String, Object> context) throws GeneralException { if (!baseScriptInitialized) { initBaseScript(); } // SCIPIO return InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context)); }
Example 12
Source File: GroovyShell.java From groovy with Apache License 2.0 | 2 votes |
/** * Parses the given script and returns it ready to be run. When running in a secure environment * (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be * given to the script. * * @param codeSource * @return ready to run script */ public Script parse(final GroovyCodeSource codeSource) throws CompilationFailedException { return InvokerHelper.createScript(parseClass(codeSource), context); }
Example 13
Source File: GroovyScriptEngine.java From groovy with Apache License 2.0 | 2 votes |
/** * 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); }