Example usage for jdk.nashorn.internal.runtime JSType nullOrUndefined

List of usage examples for jdk.nashorn.internal.runtime JSType nullOrUndefined

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime JSType nullOrUndefined.

Prototype

public static boolean nullOrUndefined(final Object obj) 

Source Link

Document

Check if an object is null or undefined

Usage

From source file:com.eas.client.queries.ScriptedQuery.java

@Override
public JSObject execute(Scripts.Space aSpace, Consumer<JSObject> onSuccess, Consumer<Exception> onFailure)
        throws Exception {
    assert Scripts.getSpace() == aSpace : "Scripts.Space TLS assumption failed";
    if (onSuccess != null) {
        ScriptedResource._require(new String[] { entityName }, null, aSpace, new HashSet<>(), (Void v) -> {
            JSObject source = aSpace.createModule(entityName);
            if (source.hasMember("fetch")) {
                Object oFetch = source.getMember("fetch");
                if (oFetch instanceof JSObject) {
                    JSObject jsFetch = (JSObject) oFetch;
                    if (jsFetch.isFunction()) {
                        JSObject jsParams = aSpace.makeObj();
                        for (int i = 0; i < params.getParametersCount(); i++) {
                            Parameter p = params.get(i + 1);
                            jsParams.setMember(p.getName(), aSpace.toJs(p.getValue()));
                        }//from w ww .j  a  va 2 s.c o m
                        final ExecutionChecker exChecker = new ExecutionChecker();
                        Object oRowset = jsFetch.call(source,
                                aSpace.toJs(new Object[] { jsParams, new AbstractJSObject() {

                                    @Override
                                    public Object call(final Object thiz, final Object... args) {
                                        if (exChecker.isExecutionNeeded()) {
                                            try {
                                                JSObject jsRowset = args.length > 0
                                                        ? (JSObject) aSpace.toJava(args[0])
                                                        : null;
                                                try {
                                                    onSuccess.accept(jsRowset);
                                                } catch (Exception ex) {
                                                    Logger.getLogger(ScriptedQuery.class.getName())
                                                            .log(Level.SEVERE, null, ex);
                                                }
                                            } catch (Exception ex) {
                                                if (onFailure != null) {
                                                    onFailure.accept(ex);
                                                }
                                            }
                                        }
                                        return null;
                                    }
                                }, new AbstractJSObject() {

                                    @Override
                                    public Object call(final Object thiz, final Object... args) {
                                        if (exChecker.isExecutionNeeded()) {
                                            if (onFailure != null) {
                                                if (args.length > 0) {
                                                    if (args[0] instanceof Exception) {
                                                        onFailure.accept((Exception) args[0]);
                                                    } else {
                                                        onFailure.accept(new Exception(
                                                                String.valueOf(aSpace.toJava(args[0]))));
                                                    }
                                                } else {
                                                    onFailure.accept(new Exception(
                                                            "No error information from fetch method"));
                                                }
                                            }
                                        }
                                        return null;
                                    }
                                } }));
                        if (!JSType.nullOrUndefined(oRowset)) {
                            onSuccess.accept((JSObject) aSpace.toJava(oRowset));
                            exChecker.setExecutionNeeded(false);
                        }
                    }
                }
            }
        }, onFailure);
        return null;
    } else {
        JSObject source = aSpace.createModule(entityName);
        if (source.hasMember("fetch")) {
            Object oFetch = source.getMember("fetch");
            if (oFetch instanceof JSObject) {
                JSObject jsFetch = (JSObject) oFetch;
                if (jsFetch.isFunction()) {
                    JSObject jsParams = aSpace.makeObj();
                    Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams }));
                    if (!JSType.nullOrUndefined(oRowset)) {
                        return (JSObject) aSpace.toJava(oRowset);
                    }
                }
            }
        }
        return null;
    }
}

From source file:com.eas.server.scripts.ModulesJSFacade.java

@Override
public void setMember(String name, Object value) {
    value = value instanceof ScriptObject ? ScriptUtils.wrap((ScriptObject) value) : value;
    if (value instanceof JSObject) {
        if (session.getId() == null) {
            throw new IllegalStateException(RESIDENT_MODULES_MODIFICATION_MSG);
        } else {/* ww w  . j  a v a2s.  c  o m*/
            session.registerModule(name, (JSObject) value);
        }
    } else if (JSType.nullOrUndefined(value)) {
        session.unregisterModule(name);
    }
}