Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#get()

The following examples show how to use jdk.nashorn.api.scripting.ScriptObjectMirror#get() . 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: ScriptExecutorImpl.java    From purplejs with Apache License 2.0 6 votes vote down vote up
private Object executeRequire( final ResourcePath script, final ScriptObjectMirror func )
{
    try
    {
        final ScriptObjectMirror exports = this.nashornRuntime.newJsObject();

        final ScriptObjectMirror module = this.nashornRuntime.newJsObject();
        module.put( "id", script.toString() );
        module.put( "exports", exports );

        final ExecutionContextImpl context = new ExecutionContextImpl( this, script );
        final Function<String, Object> requireFunc = context::require;
        final Function<String, ResourcePath> resolveFunc = context::resolve;

        func.call( exports, context, requireFunc, resolveFunc, context.getLogger(), exports, module );
        return module.get( "exports" );
    }
    catch ( final Exception e )
    {
        throw ErrorHelper.INSTANCE.handleError( e );
    }
}
 
Example 2
Source File: DFJsActor.java    From dfactor with MIT License 6 votes vote down vote up
@Override
public int mongoInitPool(Object cfg) {
	try{
		ScriptObjectMirror mirCfg = (ScriptObjectMirror) cfg;
		DFMongoCfg cfgMongo = DFMongoCfg.newCfg()
				.addAddress((String)mirCfg.get("host"),    //mongodb host
						(Integer)mirCfg.get("port")); //mongodb port
		if(mirCfg.containsKey("auth")){
			ScriptObjectMirror mirAuth = (ScriptObjectMirror) mirCfg.get("auth");
			cfgMongo.setCredential((String)mirAuth.get("db"), (String)mirAuth.get("user"), 
					(String)mirAuth.get("pwd"));
		}
		int poolId = mongo.initPool(cfgMongo);
		return poolId;
	}catch(Throwable e){
		e.printStackTrace();
	}
	return 0;
}
 
Example 3
Source File: JavaScriptResponseHandlerImpl.java    From smockin with Apache License 2.0 5 votes vote down vote up
public RestfulResponseDTO executeUserResponse(final Request req, final RestfulMock mock) {
    logger.debug("executeUserResponse called");

    Object engineResponse;

    try {

        engineResponse = executeJS(
                defaultRequestObject
                    + populateRequestObjectWithInbound(req, mock.getPath(), mock.getCreatedBy().getCtxPath())
                    + populateKVPs(req, mock)
                    + keyValuePairFindFunc
                    + defaultResponseObject
                    + userResponseFunctionInvoker
                    + mock.getJavaScriptHandler().getSyntax());

    } catch (ScriptException ex) {

        return new RestfulResponseDTO(500,
                "text/plain",
                "Looks like there is an issue with the Javascript driving this mock " + ex.getMessage());
    }

    if (!(engineResponse instanceof ScriptObjectMirror)) {
        return new RestfulResponseDTO(500,
                "text/plain",
                "Looks like there is an issue with the Javascript driving this mock!");
    }

    final ScriptObjectMirror response = (ScriptObjectMirror) engineResponse;

    return new RestfulResponseDTO(
            (int) response.get("status"),
            (String) response.get("contentType"),
            (String) response.get("body"),
            convertResponseHeaders(response));
}
 
Example 4
Source File: ConversionTestBase.java    From vertx-codetrans with Apache License 2.0 5 votes vote down vote up
public JsonArray unwrapJsonArray(ScriptObjectMirror obj) {
  JsonArray unwrapped = new JsonArray();
  long len = (long) obj.size();
  for (int i = 0;i < len;i++) {
    Object value = obj.get("" + i);
    if (value instanceof ScriptObjectMirror) {
      value = unwrapJsonElement((ScriptObjectMirror) value);
    }
    unwrapped.add(value);
  }
  return unwrapped;
}
 
Example 5
Source File: ConversionTestBase.java    From vertx-codetrans with Apache License 2.0 5 votes vote down vote up
public JsonObject unwrapJsonObject(ScriptObjectMirror obj) {
  JsonObject unwrapped = new JsonObject();
  for (String key : obj.getOwnKeys(true)) {
    Object value = obj.get(key);
    if (value instanceof ScriptObjectMirror) {
      value = unwrapJsonElement((ScriptObjectMirror) value);
    }
    unwrapped.put(key, value);
  }
  return unwrapped;
}
 
Example 6
Source File: JavaScriptResponseHandlerImpl.java    From smockin with Apache License 2.0 5 votes vote down vote up
Set<Map.Entry<String, String>> convertResponseHeaders(final ScriptObjectMirror response) {

        final Object headersJS = response.get("headers");

        final Map<String, String> responseHeaders = new HashMap<>();

        if (headersJS instanceof ScriptObjectMirror) {
            ((ScriptObjectMirror) headersJS)
                    .entrySet()
                    .forEach(e ->
                            responseHeaders.put(e.getKey(), (String)e.getValue()));
        }

        return responseHeaders.entrySet();
    }
 
Example 7
Source File: DFActorManagerJs.java    From dfactor with MIT License 5 votes vote down vote up
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 8
Source File: DFJsActor.java    From dfactor with MIT License 5 votes vote down vote up
@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 9
Source File: Window.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 10
Source File: Window.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 11
Source File: Window.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 12
Source File: Window.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 13
Source File: Window.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 14
Source File: Window.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}
 
Example 15
Source File: DFJsActor.java    From dfactor with MIT License 4 votes vote down vote up
@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 16
Source File: DFJsActor.java    From dfactor with MIT License 4 votes vote down vote up
@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 17
Source File: Window.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) {
    return sobj.get("foo");
}