Package javax.script

Examples of javax.script.SimpleBindings


    public CompletableFuture<Object> eval(final String script, final Map<String, Object> boundVars) {
        return eval(script, Optional.empty(), new SimpleBindings(boundVars));
    }

    public CompletableFuture<Object> eval(final String script, final Optional<String> language, final Map<String, Object> boundVars) {
        return eval(script, language, new SimpleBindings(boundVars));
    }
View Full Code Here


        // select the gremlin threadpool to execute the script evaluation in
        final AtomicBoolean abort = new AtomicBoolean(false);
        final CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {

            final Bindings bindings = new SimpleBindings();
            bindings.putAll(this.globalBindings);
            bindings.putAll(boundVars);

            try {
                logger.debug("Evaluating script - {} - in thread [{}]", script, Thread.currentThread().getName());

                beforeEval.accept(bindings);
View Full Code Here

                        hasErrors.set(true);
                        return Pair.with(f, Optional.<FileReader>empty());
                    }
                }).filter(p -> p.getValue1().isPresent()).map(p -> Pair.with(p.getValue0(), p.getValue1().get())).forEachOrdered(p -> {
                    try {
                        final Bindings bindings = new SimpleBindings();
                        bindings.putAll(this.globalBindings);

                        // evaluate init scripts with hard reference so as to ensure it doesn't get garbage collected
                        bindings.put(GremlinGroovyScriptEngine.KEY_REFERENCE_TYPE, GremlinGroovyScriptEngine.REFERENCE_TYPE_HARD);

                        se.eval(p.getValue1(), bindings, language);

                        // re-assign graph bindings back to global bindings.  prevent assignment of non-graph
                        // implementations just in case someone tries to overwrite them in the init
                        bindings.entrySet().stream()
                                .filter(kv -> kv.getValue() instanceof Graph)
                                .forEach(kv -> this.globalBindings.put(kv.getKey(), kv.getValue()));

                        logger.info("Initialized {} ScriptEngine with {}", language, p.getValue0());
                    } catch (ScriptException sx) {
View Full Code Here

        }
    }

    @Override
    public Bindings createBindings() {
        return new SimpleBindings();
    }
View Full Code Here

        this.script = script;
    }

    public Object apply(final Object a) {
        try {
            final Bindings bindings = new SimpleBindings();
            bindings.put(A, a);
            return this.engine.eval(this.script, bindings);
        } catch (final ScriptException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
View Full Code Here

        }
    }

    public void accept(final Object a) {
        try {
            final Bindings bindings = new SimpleBindings();
            bindings.put(A, a);
            this.engine.eval(this.script, bindings);
        } catch (final ScriptException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
View Full Code Here

        }
    }

    public void accept(final Object a, final Object b) {
        try {
            final Bindings bindings = new SimpleBindings();
            bindings.put(A, a);
            bindings.put(B, b);
            this.engine.eval(this.script, bindings);
        } catch (final ScriptException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
View Full Code Here

        }
    }

    public void accept(final Object a, final Object b, final Object c) {
        try {
            final Bindings bindings = new SimpleBindings();
            bindings.put(A, a);
            bindings.put(B, b);
            bindings.put(C, c);
            this.engine.eval(this.script, bindings);
        } catch (final ScriptException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
View Full Code Here

        }
    }

    public boolean test(final Object a) {
        try {
            final Bindings bindings = new SimpleBindings();
            bindings.put(A, a);
            return (boolean) this.engine.eval(this.script, bindings);
        } catch (final ScriptException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
View Full Code Here

                                throw new SerializationException(exception);
                            }

                            // Don't pollute the engine namespace with the listener functions
                            ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
                            scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);

                            // Create an invocation handler for this listener
                            AttributeInvocationHandler handler =
                                new AttributeInvocationHandler(scriptEngine,
                                    attribute.localName.substring(attribute.localName.lastIndexOf(".") + 1),
                                    attribute.value);

                            Object listener = Proxy.newProxyInstance(ThreadUtilities.getClassLoader(),
                                new Class<?>[]{propertyClass}, handler);

                            // Add the listener
                            Class<?> listenerListClass = listenerList.getClass();
                            Method addMethod;
                            try {
                                addMethod = listenerListClass.getMethod("add", Object.class);
                            } catch (NoSuchMethodException exception) {
                                throw new RuntimeException(exception);
                            }

                            try {
                                addMethod.invoke(listenerList, listener);
                            } catch (IllegalAccessException exception) {
                                throw new SerializationException(exception);
                            } catch (InvocationTargetException exception) {
                                throw new SerializationException(exception);
                            }
                        } else {
                            // The attribute reprsents a static setter
                            Object value = resolve(attribute.value);

                            Class<?> objectType = element.value.getClass();

                            String propertyName = attribute.localName.substring(attribute.localName.lastIndexOf(".") + 1);
                            propertyName = Character.toUpperCase(propertyName.charAt(0)) +
                            propertyName.substring(1);

                            Method setterMethod = null;
                            if (value != null) {
                                setterMethod = getStaticSetterMethod(propertyClass, propertyName,
                                    objectType, value.getClass());
                            }

                            if (setterMethod == null) {
                                Method getterMethod = getStaticGetterMethod(propertyClass, propertyName, objectType);

                                if (getterMethod != null) {
                                    Class<?> propertyType = getterMethod.getReturnType();
                                    setterMethod = getStaticSetterMethod(propertyClass, propertyName,
                                        objectType, propertyType);

                                    if (value instanceof String) {
                                        value = BeanDictionary.coerce((String)value, propertyType);
                                    }
                                }
                            }

                            if (setterMethod == null) {
                                throw new SerializationException(attribute.localName + " is not valid static property.");
                            }

                            // Invoke the setter
                            try {
                                setterMethod.invoke(null, element.value, value);
                            } catch (Exception exception) {
                                throw new SerializationException(exception);
                            }
                        }
                    }
                }

                // If the parent element is a writable property, set this as its
                // value; it will be applied later in the parent's closing tag
                if (element.parent != null
                    && element.parent.type == Element.Type.WRITABLE_PROPERTY) {
                    element.parent.value = element.value;
                }

                break;
            }

            case READ_ONLY_PROPERTY: {
                if (element.value instanceof Dictionary<?, ?>) {
                    // Process attributes looking for instance property setters
                    for (Attribute attribute : element.attributes) {
                        if (Character.isUpperCase(attribute.localName.charAt(0))) {
                            throw new SerializationException("Static setters are not supported"
                                + " for read-only properties.");
                        }

                        Dictionary<String, Object> dictionary =
                            (Dictionary<String, Object>)element.value;
                        dictionary.put(attribute.localName, resolve(attribute.value));
                    }
                }

                break;
            }

            case WRITABLE_PROPERTY: {
                BeanDictionary beanDictionary = new BeanDictionary(element.parent.value);
                beanDictionary.put(localName, element.value);
                break;
            }

            case SCRIPT: {
                // Process attributes looking for src and language
                String src = null;
                String language = this.language;
                for (Attribute attribute : element.attributes) {
                    if (attribute.localName.equals(SCRIPT_SRC_ATTRIBUTE)) {
                        src = attribute.value;
                    } else if (attribute.localName.equals(SCRIPT_LANGUAGE_ATTRIBUTE)) {
                        language = attribute.value;
                    } else {
                        throw new SerializationException(attribute.localName + " is not a valid"
                            + " attribute for the " + WTKX_PREFIX + ":" + SCRIPT_TAG + " tag.");
                    }
                }

                Bindings bindings;
                if (element.parent.value instanceof ListenerList<?>) {
                    // Don't pollute the engine namespace with the listener functions
                    bindings = new SimpleBindings();
                } else {
                    bindings = scriptEngineManager.getBindings();
                }

                // Execute script
View Full Code Here

TOP

Related Classes of javax.script.SimpleBindings

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.