Example usage for javax.script Bindings get

List of usage examples for javax.script Bindings get

Introduction

In this page you can find the example usage for javax.script Bindings get.

Prototype

public Object get(Object key);

Source Link

Document

Returns the value to which this map maps the specified key.

Usage

From source file:org.labkey.nashorn.NashornController.java

private Pair<ScriptEngine, ScriptContext> getNashorn(boolean useSession) throws Exception {
    ScriptEngineManager engineManager = new ScriptEngineManager();
    ScriptEngine engine;/*from  w  ww .j av  a 2s.c o  m*/
    ScriptContext context;

    Callable<ScriptEngine> createContext = new Callable<ScriptEngine>() {
        @Override
        @NotNull
        public ScriptEngine call() throws Exception {
            NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
            ScriptEngine engine = factory.getScriptEngine(new String[] { "--global-per-engine" });
            return engine;
        }
    };

    if (useSession) {
        HttpServletRequest req = getViewContext().getRequest();
        engine = SessionHelper.getAttribute(req, this.getClass().getName() + "#scriptEngine", createContext);
    } else {
        engine = createContext.call();
    }

    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    engineScope.put("LABKEY", new org.labkey.nashorn.env.LABKEY(getViewContext()));

    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    // null==engine.getBindings(ScriptContext.GLOBAL_SCOPE), because of --global-per-engine
    // some docs mention enginScope.get("nashorn.global"), but that is also null
    if (null == globalScope)
        globalScope = (Bindings) engineScope.get("nashorn.global");
    if (null == globalScope)
        globalScope = engineScope;
    globalScope.put("console", new Console());

    return new Pair<>(engine, engine.getContext());
}

From source file:org.onesec.raven.ivr.actions.CollectDtmfsActionNode.java

@Override
public Collection<Node> getEffectiveNodes() {
    if (getStatus() != Node.Status.STARTED)
        return null;
    Bindings bindings = new SimpleBindings();
    formExpressionBindings(bindings);//  w  w  w.j a v a  2 s.  c  o  m
    String tempDtmfsKey = getTempDtmfsKey();
    ConversationScenarioState state = getConversationState(bindings);
    List<String> dtmfs = (List<String>) state.getBindings().get(tempDtmfsKey);
    if (dtmfs == null) {
        dtmfs = new LinkedList<String>();
        state.setBinding(tempDtmfsKey, dtmfs, BindingScope.POINT);
        state.setBinding(dtmfsBindingName, dtmfs, bindingScope);
    }
    String dtmf = (String) bindings.get(DTMF_BINDING);
    Integer _maxDtmfsCount = maxDtmfsCount;
    if (stopDtmf.equals(dtmf))
        return processStopAction(state, dtmfs, tempDtmfsKey);
    else {
        String lastTsKey = getLastTsKey();
        long curTime = System.currentTimeMillis();
        if (!(EMPTY_DTMF + "").equals(dtmf)) {
            dtmfs.add(dtmf);
            if (_maxDtmfsCount != null && dtmfs.size() == _maxDtmfsCount)
                return processStopAction(state, dtmfs, tempDtmfsKey);
            else if (autoStopDelay != null)
                state.setBinding(lastTsKey, curTime, BindingScope.POINT);
        } else {
            Long _autoStopDelay = autoStopDelay;
            if (_autoStopDelay != null) {
                Long lastTs = (Long) state.getBindings().get(lastTsKey);
                if (lastTs != null && lastTs + autoStopDelayUnit.toMillis(autoStopDelay) <= curTime)
                    return processStopAction(state, dtmfs, tempDtmfsKey);
            }
        }
        return null;
    }
}

From source file:org.jtotus.network.NordnetConnect.java

