Example usage for javax.script ScriptEngine FILENAME

List of usage examples for javax.script ScriptEngine FILENAME

Introduction

In this page you can find the example usage for javax.script ScriptEngine FILENAME.

Prototype

String FILENAME

To view the source code for javax.script ScriptEngine FILENAME.

Click Source Link

Document

Reserved key for a named value that is the name of the file being executed.

Usage

From source file:org.apache.nifi.scripting.ScriptFactory.java

private void updateEngine() throws IOException, ScriptException {
    final String extension = getExtension(scriptFileName);
    // if engine is thread safe, it's being reused...if it's a JrubyEngine it
    File scriptFile = new File(this.scriptFileName);
    ScriptEngine scriptEngine = engineFactory.getNewEngine(scriptFile, extension);
    scriptText = getScriptText(scriptFile, extension);
    Map<String, Object> localThreadVariables = new HashMap<>();
    String loggerVariableKey = getVariableName("GLOBAL", "logger", extension);
    localThreadVariables.put(loggerVariableKey, logger);
    String propertiesVariableKey = getVariableName("INSTANCE", "properties", extension);
    localThreadVariables.put(propertiesVariableKey, new HashMap<String, String>());
    localThreadVariables.put(ScriptEngine.FILENAME, scriptFileName);
    if (scriptEngine instanceof Compilable) {
        Bindings bindings = new SimpleBindings(localThreadVariables);
        scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        compiledScript = ((Compilable) scriptEngine).compile(scriptText);
    }//from w ww.ja v  a2s.co m
    logger.debug("Updating Engine!!");
}

From source file:org.apache.sling.scripting.javascript.internal.RhinoJavaScriptEngine.java

public Object eval(Reader scriptReader, ScriptContext scriptContext) throws ScriptException {
    String scriptName = getScriptName(scriptReader);
    Reader reader = wrapReaderIfEspScript(scriptReader, scriptName);
    if (!(scriptReader instanceof ScriptNameAware)) {
        if (NO_SCRIPT_NAME.equals(scriptName)) {
            String script = (String) scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)
                    .get(ScriptEngine.FILENAME);
            if (script != null) {
                for (String extension : getFactory().getExtensions()) {
                    if (script.endsWith(extension)) {
                        scriptName = script;
                        reader = new ScriptNameAwareReader(reader, scriptName);
                        break;
                    }// w w  w  .  j  av  a  2 s . c  om
                }
            }
        }
    }
    return compile(reader).eval(scriptContext);
}

From source file:org.apache.sling.scripting.javascript.internal.RhinoJavaScriptEngine.java

private String getScriptName(ScriptContext scriptContext) {
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    String scriptName = (String) bindings.get(ScriptEngine.FILENAME);
    if (scriptName != null && !"".equals(scriptName)) {
        return scriptName;
    }// w  ww. j  av a2  s.  c  om
    SlingScriptHelper sling = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (sling != null) {
        return sling.getScript().getScriptResource().getPath();
    }
    return NO_SCRIPT_NAME;
}

From source file:org.apache.sling.scripting.sightly.js.impl.JsEnvironment.java

public void runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments,
        UnaryCallback callback) {//from w ww .  j ava 2  s.c o  m
    ScriptContext scriptContext = new SimpleScriptContext();
    CommonJsModule module = new CommonJsModule();
    Bindings scriptBindings = buildBindings(scriptResource, globalBindings, arguments, module);
    scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setAttribute(ScriptEngine.FILENAME, scriptResource.getPath(), ScriptContext.ENGINE_SCOPE);
    runScript(scriptResource, scriptContext, callback, module);
}

From source file:org.freeplane.plugin.script.GenericScript.java

public GenericScript(File scriptFile, ScriptingPermissions permissions) {
    this(new ScriptSource(scriptFile), findScriptEngine(scriptFile), permissions);
    engine.put(ScriptEngine.FILENAME, scriptFile.toString());
    compilationEnabled = !disableScriptCompilation(scriptFile);
    compileTimeStrategy = new CompileTimeStrategy(scriptFile);
}

From source file:org.siphon.common.js.JsEngineUtil.java

/**
 * /*from  w ww.  ja  va  2  s.  co m*/
 * @param jsEngine
 * @param libs [path, path, ...]
 */
public static void initEngine(ScriptEngine jsEngine, Object[] libs) {
    try {
        jsEngine.put("engine", jsEngine);
        NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) jsEngine;
        JsTypeUtil jsTypeUtil = new JsTypeUtil(jsEngine);
        ScriptObjectMirror importedFiles = jsTypeUtil.newObject();
        jsEngine.put("IMPORTED_FILES", importedFiles);

        ScriptObjectMirror stk = jsTypeUtil.newArray();
        jsEngine.put("IMPORTS_PATH_STACK", stk);

        ScriptObjectMirror defaults = (libs.length > 0 && libs[0] != null && libs[0] instanceof String
                && ((String) libs[0]).length() > 0) ? jsTypeUtil.newArray(libs) : jsTypeUtil.newArray();
        jsEngine.put("DEFAULT_IMPORTS_PATHS", defaults);

        jsEngine.put(ScriptEngine.FILENAME, "common/engine.js");
        jsEngine.eval(importsFn);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

From source file:org.siphon.common.js.JsEngineUtil.java

public static Object eval(ScriptEngine jsEngine, String srcFile) throws ScriptException, IOException {
    jsEngine.put(ScriptEngine.FILENAME, srcFile);
    return jsEngine.eval(FileUtils.readFileToString(new File(srcFile)));
}

From source file:org.siphon.common.js.JsEngineUtil.java

public static Object eval(ScriptEngine jsEngine, String srcFile, String code) throws ScriptException {

    jsEngine.put(ScriptEngine.FILENAME, srcFile);
    return jsEngine.eval(code);
}