List of usage examples for javax.script ScriptContext ENGINE_SCOPE
int ENGINE_SCOPE
To view the source code for javax.script ScriptContext ENGINE_SCOPE.
Click Source Link
ScriptEngine
and a set of attributes is maintained for each engine. From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java
/** * Creates the {@code ScriptContext} using a {@link GremlinScriptContext} which avoids a significant amount of * additional object creation on script evaluation. */// w w w . j ava 2 s . co m @Override protected ScriptContext getScriptContext(final Bindings nn) { final GremlinScriptContext ctxt = new GremlinScriptContext(context.getReader(), context.getWriter(), context.getErrorWriter()); final Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE); if (gs != null) ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE); if (nn != null) { ctxt.setBindings(nn, ScriptContext.ENGINE_SCOPE); } else { throw new NullPointerException("Engine scope Bindings may not be null."); } return ctxt; }
From source file:org.apache.camel.builder.script.ScriptBuilder.java
protected ScriptEngine createScriptEngine() { ScriptEngineManager manager = new ScriptEngineManager(); try {//from ww w. ja v a 2s .c o m engine = manager.getEngineByName(scriptEngineName); } catch (NoClassDefFoundError ex) { LOG.error("Cannot load the scriptEngine for " + scriptEngineName + ", the exception is " + ex + ", please ensure correct JARs is provided on classpath."); } if (engine == null) { throw new IllegalArgumentException("No script engine could be created for: " + getScriptEngineName()); } if (isPython()) { ScriptContext context = engine.getContext(); context.setAttribute("com.sun.script.jython.comp.mode", "eval", ScriptContext.ENGINE_SCOPE); } return engine; }
From source file:org.jahia.osgi.FrameworkService.java
private void updateFileReferencesIfNeeded() { File script = new File( SettingsBean.getInstance().getJahiaVarDiskPath() + "/scripts/groovy/updateFileReferences.groovy"); if (!script.isFile()) { return;/*from ww w . ja va 2s . co m*/ } ScriptEngine scriptEngine; try { scriptEngine = ScriptEngineUtils.getInstance() .scriptEngine(FilenameUtils.getExtension(script.getName())); } catch (ScriptException e) { throw new JahiaRuntimeException(e); } if (scriptEngine == null) { throw new IllegalStateException("No script engine available"); } ScriptContext scriptContext = new SimpleScriptContext(); Bindings bindings = new SimpleBindings(); bindings.put("log", logger); bindings.put("logger", logger); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); try (FileInputStream scriptInputStream = new FileInputStream(script); InputStreamReader scriptReader = new InputStreamReader(scriptInputStream, Charsets.UTF_8); StringWriter out = new StringWriter()) { scriptContext.setWriter(out); scriptEngine.eval(scriptReader, scriptContext); } catch (ScriptException | IOException e) { throw new JahiaRuntimeException(e); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java
/** * {@inheritDoc}//from w w w. j a va2s . c om */ @Override public Object eval(final String script, final ScriptContext context) throws ScriptException { try { final String val = (String) context.getAttribute(KEY_REFERENCE_TYPE, ScriptContext.ENGINE_SCOPE); ReferenceBundle bundle = ReferenceBundle.getHardBundle(); if (val != null && val.length() > 0) { if (val.equalsIgnoreCase(REFERENCE_TYPE_SOFT)) { bundle = ReferenceBundle.getSoftBundle(); } else if (val.equalsIgnoreCase(REFERENCE_TYPE_WEAK)) { bundle = ReferenceBundle.getWeakBundle(); } else if (val.equalsIgnoreCase(REFERENCE_TYPE_PHANTOM)) { bundle = ReferenceBundle.getPhantomBundle(); } } globalClosures.setBundle(bundle); } catch (ClassCastException cce) { /*ignore.*/ } try { registerBindingTypes(context); final Class clazz = getScriptClass(script); if (null == clazz) throw new ScriptException("Script class is null"); return eval(clazz, context); } catch (Exception e) { throw new ScriptException(e); } }
From source file:org.apache.camel.builder.script.ScriptBuilder.java
protected void populateBindings(ScriptEngine engine, Exchange exchange) { ScriptContext context = engine.getContext(); int scope = ScriptContext.ENGINE_SCOPE; context.setAttribute("context", exchange.getContext(), scope); context.setAttribute("exchange", exchange, scope); context.setAttribute("request", exchange.getIn(), scope); if (exchange.hasOut()) { context.setAttribute("response", exchange.getOut(), scope); }//from w w w.j a va 2 s. c o m }
From source file:org.siphon.common.js.JsEngineUtil.java
public static Object getGlobal(ScriptEngine engine) { ScriptContext context = engine.getContext(); ScriptObjectMirror binding = (ScriptObjectMirror) context.getBindings(ScriptContext.ENGINE_SCOPE); Field globalField = null;//from w w w .java2 s .c o m try { globalField = ScriptObjectMirror.class.getDeclaredField("global"); globalField.setAccessible(true); Object global = globalField.get(binding); return global; } catch (NoSuchFieldException e) { } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return null; }
From source file:org.nuxeo.ecm.core.io.download.DownloadServiceImpl.java
@Override public boolean checkPermission(DocumentModel doc, String xpath, Blob blob, String reason, Map<String, Serializable> extendedInfos) { List<DownloadPermissionDescriptor> descriptors = registry.getDownloadPermissionDescriptors(); if (descriptors.isEmpty()) { return true; }//from www . j a v a 2 s . c o m xpath = fixXPath(xpath); Map<String, Object> context = new HashMap<>(); Map<String, Serializable> ei = extendedInfos == null ? Collections.emptyMap() : extendedInfos; NuxeoPrincipal currentUser = ClientLoginModule.getCurrentPrincipal(); context.put("Document", doc); context.put("XPath", xpath); context.put("Blob", blob); context.put("Reason", reason); context.put("Infos", ei); context.put("Rendition", ei.get("rendition")); context.put("CurrentUser", currentUser); for (DownloadPermissionDescriptor descriptor : descriptors) { ScriptEngine engine = scriptEngineManager.getEngineByName(descriptor.getScriptLanguage()); if (engine == null) { throw new NuxeoException("Engine not found for language: " + descriptor.getScriptLanguage() + " in permission: " + descriptor.getName()); } if (!(engine instanceof Invocable)) { throw new NuxeoException("Engine " + engine.getClass().getName() + " not Invocable for language: " + descriptor.getScriptLanguage() + " in permission: " + descriptor.getName()); } Object result; try { engine.eval(descriptor.getScript()); engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(context); result = ((Invocable) engine).invokeFunction(RUN_FUNCTION); } catch (NoSuchMethodException e) { throw new NuxeoException("Script does not contain function: " + RUN_FUNCTION + "() in permission: " + descriptor.getName(), e); } catch (ScriptException e) { log.error("Failed to evaluate script: " + descriptor.getName(), e); continue; } if (!(result instanceof Boolean)) { log.error("Failed to get boolean result from permission: " + descriptor.getName() + " (" + result + ")"); continue; } boolean allow = ((Boolean) result).booleanValue(); if (!allow) { return false; } } return true; }
From source file:org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.DataFactoryScriptingSupport.java
public void initialize(final DataFactory dataFactory, final DataFactoryContext dataFactoryContext) throws ReportDataFactoryException { if (globalScriptContext != null) { return;/*from ww w . j av a 2 s. c o m*/ } this.dataFactory = dataFactory; this.resourceManager = dataFactoryContext.getResourceManager(); this.contextKey = dataFactoryContext.getContextKey(); this.configuration = dataFactoryContext.getConfiguration(); this.resourceBundleFactory = dataFactoryContext.getResourceBundleFactory(); this.dataFactoryContext = dataFactoryContext; globalScriptContext = new SimpleScriptContext(); globalScriptContext.setAttribute("dataFactory", dataFactory, ScriptContext.ENGINE_SCOPE); globalScriptContext.setAttribute("configuration", configuration, ScriptContext.ENGINE_SCOPE); globalScriptContext.setAttribute("resourceManager", resourceManager, ScriptContext.ENGINE_SCOPE); globalScriptContext.setAttribute("contextKey", contextKey, ScriptContext.ENGINE_SCOPE); globalScriptContext.setAttribute("resourceBundleFactory", resourceBundleFactory, ScriptContext.ENGINE_SCOPE); if (StringUtils.isEmpty(globalScriptLanguage)) { return; } globalScriptContext.setAttribute("scriptHelper", new ScriptHelper(globalScriptContext, globalScriptLanguage, resourceManager, contextKey), ScriptContext.ENGINE_SCOPE); final ScriptEngine maybeInvocableEngine = new ScriptEngineManager().getEngineByName(globalScriptLanguage); if (maybeInvocableEngine == null) { throw new ReportDataFactoryException(String.format( "DataFactoryScriptingSupport: Failed to locate scripting engine for language '%s'.", globalScriptLanguage)); } if (maybeInvocableEngine instanceof Invocable == false) { return; } this.globalScriptEngine = (Invocable) maybeInvocableEngine; maybeInvocableEngine.setContext(globalScriptContext); try { maybeInvocableEngine.eval(globalScript); } catch (ScriptException e) { throw new ReportDataFactoryException( "DataFactoryScriptingSupport: Failed to execute datafactory init script.", e); } }
From source file:org.jahia.services.workflow.jbpm.custom.email.JBPMMailProducer.java
protected String evaluateExpression(WorkItem workItem, String scriptToExecute, JCRSessionWrapper session) throws RepositoryException, ScriptException { ScriptContext scriptContext = new SimpleScriptContext(); if (bindings == null) { bindings = getBindings(workItem, session); }// w w w .ja va 2s .com scriptContext.setWriter(new StringWriter()); scriptContext.setErrorWriter(new StringWriter()); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); scriptContext.setBindings(scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); scriptEngine.eval(scriptToExecute, scriptContext); String error = scriptContext.getErrorWriter().toString(); if (error.length() > 0) { logger.error("Scripting error : " + error); } return scriptContext.getWriter().toString().trim(); }
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// w w w .j a va 2 s. com * @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); } }