public String fetchEncryptedPassword(String encryptJS, String pass, String pubKey, String sessionId) {
    String password = null;//w ww .  j a  va 2 s  .  c om

    StartUpLoader loader = StartUpLoader.getInstance();

    //ScriptEngineManager mgr = loader.getLoadedScriptManager();
    //         Bindings bindings = mgr.getBindings();

    ScriptEngine engine = loader.getLoadedEngine();
    Bindings bindings = engine.getBindings(ScriptContext.GLOBAL_SCOPE);

    try {
        StringBuilder strBuild = new StringBuilder();
        strBuild.append(encryptJS);

        strBuild.append(" \n var keyObj = RSA.getPublicKey(\'" + pubKey + "\');\n"
                + "  var encryptedPass = RSA.encrypt(\'" + pass + "\', keyObj, \'" + sessionId + "\');\n");

        engine.eval(strBuild.toString(), bindings);

        password = (String) bindings.get("encryptedPass");

    } catch (ScriptException ex) {
        Logger.getLogger(NordnetConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

    log.info("JavaScript engine loaded:" + engine.NAME);

    return password;
}

From source file:org.apache.sling.scripting.sightly.impl.engine.UnitLoader.java

/**
 * Create a render unit from the given resource
 *
 * @param scriptResource the resource/*from   w ww.j a v a  2 s .  c o m*/
 * @param bindings       the bindings
 * @param renderContext  the rendering context
 * @return the render unit
 */
public RenderUnit createUnit(Resource scriptResource, Bindings bindings, RenderContextImpl renderContext) {
    Lock lock = null;
    try {
        SourceIdentifier sourceIdentifier = obtainIdentifier(scriptResource);
        Object obj;
        ResourceMetadata resourceMetadata = scriptResource.getResourceMetadata();
        String encoding = resourceMetadata.getCharacterEncoding();
        if (encoding == null) {
            encoding = sightlyEngineConfiguration.getEncoding();
        }
        SlingHttpServletResponse response = (SlingHttpServletResponse) bindings.get(SlingBindings.RESPONSE);
        response.setCharacterEncoding(encoding);
        ResourceResolver adminResolver = renderContext.getScriptResourceResolver();
        if (needsUpdate(sourceIdentifier)) {
            synchronized (activeWrites) {
                String sourceFullPath = sourceIdentifier.getSourceFullPath();
                lock = activeWrites.get(sourceFullPath);
                if (lock == null) {
                    lock = new ReentrantLock();
                    activeWrites.put(sourceFullPath, lock);
                }
                lock.lock();
            }
            Resource javaClassResource = createClass(adminResolver, sourceIdentifier, bindings, encoding,
                    renderContext);
            obj = sightlyJavaCompilerService.compileSource(javaClassResource,
                    sourceIdentifier.getFullyQualifiedName());
        } else {
            obj = sightlyJavaCompilerService.getInstance(adminResolver, null,
                    sourceIdentifier.getFullyQualifiedName());
        }
        if (!(obj instanceof RenderUnit)) {
            throw new SightlyException("Class is not a RenderUnit instance");
        }
        return (RenderUnit) obj;
    } finally {
        if (lock != null) {
            lock.unlock();
        }
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object getTransformationName(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*from w  w w.j a  v a  2 s.  com*/
        Object objTranName = (String) actualObject.get("_TransformationName_");
        return (String) objTranName;
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static RowMetaInterface getOutputRowMeta(ScriptEngine actualContext, Bindings actualObject,
        Object[] ArgList, Object FunctionContext) {
    if (ArgList.length == 0) {
        try {//ww  w. ja v a2 s .c  o m
            Object scmO = actualObject.get("_step_");
            try {
                ScriptValuesMod scm = (ScriptValuesMod) scmO;
                return scm.getOutputRowMeta();
            } catch (Exception e) {
                ScriptValuesModDummy scm = (ScriptValuesModDummy) scmO;
                return scm.getOutputRowMeta();
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to get the output row metadata because of an error: " + Const.CR + e.toString());
        }
    } else {
        throw new RuntimeException("The function call getOutputRowMeta doesn't require arguments.");
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static RowMetaInterface getInputRowMeta(ScriptEngine actualContext, Bindings actualObject,
        Object[] ArgList, Object FunctionContext) {
    if (ArgList.length == 0) {
        try {//from   www  . java  2 s .  c om
            Object scmO = actualObject.get("_step_");
            try {
                ScriptValuesMod scm = (ScriptValuesMod) scmO;
                return scm.getInputRowMeta();
            } catch (Exception e) {
                ScriptValuesModDummy scm = (ScriptValuesModDummy) scmO;
                return scm.getInputRowMeta();
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to get the input row metadata because of an error: " + Const.CR + e.toString());
        }
    } else {
        throw new RuntimeException("The function call getInputRowMeta doesn't require arguments.");
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static double getProcessCount(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    if (ArgList.length == 1) {
        try {/*  ww  w  .ja  v a  2  s . c o  m*/
            Object scmO = actualObject.get("_step_");
            StepInterface scm = (StepInterface) scmO;
            String strType = (String) ArgList[0];

            if (strType.toLowerCase().equals("i"))
                return (double) scm.getLinesInput();
            else if (strType.toLowerCase().equals("o"))
                return (double) scm.getLinesOutput();
            else if (strType.toLowerCase().equals("r"))
                return (double) scm.getLinesRead();
            else if (strType.toLowerCase().equals("u"))
                return (double) scm.getLinesUpdated();
            else if (strType.toLowerCase().equals("w"))
                return (double) scm.getLinesWritten();
            else if (strType.toLowerCase().equals("e"))
                return (double) scm.getLinesRejected();
            else
                return 0;
        } catch (Exception e) {
            //throw new RuntimeException(e.toString());
            return 0;
        }
    } else {
        throw new RuntimeException("The function call getProcessCount requires 1 argument.");
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static void putRow(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 1) {
        try {//from w  ww. j  a  v a  2  s  . co m
            Object[] newRow = (Object[]) ArgList[0];

            Object scmO = actualObject.get("_step_");
            try {
                ScriptValuesMod step = (ScriptValuesMod) scmO;
                step.putRow(step.getOutputRowMeta(), newRow);
            } catch (Exception e) {
                ScriptValuesModDummy step = (ScriptValuesModDummy) scmO;
                step.putRow(step.getOutputRowMeta(), newRow);
            }

        } catch (Exception e) {
            throw new RuntimeException("Unable to pass the new row to the next step(s) because of an error: "
                    + Const.CR + e.toString());
        }
    } else {
        throw new RuntimeException(
                "The function call putRow requires 1 argument : the output row data (Object[])");
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object[] createRowCopy(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 1) {
        try {//from   w  w  w  .jav  a2  s .c o  m
            int newSize = (int) Math.round((Double) ArgList[0]);

            Object scmO = actualObject.get("row");
            Object[] row = (Object[]) scmO;//TODO AKRETION ensure

            return RowDataUtil.createResizedCopy(row, newSize);
        } catch (Exception e) {
            throw new RuntimeException("Unable to create a row copy: " + Const.CR + e.toString());
        }
    } else {
        throw new RuntimeException(
                "The function call createRowCopy requires a single arguments : the new size of the row");
    }
}