List of usage examples for javax.script ScriptEngine eval
public Object eval(Reader reader) throws ScriptException;
eval(String)
except that the source of the script is provided as a Reader
From source file:org.archive.modules.deciderules.ScriptedDecideRule.java
/** * Create a new ScriptEngine instance, preloaded with any supplied * source file and the variables 'self' (this ScriptedDecideRule) * and 'context' (the ApplicationContext). * /* www . j a v a 2 s. c o m*/ * @return the new Interpreter instance */ protected ScriptEngine newEngine() { ScriptEngine interpreter = new ScriptEngineManager().getEngineByName(engineName); interpreter.put("self", this); interpreter.put("context", appCtx); Reader reader = null; try { reader = getScriptSource().obtainReader(); interpreter.eval(reader); } catch (ScriptException e) { logger.log(Level.SEVERE, "script problem", e); } finally { IOUtils.closeQuietly(reader); } return interpreter; }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java
@Test public void testGroovyFileAndParam() throws ScriptException, IOException { String engineName = "groovy"; ScriptEngineManager mgr = new ScriptEngineManager(); // create a JavaScript engine ScriptEngine engine = mgr.getEngineByName(engineName); assertNotNull("Can't find engine '" + engineName + "'", engine); File script = new ClassPathResource("test-data/test.groovy").getFile(); assertNotNull("Can't find test script", script); engine.put("gbtest", "testok"); // evaluate code from File engine.eval(new FileReader(script)); }
From source file:org.archive.modules.ScriptedProcessor.java
@Override protected void innerProcess(CrawlURI curi) { // depending on previous configuration, engine may // be local to this thread or shared ScriptEngine engine = getEngine(); synchronized (engine) { // synchronization is harmless for local thread engine, // necessary for shared engine engine.put("curi", curi); engine.put("appCtx", appCtx); try {//from w w w . j ava 2s . c om engine.eval("process(curi)"); } catch (ScriptException e) { logger.log(Level.WARNING, e.getMessage(), e); } finally { engine.put("curi", null); engine.put("appCtx", null); } } }
From source file:org.nuxeo.runtime.scripting.ScriptingComponent.java
@Override public Object eval(String path) throws ScriptException { ScriptEngine engine = getEngineByFileName(path); if (engine != null) { try {//from w w w . ja v a 2s . c o m Reader reader = new FileReader(getScriptFile(path)); try { return engine.eval(reader); } finally { reader.close(); } } catch (IOException e) { throw new ScriptException(e); } } else { throw new ScriptException("No script engine was found for the file: " + path); } }
From source file:com.seajas.search.profiler.validator.ModifierValidator.java
/** * Validate the given command object.//from w ww.j a v a2s . c om * * @param command * @param errors */ @Override public void validate(final Object command, final Errors errors) { ModifierCommand modifier = (ModifierCommand) command; if (modifier.getAction() != null && (modifier.getAction().equals("add") || modifier.getAction().equals("edit"))) { if (StringUtils.isEmpty(modifier.getUrlExpression())) errors.rejectValue("urlExpression", "modifiers.validator.error.url.expression.empty"); else try { Pattern.compile(modifier.getUrlExpression()); } catch (PatternSyntaxException e) { errors.rejectValue("urlExpression", "modifiers.validator.error.url.expression.invalid"); } if (StringUtils.isEmpty(modifier.getUrlType())) errors.rejectValue("urlType", "modifiers.validator.error.url.type.empty"); } else if (modifier.getFilterAction() != null && (modifier.getFilterAction().equals("add-filter") || modifier.getFilterAction().equals("edit-filter"))) { if (StringUtils.isEmpty(modifier.getFilterStart())) errors.rejectValue("filterStart", "modifiers.validator.error.filter.start.empty"); else if (modifier.getFilterAsExpression()) try { Pattern.compile(modifier.getFilterStart()); } catch (PatternSyntaxException e) { errors.rejectValue("filterStart", "modifiers.validator.error.filter.start.invalid"); } if (StringUtils.isEmpty(modifier.getFilterEnd())) errors.rejectValue("filterEnd", "modifiers.validator.error.filter.end.empty"); else if (modifier.getFilterAsExpression()) try { Pattern.compile(modifier.getFilterEnd()); } catch (PatternSyntaxException e) { errors.rejectValue("filterEnd", "modifiers.validator.error.filter.end.invalid"); } } else if (modifier.getScriptAction() != null && (modifier.getScriptAction().equals("add-script") || modifier.getScriptAction().equals("edit-script"))) { if (StringUtils.isEmpty(modifier.getScriptLanguage())) errors.rejectValue("scriptLanguage", "modifiers.validator.error.script.language.invalid"); if (StringUtils.isEmpty(modifier.getScriptContent())) errors.rejectValue("scriptContent", "modifiers.validator.error.script.content.empty"); else { if (modifier.getScriptLanguage().equalsIgnoreCase("xslt")) { StreamSource source = new StreamSource(new StringReader(modifier.getScriptContent())); try { TransformerFactory.newInstance().newTransformer(source); } catch (Exception e) { errors.rejectValue("scriptContent", "modifiers.validator.error.script.content.invalid.transformation"); } } else { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByName(modifier.getScriptLanguage().toLowerCase()); engine.put("input", ""); engine.put("inputUrl", ""); engine.put("inputMaximumContentLength", 0L); try { engine.eval(modifier.getScriptContent()); } catch (ScriptException e) { errors.rejectValue("scriptContent", "modifiers.validator.error.script.content.syntax.error"); logger.warn("Script validation failed", e); } } } } }
From source file:org.nuxeo.automation.scripting.test.TestCompileAndContext.java
@Test public void serviceShouldBeDeclared() throws Exception { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByName(AutomationScriptingConstants.NASHORN_ENGINE); assertNotNull(engine);/*from www . j ava 2 s . c om*/ InputStream stream = this.getClass().getResourceAsStream("/checkWrapper.js"); assertNotNull(stream); engine.eval(scriptingService.getJSWrapper()); engine.eval(IOUtils.toString(stream)); assertEquals("Hello" + System.lineSeparator(), outContent.toString()); }
From source file:org.archive.modules.deciderules.ScriptedDecideRule.java
@Override public DecideResult innerDecide(CrawlURI uri) { // depending on previous configuration, engine may // be local to this thread or shared ScriptEngine engine = getEngine(); synchronized (engine) { // synchronization is harmless for local thread engine, // necessary for shared engine try {//from w ww . j a v a 2s.co m engine.put("object", uri); engine.put("appCtx", appCtx); return (DecideResult) engine.eval("decisionFor(object)"); } catch (ScriptException e) { logger.log(Level.WARNING, e.getMessage(), e); return DecideResult.NONE; } finally { engine.put("object", null); engine.put("appCtx", null); } } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTest.java
@Test public void shouldProcessScriptWithUTF8Characters() throws Exception { final ScriptEngine engine = new GremlinGroovyScriptEngine(); assertEquals("", engine.eval("''")); }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineOverGraphTest.java
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldProcessUTF8Query() throws Exception { final Vertex nonUtf8 = graph.addVertex("name", "marko", "age", 29); final Vertex utf8Name = graph.addVertex("name", "", "age", 32); final ScriptEngine engine = new GremlinGroovyScriptEngine(); engine.put("g", g); Traversal eval = (Traversal) engine.eval("g.V().has('name', 'marko')"); assertEquals(nonUtf8, eval.next());//from ww w .j av a 2 s . com eval = (Traversal) engine.eval("g.V().has('name','')"); assertEquals(utf8Name, eval.next()); }
From source file:org.rhq.scripting.python.PythonScriptEngineInitializer.java
@Override public ScriptEngine instantiate(Set<String> packages, PermissionCollection permissions) throws ScriptException { ScriptEngine eng = engineManager.getEngineByName("python"); //XXX this might not work perfectly in jython //but we can't make it work perfectly either, so let's just //keep our fingers crossed.. //http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#from-import-statements for (String pkg : packages) { try {/* w ww . j a v a2s. c o m*/ eng.eval("from " + pkg + " import *\n"); } catch (ScriptException e) { //well, let's just keep things going, this is not fatal... LOG.info("Python script engine could not pre-import members of package '" + pkg + "'."); } } //fingers crossed we can secure jython like this return permissions == null ? eng : new SandboxedScriptEngine(eng, permissions); }