List of usage examples for org.springframework.scripting ScriptSource getScriptAsString
String getScriptAsString() throws IOException;
From source file:org.arrow.service.DefaultExecutionScriptService.java
/** * {@inheritDoc}//from ww w .j ava 2 s.c o m */ @Override public Object evaluateCompiledGroovy(ScriptSource source, ScriptEvaluationContext context) { try { final String script = source.getScriptAsString(); if (GROOVY_SCRIPT_CACHE.containsKey(script.hashCode())) { return GROOVY_SCRIPT_CACHE.get(script.hashCode()).run(); } GroovyShell shell = new GroovyShell(); context.getArguments().forEach((k, v) -> shell.setVariable((String) k, v)); Script gs = shell.parse(new StringReader(source.getScriptAsString())); GROOVY_SCRIPT_CACHE.put(script.hashCode(), gs); return gs.run(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.red5.server.script.groovy.GroovyScriptFactory.java
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException { synchronized (this.scriptClassMonitor) { if (this.scriptClass == null || scriptSource.isModified()) { this.scriptClass = this.groovyClassLoader.parseClass(scriptSource.getScriptAsString()); if (Script.class.isAssignableFrom(this.scriptClass)) { // A Groovy script, probably creating an instance: let's execute it. Object result = executeScript(this.scriptClass); this.scriptResultClass = (result != null ? result.getClass() : null); } else { this.scriptResultClass = this.scriptClass; }//from w w w . j a v a2s. c om } return this.scriptResultClass; } }
From source file:org.red5.server.script.groovy.GroovyScriptFactory.java
/** * Loads and parses the Groovy script via the GroovyClassLoader. * @see groovy.lang.GroovyClassLoader/*from w w w . j a v a 2 s .c o m*/ */ @SuppressWarnings({ "rawtypes" }) public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces) throws IOException, ScriptCompilationException { try { Class<?> scriptClassToExecute = null; synchronized (this.scriptClassMonitor) { if (this.scriptClass == null || scriptSource.isModified()) { this.scriptClass = this.groovyClassLoader.parseClass(scriptSource.getScriptAsString()); if (Script.class.isAssignableFrom(this.scriptClass)) { // A Groovy script, probably creating an instance: let's execute it. Object result = executeScript(this.scriptClass); this.scriptResultClass = (result != null ? result.getClass() : null); return result; } else { this.scriptResultClass = this.scriptClass; } } scriptClassToExecute = this.scriptClass; } // Process re-execution outside of the synchronized block. return executeScript(scriptClassToExecute); } catch (CompilationFailedException ex) { throw new ScriptCompilationException("Could not compile Groovy script: " + scriptSource, ex); } }
From source file:org.red5.server.script.jython.JythonScriptFactory.java
/** {@inheritDoc} */ @SuppressWarnings({ "rawtypes" }) public Object getScriptedObject(ScriptSource scriptSourceLocator, Class[] scriptInterfaces) throws IOException, ScriptCompilationException { String basePath = ""; /* TODO: how to do this when running under Tomcat? ContextHandler handler = WebAppContext.getCurrentWebAppContext(); if (handler != null) {//w ww. j a v a2s. c o m File root = handler.getBaseResource().getFile(); if (root != null && root.exists()) { basePath = root.getAbsolutePath() + File.separator + "WEB-INF" + File.separator; } } */ String strScript = scriptSourceLocator.getScriptAsString(); if (scriptInterfaces.length > 0) { try { PySystemState state = new PySystemState(); if (!"".equals(basePath)) { // Add webapp paths that can contain classes and .jar files to python search path state.path.insert(0, Py.newString(basePath + "classes")); File jarRoot = new File(basePath + "lib"); if (jarRoot.exists()) { for (String filename : jarRoot.list(new FilenameFilter() { public boolean accept(File dir, String name) { return (name.endsWith(".jar")); } })) { state.path.insert(1, Py.newString(basePath + "lib" + File.separator + filename)); } } } PythonInterpreter interp = new PythonInterpreter(null, state); interp.exec(strScript); PyObject getInstance = interp.get("getInstance"); if (!(getInstance instanceof PyFunction)) { throw new ScriptCompilationException("\"getInstance\" is not a function."); } PyObject _this; if (arguments == null) { _this = ((PyFunction) getInstance).__call__(); } else { PyObject[] args = new PyObject[arguments.length]; for (int i = 0; i < arguments.length; i++) { args[i] = PyJavaType.wrapJavaObject(arguments[i]); } _this = ((PyFunction) getInstance).__call__(args); } return _this.__tojava__(scriptInterfaces[0]); } catch (Exception ex) { logger.error("Error while loading script.", ex); if (ex instanceof IOException) { // Raise to caller throw (IOException) ex; } else if (ex instanceof ScriptCompilationException) { // Raise to caller throw (ScriptCompilationException) ex; } throw new ScriptCompilationException(ex.getMessage()); } } logger.error("No scriptInterfaces provided."); return null; }
From source file:org.romaz.spring.scripting.ext.rhino.RhinoScriptFactory.java
public Object getScriptedObject(ScriptSource actualScriptSource, Class[] interfacesImplementedByScript) throws IOException, ScriptCompilationException { // Fall back on the interfaces passed in via the constructor. if (interfacesImplementedByScript.length == 0) { interfacesImplementedByScript = scriptInterfaces; }//from w ww . j ava 2 s. com synchronized (this.scriptClassMonitor) { if (this.cached == null || actualScriptSource.isModified()) { logger.info("Compiling " + getScriptSourceLocator()); Context ctx = Context.enter(); try { ctx.setLanguageVersion(Context.VERSION_1_7); ctx.setOptimizationLevel(9); Script script = ctx.compileString(actualScriptSource.getScriptAsString(), scriptSourceLocator, 1, null); Object scriptExecutionResult = script.exec(ctx, config.getSharedScope()); this.cached = processScriptResult(ctx, config.getSharedScope(), scriptExecutionResult, interfacesImplementedByScript); } catch (RhinoException e) { throw new ScriptCompilationException("Compilation error: " + e.getMessage(), e); } finally { Context.exit(); } } } return this.cached; }
From source file:org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine.java
public Template createTemplate(ScriptSource scriptSource) { if (scriptSource instanceof GroovyPageCompiledScriptSource) { // handle pre-compiled return createTemplateFromPrecompiled((GroovyPageCompiledScriptSource) scriptSource); }/*from w w w . j a v a2 s . c o m*/ if (scriptSource instanceof ResourceScriptSource) { ResourceScriptSource resourceSource = (ResourceScriptSource) scriptSource; Resource resource = resourceSource.getResource(); return createTemplate(resource, true); } try { return createTemplate(scriptSource.getScriptAsString(), scriptSource.suggestedClassName()); } catch (IOException e) { throw new RuntimeException("IOException in createTemplate", e); } }
From source file:org.red5.server.script.rhino.RhinoScriptFactory.java
/** * Load and parse the Rhino script via RhinoScriptUtils. * * @see RhinoScriptUtils#createRhinoObject(String, Class[]) *///from ww w . j av a2 s.co m public Object getScriptedObject(ScriptSource actualScriptSource, Class[] actualInterfaces) throws IOException, ScriptCompilationException { log.debug("Getting scripted object..."); try { return RhinoScriptUtils.createRhinoObject(actualScriptSource.getScriptAsString(), actualInterfaces, extendedClass); } catch (Exception ex) { throw new ScriptCompilationException("Could not compile Rhino script: " + actualScriptSource, ex); } }
From source file:org.springframework.data.hadoop.scripting.Jsr223ScriptEvaluator.java
@Override public Object evaluate(ScriptSource script, Map<String, Object> arguments) { ScriptEngine engine = discoverEngine(script, arguments); Bindings bindings = (!CollectionUtils.isEmpty(arguments) ? new SimpleBindings(arguments) : null); try {//from w w w.j a va 2s . c o m return (bindings == null ? engine.eval(script.getScriptAsString()) : engine.eval(script.getScriptAsString(), bindings)); } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, "Execution failure", ex); } }
From source file:org.springframework.integration.scripting.jsr223.AbstractScriptExecutor.java
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) { Object result = null;//w w w. j av a 2 s.c om try { if (variables != null) { for (Entry<String, Object> entry : variables.entrySet()) { scriptEngine.put(entry.getKey(), entry.getValue()); } } String script = scriptSource.getScriptAsString(); Date start = new Date(); if (logger.isDebugEnabled()) { logger.debug("executing script: " + script); } result = scriptEngine.eval(script); result = postProcess(result, scriptEngine, script); if (logger.isDebugEnabled()) { logger.debug("script executed in " + (new Date().getTime() - start.getTime()) + " ms"); } } catch (Exception e) { throw new ScriptingException(e.getMessage(), e); } return result; }