Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#isUndefined()
The following examples show how to use
jdk.nashorn.api.scripting.ScriptObjectMirror#isUndefined() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavascriptTextTransformer.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Invokes the UDF with specified data. * * @param data data to pass to the invocable function * @return The data transformed by the UDF in String format */ @Nullable public String invoke(String data) throws ScriptException, IOException, NoSuchMethodException { Invocable invocable = getInvocable(); if (invocable == null) { throw new RuntimeException("No udf was loaded"); } Object result = getInvocable().invokeFunction(functionName(), data); if (result == null || ScriptObjectMirror.isUndefined(result)) { return null; } else if (result instanceof String) { return (String) result; } else { String className = result.getClass().getName(); throw new RuntimeException( "UDF Function did not return a String. Instead got: " + className); } }
Example 2
Source File: JavascriptTextTransformer.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Invokes the UDF with specified data. * * @param data data to pass to the invocable function * @return The data transformed by the UDF in String format */ @Nullable public String invoke(String data) throws ScriptException, IOException, NoSuchMethodException { Invocable invocable = getInvocable(); if (invocable == null) { throw new RuntimeException("No udf was loaded"); } Object result = getInvocable().invokeFunction(functionName(), data); if (result == null || ScriptObjectMirror.isUndefined(result)) { return null; } else if (result instanceof String) { return (String) result; } else { String className = result.getClass().getName(); throw new RuntimeException( "UDF Function did not return a String. Instead got: " + className); } }
Example 3
Source File: DFJsActor.java From dfactor with MIT License | 5 votes |
private boolean _checkFunction(String name){ Object obj = _js.getMember(name); if(obj != null && !ScriptObjectMirror.isUndefined(obj)){ ScriptObjectMirror mir = (ScriptObjectMirror) obj; if(mir.isFunction()){ return true; } } return false; }
Example 4
Source File: DFJsActor.java From dfactor with MIT License | 5 votes |
@Override public void httpCli(Object cfg, Object cb) { try{ ScriptObjectMirror mirCfg = (ScriptObjectMirror) cfg; int port = (Integer)mirCfg.get("port"); String host = (String) mirCfg.get("host"); DFHttpCliReq req = (DFHttpCliReq) mirCfg.get("req"); int connTimeout = mirCfg.containsKey("timeout")?(Integer)(mirCfg.get("timeout")):0; final ScriptObjectMirror mirCb = DFJsUtil.isJsFunction(cb)?(ScriptObjectMirror) cb:null; // req.end(); DFTcpClientCfg cfgCli = DFTcpClientCfg.newCfg(host, port) .setReqData(req); if(connTimeout > 0){ cfgCli.setConnTimeout(connTimeout); } Object objSsl = mirCfg.get("ssl"); if(objSsl != null && !ScriptObjectMirror.isUndefined(objSsl)){ //has ssl config if((Boolean)objSsl){ cfgCli.setSslCfg(DFSSLConfig.newCfg()); } } net.httpCli(cfgCli, mirCb==null?null:new CbHttpClient() { @Override public int onHttpResponse(Object msg, boolean isSucc, String errMsg) { if(mirCb != null){ if(isSucc){ DFHttpCliRsp rsp = (DFHttpCliRsp) msg; mirCb.call(_js, null, rsp); }else{ mirCb.call(_js, errMsg, null); } } return 0; } }); }catch(Throwable e){ e.printStackTrace(); } }
Example 5
Source File: DFActorManagerJs.java From dfactor with MIT License | 5 votes |
protected int createActor(Object template, Object name, Object param, Object initCfg) { HashMap<String,Object> mapParam = new HashMap<>(); mapParam.put("template", template); mapParam.put("fnApi", _jsFnApi); if(param != null && !ScriptObjectMirror.isUndefined(param)){ mapParam.put("param", param); } ActorProp prop = ActorProp.newProp() .classz(DFJsActor.class) .param(mapParam); if(name != null && name instanceof String){ prop.name((String) name); } if(initCfg == null){ //check default cfg ScriptObjectMirror mirFunc = (ScriptObjectMirror) template; ScriptObjectMirror tmpInstance = (ScriptObjectMirror) mirFunc.newObject(); initCfg = tmpInstance.getMember("initCfg"); tmpInstance = null; } if(initCfg != null){ //has cfg try{ if(!ScriptObjectMirror.isUndefined(initCfg)){ ScriptObjectMirror mirCfg = (ScriptObjectMirror) initCfg; Object objTmp = mirCfg.get("block"); if(objTmp != null) prop.blockActor((Boolean)objTmp); objTmp = mirCfg.get("schedule"); if(objTmp != null) prop.scheduleMilli((Integer)objTmp); } }catch(Throwable e){e.printStackTrace();} } return _mgrActor.createActor(prop.getName(), prop.getClassz(), prop.getParam(), (int) (prop.getScheduleMilli()/DFActor.TIMER_UNIT_MILLI), prop.getConsumeType(), prop.isBlock()); }
Example 6
Source File: DFJsActor.java From dfactor with MIT License | 4 votes |
@Override public boolean tcpSvr(Object cfg, Object func) { boolean bRet = false; do { try{ if(cfg == null || !(cfg instanceof ScriptObjectMirror) || ScriptObjectMirror.isUndefined(cfg)){ log.error("invalid cfg for tcpSvr: "+cfg); break; } if(!DFJsUtil.isJsFunction(func)){ log.error("invalid func for tcpSvr: "+func); break; } ScriptObjectMirror cfgWrap = (ScriptObjectMirror) cfg; int port = (Integer)cfgWrap.get("port"); if(port <= 0){ log.error("invalid port for tcpSvr: "+port); break; } if(_mapTcpSvrJsFunc == null) _mapTcpSvrJsFunc = new HashMap<>(); if(_mapTcpSvrJsFunc.containsKey(port)){ log.error("port already inuse: "+port); break; } int workerTh = 0, bossTh = 0; boolean websocket = false; Object obj = cfgWrap.get("worker"); workerTh = obj==null?workerTh:(Integer)obj; obj = cfgWrap.get("boss"); bossTh = obj==null?bossTh:(Integer)obj; obj = cfgWrap.get("ws"); websocket = obj==null?false:true; // log.info("start try tcpSvr, port="+port+", worker="+workerTh+", boss="+bossTh+", isWebsocket="+websocket); DFTcpServerCfg svrCfg = new DFTcpServerCfg(port, workerTh, bossTh) .setTcpProtocol(websocket?DFActorDefine.TCP_DECODE_WEBSOCKET:DFActorDefine.TCP_DECODE_LENGTH); if(websocket){ //检测uri obj = ((ScriptObjectMirror)cfgWrap.get("ws")).get("uri"); if(obj != null){ svrCfg.setWsUri(((String)obj).trim()); } } //check ssl obj = cfgWrap.get("ssl"); if(obj!=null && !ScriptObjectMirror.isUndefined(obj)){ //use ssl DFSSLConfig sslCfg = DFSSLConfig.newCfg() .certPath((String)((ScriptObjectMirror)obj).get("cert")) .pemPath((String)((ScriptObjectMirror)obj).get("key")); svrCfg.setSslConfig(sslCfg); } // net.tcpSvr(svrCfg); _mapTcpSvrJsFunc.put(port, (ScriptObjectMirror) func); //map port<->jsFunc }catch(Throwable e){ e.printStackTrace(); break; } bRet = true; } while (false); return bRet; }
Example 7
Source File: DFJsActor.java From dfactor with MIT License | 4 votes |
@Override public boolean tcpCli(Object cfg, Object func) { boolean bRet = false; do { try{ if(cfg == null || !(cfg instanceof ScriptObjectMirror) || ScriptObjectMirror.isUndefined(cfg)){ log.error("invalid cfg for tcpCli: "+cfg); break; } if(!DFJsUtil.isJsFunction(func)){ log.error("invalid func for tcpCli: "+func); break; } ScriptObjectMirror cfgWrap = (ScriptObjectMirror) cfg; int port = (Integer)cfgWrap.get("port"); if(port <= 0){ log.error("invalid port for tcpCli: "+port); break; } String host = (String) cfgWrap.get("host"); if(host == null){ log.error("invalid host for tcpCli: "+host); break; } host = host.trim(); int reqId = _genTcpCliReqId(); Object obj = cfgWrap.get("ssl"); boolean ssl = obj==null?false:(Boolean)obj; DFTcpClientCfg cfgCli = new DFTcpClientCfg(host, port); obj = cfgWrap.get("ws"); boolean websocket = obj==null?false:true; if(websocket){ cfgCli.setTcpProtocol(DFActorDefine.TCP_DECODE_WEBSOCKET); obj = ((ScriptObjectMirror)obj).get("uri"); if(obj != null){ cfgCli.setWsUri(ssl?"wss":"ws" + "://"+host+":"+port+"/"+obj); }else{ cfgCli.setWsUri(ssl?"wss":"ws" + "://"+host+":"+port); } }else{ cfgCli.setTcpProtocol(DFActorDefine.TCP_DECODE_LENGTH); } if(ssl){ cfgCli.setSslCfg(DFSSLConfig.newCfg()); } obj = cfgWrap.get("timeout"); if(obj != null) cfgCli.setConnTimeout((Integer)obj); net.tcpCli(cfgCli, reqId); if(_mapTcpCliJsFunc == null) _mapTcpCliJsFunc = new HashMap<>(); _mapTcpCliJsFunc.put(reqId, (ScriptObjectMirror)func); log.info("start try tcpCli, "+host+":"+port); }catch(Throwable e){ e.printStackTrace(); break; } bRet = true; } while (false); return bRet; }
Example 8
Source File: DFJsActor.java From dfactor with MIT License | 4 votes |
@Override public void timeout(int delay, Object requestId) { if(requestId == null || ScriptObjectMirror.isUndefined(requestId)) timer.timeout(delay, 0); else timer.timeout(delay, (Integer)requestId); }