List of usage examples for javax.script ScriptException getCause
public synchronized Throwable getCause()
From source file:org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper.java
public static Object eval(Message message, ComponentContext context, ModelAttribute attribute, Object value, ModelEntity entity, EntityData data, String expression) { ScriptEngine engine = scriptEngine.get(); if (engine == null) { ScriptEngineManager factory = new ScriptEngineManager(); engine = factory.getEngineByName("groovy"); scriptEngine.set(engine);//w w w . j av a 2 s .c om } engine.put("value", value); engine.put("data", data); engine.put("entity", entity); engine.put("attribute", attribute); engine.put("message", message); engine.put("context", context); try { String importString = "import org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper;\n"; String code = String.format( "return new ModelAttributeScriptHelper(message, context, attribute, entity, data, value) { public Object eval() { return %s } }.eval()", expression); return engine.eval(importString + code); } catch (ScriptException e) { throw new RuntimeException("Unable to evaluate groovy script. Attribute ==> " + attribute.getName() + ". Value ==> " + value.toString() + "." + e.getCause().getMessage(), e); } }
From source file:org.wso2.carbon.business.rules.core.util.TemplateManagerHelper.java
/** * Runs the script that is given as a string, and gives all the variables specified in the script * * @param script//from w w w .j a v a 2 s . c om * @return Map of Strings * @throws TemplateManagerHelperException */ public static Map<String, String> getScriptGeneratedVariables(String script) throws RuleTemplateScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); ScriptContext scriptContext = new SimpleScriptContext(); scriptContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); try { // Run script engine.eval(script); Map<String, Object> returnedScriptContextBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); // Variable names and their values as strings, from the script context binding Map<String, String> scriptVariables = new HashMap<>(); for (Map.Entry scriptVariable : returnedScriptContextBindings.entrySet()) { if (scriptVariable.getValue() == null) { scriptVariables.put(scriptVariable.getKey().toString(), null); } else { scriptVariables.put(scriptVariable.getKey().toString(), scriptVariable.getValue().toString()); } } return scriptVariables; } catch (ScriptException e) { throw new RuleTemplateScriptException(e.getCause().getMessage(), e); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineCompileStaticTest.java
@Test public void shouldCompileStaticWithExtension() throws Exception { // with no type checking extension this should pass final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider(); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) { assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red")); }//from w ww. java 2 s . c o m final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider( PrecompiledExtensions.PreventColorUsageExtension.class.getName()); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTypeCheckedTest.java
@Test public void shouldTypeCheckWithExtension() throws Exception { // with no type checking extension this should pass final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider(); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) { assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red")); }//from w w w . j av a 2s. c om final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider( PrecompiledExtensions.PreventColorUsageExtension.class.getName()); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineCompileStaticTest.java
@Test public void shouldCompileStaticWithMultipleExtension() throws Exception { // with no type checking extension this should pass final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider(); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) { assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red")); assertEquals(1l, scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count")); }/* w w w. j a v a 2 s . co m*/ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider( PrecompiledExtensions.PreventColorUsageExtension.class.getName() + "," + PrecompiledExtensions.PreventCountDownLatchUsageExtension.class.getName()); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTypeCheckedTest.java
@Test public void shouldTypeCheckWithMultipleExtension() throws Exception { // with no type checking extension this should pass final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider(); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) { assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red")); assertEquals(1l, scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count")); }/*from www . ja va2 s. co m*/ final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider( PrecompiledExtensions.PreventColorUsageExtension.class.getName() + "," + PrecompiledExtensions.PreventCountDownLatchUsageExtension.class.getName()); try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) { scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count"); fail("Should have failed type checking"); } catch (ScriptException se) { assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass()); assertThat(se.getMessage(), containsString("Method call is not allowed!")); } }
From source file:com.hortonworks.streamline.streams.runtime.processor.RuleProcessorRuntime.java
private GroovyScript<Boolean> createHelperGroovyScript(GroovyExpression groovyExpression, GroovyScriptEngine groovyScriptEngine) { return new GroovyScript<Boolean>(groovyExpression.asString(), groovyScriptEngine) { @Override/*from w w w .jav a 2 s. c om*/ public Boolean evaluate(StreamlineEvent event) throws ScriptException { Boolean evaluates = false; try { evaluates = super.evaluate(event); } catch (ScriptException e) { if (e.getCause() != null && e.getCause() instanceof groovy.lang.MissingPropertyException) { // Occurs when not all the properties required for evaluating the script are set. This can happen for example // when receiving an StreamlineEvent that does not have all the fields required to evaluate the expression LOG.debug("Missing property required to evaluate expression. {}", e.getCause().getMessage()); LOG.trace("", e); evaluates = false; } else { throw e; } } return evaluates; } }; }
From source file:org.jumpmind.metl.core.runtime.component.Transformer.java
@Override public void handle(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) { if (scriptEngine == null) { ScriptEngineManager factory = new ScriptEngineManager(); scriptEngine = factory.getEngineByName("groovy"); }//from ww w. j a v a2 s. c o m totalTime = 0; if (inputMessage instanceof EntityDataMessage) { Model inputModel = getComponent().getInputModel(); List<EntityData> inDatas = ((EntityDataMessage) inputMessage).getPayload(); ArrayList<EntityData> outDatas = new ArrayList<EntityData>(inDatas != null ? inDatas.size() : 0); if (inDatas != null) { for (EntityData inData : inDatas) { EntityData outData = new EntityData(); outData.setChangeType(inData.getChangeType()); outDatas.add(outData); Set<String> attributeIds = new HashSet<String>(); Set<ModelEntity> processedEntities = new HashSet<ModelEntity>(); for (String attributeId : inData.keySet()) { ModelAttribute attribute = inputModel.getAttributeById(attributeId); if (attribute != null) { ModelEntity entity = inputModel.getEntityById(attribute.getEntityId()); if (entity != null && !processedEntities.contains(entity)) { List<ModelAttribute> attributes = entity.getModelAttributes(); for (ModelAttribute modelAttribute : attributes) { attributeIds.add(modelAttribute.getId()); } processedEntities.add(entity); } } } for (String attributeId : attributeIds) { String transform = transformsByAttributeId.get(attributeId); Object value = inData.get(attributeId); if (isNotBlank(transform)) { ModelAttribute attribute = inputModel.getAttributeById(attributeId); ModelEntity entity = inputModel.getEntityById(attribute.getEntityId()); ModelAttributeScriptHelper helper = helpers.get(attribute.getId()); if (helper == null) { long ts = System.currentTimeMillis(); scriptEngine.put("entity", entity); scriptEngine.put("attribute", attribute); scriptEngine.put("context", context); try { String importString = "import org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper;\n"; String code = String.format( "return new ModelAttributeScriptHelper(context, attribute, entity) { public Object eval() { return %s } }", transform); helper = (ModelAttributeScriptHelper) scriptEngine.eval(importString + code); helpers.put(attribute.getId(), helper); } catch (ScriptException e) { throw new RuntimeException("Unable to evaluate groovy script. Attribute ==> " + attribute.getName() + ". Value ==> " + value.toString() + "." + e.getCause().getMessage(), e); } log.debug("It took " + (System.currentTimeMillis() - ts) + "ms to create class"); } helper.setData(inData); helper.setValue(value); helper.setMessage(inputMessage); long ts = System.currentTimeMillis(); value = helper.eval(); totalTime += (System.currentTimeMillis() - ts); totalCalls++; } if (value != ModelAttributeScriptHelper.REMOVE_ATTRIBUTE) { outData.put(attributeId, value); } } getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber); } } callback.sendEntityDataMessage(null, outDatas); if (totalCalls > 0) { log.debug("It took " + (totalTime / totalCalls) + "ms on average to call eval"); } } else if (inputMessage instanceof ControlMessage) { callback.sendControlMessage(); } }
From source file:com.github.safrain.remotegsh.server.RgshFilter.java
private void performRunScript(HttpServletRequest request, HttpServletResponse response) throws IOException { String script = toString(request.getInputStream(), charset); try {/*w ww. j a v a 2 s . co m*/ ScriptEngine engine = prepareEngine(request, response); engine.eval(script); response.setStatus(200); // Post and run won't return evaluate result to client } catch (ScriptException e) { log.log(Level.SEVERE, "Error while running script:" + script, e); response.setStatus(500); e.getCause().printStackTrace(response.getWriter()); } }
From source file:org.graphwalker.machines.ExtendedFiniteStateMachine.java
public void eval(String script) { if (jsEngine != null) { try {// w w w . ja v a 2s .c o m jsEngine.eval(script); } catch (ScriptException e) { logger.error("Problem when running: '" + script + "' in BeanShell"); logger.error("EvalError: " + e); logger.error(e.getCause()); throw new RuntimeException("Execution of startup script generated an error.", e); } } else if (beanShellEngine != null) { try { beanShellEngine.eval(script); } catch (EvalError e) { logger.error("Problem when running: '" + script + "' in BeanShell"); logger.error("EvalError: " + e); logger.error(e.getCause()); throw new RuntimeException("Execution of startup script generated an error.", e); } } }