List of usage examples for jdk.nashorn.internal.runtime ScriptObject containsKey
public boolean containsKey(final Object key)
From source file:org.siphon.common.js.JsTypeUtil.java
License:Open Source License
private static Object jsObjectToJava(ScriptObject object) throws UnsupportedConversionException { if (object.containsKey("_d2js_type")) { String type = object.get("_d2js_type").toString(); Object value = object.get("value"); return jsObjectToJava(type, value); }//from w ww . j a v a 2s . co m if (object instanceof NativeDate) { return jsObjectToJava((NativeDate) object); } if (object instanceof NativeArray) { return jsObjectToJava((NativeArray) object); } if (object instanceof NativeRegExp) { return jsObjectToJava((NativeRegExp) object); } if (object instanceof ScriptFunction) { return jsObjectToJava((ScriptFunction) object); } if (object instanceof NativeString) { return jsObjectToJava((NativeString) object); } Map<String, Object> result = new HashMap<>(); String[] names = object.getOwnKeys(false); for (int i = 0; i < names.length; i++) { String name = names[i]; Object value = object.get(name); result.put(name, jsObjectToJava(value)); } return result; }
From source file:org.siphon.db2js.DbjsRunner.java
License:Open Source License
public void run(HttpServletRequest request, HttpServletResponse response, String method) throws ServletException, IOException { String jsfile = request.getServletContext().getRealPath(request.getServletPath()); if (!new File(jsfile).exists()) { response.setStatus(404);//from w ww .j av a2 s. c o m PrintWriter out = response.getWriter(); out.print(request.getServletPath() + " not found"); out.flush(); return; } JsEngineHandlerContext engineContext = null; try { engineContext = dbjsManager.getEngineContext(jsfile, request.getServletPath(), dataSource, otherArgs); } catch (Exception e3) { logger.error("", e3); engineContext.free(); throw new ServletException(e3); } JsspRequest jsspRequest = new JsspRequest(request, engineContext); ScriptObjectMirror params; try { params = getParams(engineContext, jsspRequest); } catch (Exception e3) { response.setStatus(500); PrintWriter out = response.getWriter(); out.print("params must be json"); out.flush(); engineContext.free(); return; } formatter.writeHttpHeader(response, engineContext); JsspWriter out = null; try { initEngineContext(engineContext, jsspRequest, response); out = (JsspWriter) engineContext.getScriptEngine().get("out"); Object res = run(engineContext, jsspRequest, response, method, params); formatter.formatQueryResult(res, null, engineContext); } catch (Exception e) { try { this.completeTask(engineContext.getScriptEngine(), e); } catch (Exception e2) { logger.error("", e2); } Object ex = JsEngineUtil.parseJsException(e); if (ex instanceof Throwable == false) { boolean ignore = false; if (ex instanceof ScriptObjectMirror) { ScriptObjectMirror mex = (ScriptObjectMirror) ex; if (mex.containsKey("name") && "ValidationError".equals(mex.get("name"))) { ignore = true; } } else if (ex instanceof ScriptObject) { ScriptObject oex = (ScriptObject) ex; if (oex.containsKey("name") && "ValidationError".equals(oex.get("name"))) { ignore = true; } } if (!ignore) logger.error(engineContext.getJson().tryStringify(ex), e); } else { logger.error("", (Throwable) ex); } try { out.print(formatter.formatException(ex, engineContext)); out.flush(); } catch (Exception e1) { logger.error("", e1); } } finally { if (engineContext != null) engineContext.free(); out.flush(); } }
From source file:org.siphon.jsmongo.MongoExecutor.java
License:Open Source License
private BsonValue jsValueToBson(Object arg) throws SqlExecutorException { if (JsTypeUtil.isNull(arg)) { return BsonNull.VALUE; }//from w w w . j a v a 2 s . co m String type = null; Object value = null; if (arg instanceof ScriptObjectMirror) { if (jsTypeUtil.isNativeDate(arg)) return jsSimpleValueToBson(((ScriptObjectMirror) arg).to(NativeDate.class)); ScriptObjectMirror atm = (ScriptObjectMirror) arg; if (atm.isArray()) { return nativeArrayToBson(atm.to(NativeArray.class)); } if (!atm.containsKey("_d2js_type")) { return jsObjectToBsonDocument(atm); } else { type = atm.get("_d2js_type").toString(); value = atm.get("value"); } } else if (arg instanceof ScriptObject) { if (arg instanceof NativeDate) return jsSimpleValueToBson((NativeDate) arg); if (arg instanceof NativeArray) { return nativeArrayToBson((NativeArray) arg); } if (arg instanceof NativeRegExp) { return new BsonRegularExpression((String) NativeRegExp.source((NativeRegExp) arg)); } if (arg instanceof ScriptFunction) { return new BsonString(((ScriptFunction) arg).toSource()); } if (arg instanceof NativeString) { return jsSimpleValueToBson(NativeString.toString(arg)); } ScriptObject obj = (ScriptObject) arg; if (!obj.containsKey("_d2js_type")) { return jsObjectToBsonDocument(obj); } else { type = obj.get("_d2js_type").toString(); value = obj.get("value"); } } else { return jsSimpleValueToBson(arg); } if ("STRING".equals(type)) { return new BsonString(value.toString()); } else if ("DECIMAL".equals(type)) { if (value instanceof Double) { return new BsonDouble((double) value); } else { return new BsonDouble(Double.parseDouble(value + "")); } } else if ("INT".equals(type)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return new BsonDouble(Double.NaN); } else { return new BsonInt32(((Double) value).intValue()); } } else { return new BsonInt32(new Integer(value + "")); } } else if ("BOOLEAN".equals(type)) { return new BsonBoolean(JsTypeUtil.isTrue(arg)); } else if ("DOUBLE".equals(type) || "FLOAT".equals(type)) { if (value instanceof Double) { return new BsonDouble((Double) value); } else { return new BsonDouble(new Double(value + "")); } } else if ("DATE".equals(type)) { long t = parseDate(value); return new BsonDateTime(t); } else if ("OBJECTID".equals(type)) { return new BsonObjectId(new ObjectId((String) value)); } else if ("TIME".equals(type)) { return new BsonDateTime(parseTime(value)); } else if ("BINARY".equals(type)) { return new BsonBinary(parseBinary(value)); } else if ("CLOB".equals(type)) { return new BsonString(value.toString()); } else if ("LONG".equals(type)) { if (value instanceof Double) { if (((Double) value).isNaN()) { return new BsonDouble(Double.NaN); } else { return new BsonInt64(((Double) value).longValue()); } } else { return new BsonInt64(new Long(value + "")); } } else if ("REGEX".equals(type)) { return new BsonRegularExpression(value.toString()); } else { throw new SqlExecutorException("unknown object type " + type + " " + value); } // else if ("OUTCURSOR".equals(type)) { // } else if ("ARRAY".equals(type)) { // } else if ("JSON".equals(type) || "JSONB".equals(type)){ // } else { // } }