Example usage for javax.script ScriptContext getAttribute

List of usage examples for javax.script ScriptContext getAttribute

Introduction

In this page you can find the example usage for javax.script ScriptContext getAttribute.

Prototype

public Object getAttribute(String name, int scope);

Source Link

Document

Gets the value of an attribute in a given scope.

Usage

From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java

/**
 * {@inheritDoc}/*from w  w w .  ja  v  a  2 s  . c o m*/
 */
@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.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java

Object eval(final Class scriptClass, final ScriptContext context) throws ScriptException {
    final Binding binding = new Binding(context.getBindings(ScriptContext.ENGINE_SCOPE)) {
        @Override//from  w ww.j  a  va  2 s.  c  o m
        public Object getVariable(String name) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope != -1) {
                    return context.getAttribute(name, scope);
                }
                // Redirect script output to context writer, if out var is not already provided
                if ("out".equals(name)) {
                    final Writer writer = context.getWriter();
                    if (writer != null) {
                        return (writer instanceof PrintWriter) ? (PrintWriter) writer
                                : new PrintWriter(writer, true);
                    }
                }
                // Provide access to engine context, if context var is not already provided
                if ("context".equals(name)) {
                    return context;
                }
            }
            throw new MissingPropertyException(name, getClass());
        }

        @Override
        public void setVariable(String name, Object value) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope == -1) {
                    scope = ScriptContext.ENGINE_SCOPE;
                }
                context.setAttribute(name, value, scope);
            }
        }
    };

    try {
        // if this class is not an instance of Script, it's a full-blown class then simply return that class
        if (!Script.class.isAssignableFrom(scriptClass)) {
            return scriptClass;
        } else {
            final Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
            for (Method m : scriptClass.getMethods()) {
                final String name = m.getName();
                globalClosures.put(name, new MethodClosure(scriptObject, name));
            }

            final MetaClass oldMetaClass = scriptObject.getMetaClass();
            scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
                @Override
                public Object invokeMethod(final Object object, final String name, final Object args) {
                    if (args == null) {
                        return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                    } else if (args instanceof Tuple) {
                        return invokeMethod(object, name, ((Tuple) args).toArray());
                    } else if (args instanceof Object[]) {
                        return invokeMethod(object, name, (Object[]) args);
                    } else {
                        return invokeMethod(object, name, new Object[] { args });
                    }
                }

                @Override
                public Object invokeMethod(final Object object, final String name, final Object args[]) {
                    try {
                        return super.invokeMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }

                @Override
                public Object invokeStaticMethod(final Object object, final String name, final Object args[]) {
                    try {
                        return super.invokeStaticMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }
            });

            final Object o = scriptObject.run();

            // if interpreter mode is enable then local vars of the script are promoted to engine scope bindings.
            if (interpreterModeEnabled) {
                final Map<String, Object> localVars = (Map<String, Object>) context
                        .getAttribute(COLLECTED_BOUND_VARS_MAP_VARNAME);
                if (localVars != null) {
                    localVars.entrySet().forEach(e -> {
                        // closures need to be cached for later use
                        if (e.getValue() instanceof Closure)
                            globalClosures.put(e.getKey(), (Closure) e.getValue());

                        context.setAttribute(e.getKey(), e.getValue(), ScriptContext.ENGINE_SCOPE);
                    });

                    // get rid of the temporary collected vars
                    context.removeAttribute(COLLECTED_BOUND_VARS_MAP_VARNAME, ScriptContext.ENGINE_SCOPE);
                    localVars.clear();
                }
            }

            return o;
        }
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}