List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror getMember
@Override
public Object getMember(final String name)
From source file:com.bytelightning.opensource.pokerface.ScriptResponseProducer.java
License:Open Source License
/** * {@inheritDoc}/*from w ww . j ava2 s. c om*/ * This method actually does all the work of this class by invoking the endpoint, and processing it's result. */ @SuppressWarnings("unchecked") @Override public HttpResponse generateResponse() { Object result; ScriptObjectMirror som; // First call the endpoint to obtain a result which will be either an HttpResponse (in which case our work is done), or a ScriptObjectMirror with properties to create an HttpResponse ourselves. try { result = endpoint.callMember("generateResponse", request, context); if (result instanceof HttpResponse) { this.setResponse((HttpResponse) result); return response; } som = (ScriptObjectMirror) result; } catch (Exception ex) { setException(ex); return response; } Object obj; // Interpret the http statusCode int statusCode; obj = som.getMember("statusCode"); if ((obj == null) || ScriptObjectMirror.isUndefined(obj)) statusCode = HttpStatus.SC_OK; else if (obj instanceof Number) statusCode = ((Number) obj).intValue(); else if (obj instanceof ScriptObjectMirror) statusCode = (int) (((ScriptObjectMirror) obj).toNumber() + Double.MIN_VALUE); else statusCode = Integer.parseInt(obj.toString()); // Interpret the http reasonPhrase String reasonPhrase; obj = som.getMember("reasonPhrase"); if ((obj == null) || ScriptObjectMirror.isUndefined(obj)) reasonPhrase = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.US); else reasonPhrase = obj.toString(); // Create a basic response BasicHttpResponse response = new BasicHttpResponse(request.getProtocolVersion(), statusCode, reasonPhrase); // Interpret the headers supplied by the endpoint. obj = som.getMember("headers"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) { List<ScriptObjectMirror> headers; if (obj instanceof List<?>) headers = (List<ScriptObjectMirror>) obj; else { headers = new ArrayList<ScriptObjectMirror>(); if ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror) obj).isArray()) { for (Object sobj : ((ScriptObjectMirror) obj).values()) headers.add((ScriptObjectMirror) sobj); } else Logger.error("The endpoint at " + request.getRequestLine().getUri() + " returned an illegal headers list [class=" + obj.getClass().getName() + "]"); } for (ScriptObjectMirror hdr : headers) { for (String key : hdr.keySet()) { Object value = hdr.getMember(key); response.addHeader(key, ConvertHeaderToString(value)); } } } // Interpret the content type of the response data. ContentType ct = ContentType.DEFAULT_TEXT; if (response.getFirstHeader("Content-Type") != null) ct = ContentType.parse(response.getFirstHeader("content-type").getValue()); obj = som.getMember("mimeType"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) ct = ContentType.create(obj.toString(), ct.getCharset()); obj = som.getMember("charset"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) ct = ContentType.create(ct.getMimeType(), obj.toString()); obj = som.getMember("completion"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) if (((ScriptObjectMirror) obj).isFunction()) completionCallback = (ScriptObjectMirror) obj; // Create an HttpEntity to represent the content. obj = som.getMember("content"); if ((obj != null) && (!ScriptObjectMirror.isUndefined(obj))) { HttpEntity entity; if ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror) obj).isFunction()) entity = new NJavascriptFunctionEntity(request.getRequestLine().getUri(), endpoint, (ScriptObjectMirror) obj, ct, response, context); else { entity = Utils.WrapObjWithHttpEntity(obj, ct); if (entity == null) Logger.error("The endpoint at " + request.getRequestLine().getUri() + " returned an unknown content type [class=" + obj.getClass().getName() + "]"); } if (entity != null) response.setEntity(entity); } else // Normally setting the HttpEntity into the response would set the content-type, but we will have to specify it ourselves in this case. response.setHeader("Content-Type", ct.toString()); // Let our superclass know what the endpoint has provided. this.setResponse(response); String id = (String) context.getAttribute("pokerface.txId"); Logger.info("[client<-endpoint] " + id + " " + response.getStatusLine()); return response; }
From source file:io.vertx.lang.js.VertxGenConverterList.java
License:Open Source License
public VertxGenConverterList(List other) { for (Object entry : other) { if (entry == null) { add(null);/*from www. j a v a 2 s . com*/ } else { if (!(entry instanceof ScriptObjectMirror)) { throw new IllegalArgumentException("Array does not contain objects"); } ScriptObjectMirror mirror = (ScriptObjectMirror) entry; if (mirror.hasMember("_jdel")) { add(mirror.getMember("_jdel")); } else { throw new IllegalArgumentException("Object in array is not @VertxGen object"); } } } }
From source file:io.vertx.lang.js.VertxGenConverterMap.java
License:Open Source License
public VertxGenConverterMap(Map<String, Object> other) { for (Map.Entry<String, Object> entry : other.entrySet()) { if (entry.getValue() == null) { put(entry.getKey(), null);/*from w ww. j a v a2 s . com*/ } else { if (!(entry.getValue() instanceof ScriptObjectMirror)) { throw new IllegalArgumentException("Array does not contain objects"); } ScriptObjectMirror mirror = (ScriptObjectMirror) entry.getValue(); if (mirror.hasMember("_jdel")) { put(entry.getKey(), mirror.getMember("_jdel")); } else { throw new IllegalArgumentException("Object in array is not @VertxGen object"); } } } }
From source file:io.vertx.lang.js.VertxGenConverterSet.java
License:Open Source License
public VertxGenConverterSet(List other) { for (Object entry : other) { if (entry == null) { add(null);/*from w w w. j a v a2s .c o m*/ } else { if (!(entry instanceof ScriptObjectMirror)) { throw new IllegalArgumentException("Array does not contain objects"); } ScriptObjectMirror mirror = (ScriptObjectMirror) entry; if (mirror.hasMember("_jdel")) { add(mirror.getMember("_jdel")); } else { throw new IllegalArgumentException("Object in array is not @VertxGen object"); } } } }
From source file:net.orzo.scripting.JsEngineAdapter.java
License:Apache License
/** *///from w w w.j a v a2s .co m public Object loadModule(String moduleId, SourceCode code) throws ScriptException, IOException { if (!this.modules.containsKey(code.getFullyQualifiedName())) { ScriptContext context = new SimpleScriptContext(); context.setBindings(this.engine.createBindings(), ScriptContext.ENGINE_SCOPE); Bindings engineScope = context.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.put("require", this.require); engineScope.put("doWith", this.scope.get("doWith")); engineScope.put("orzo", this.scope.get("orzo")); engineScope.put("_moduleId", moduleId); SourceCode modEnv = SourceCode.fromResource("net/orzo/modenv.js"); CompiledScript moduleScript; // empty module moduleScript = modEnv.compile((Compilable) this.engine); moduleScript.eval(context); // actual module moduleScript = code.compile((Compilable) this.engine); this.engine.put(ScriptEngine.FILENAME, code.getName()); moduleScript.eval(context); ScriptObjectMirror moduleObj = (ScriptObjectMirror) engineScope.get("module"); this.modules.put(code.getFullyQualifiedName(), moduleObj.getMember("exports")); } return this.modules.get(code.getFullyQualifiedName()); }
From source file:nl.tjonahen.abk.backend.javascript.JavaScriptTest.java
License:Open Source License
@Test public void testParseToJson() throws ScriptException, NoSuchMethodException { final ScriptEngineManager engineManager = new ScriptEngineManager(); final ScriptEngine engine = engineManager.getEngineByName("nashorn"); engine.eval(new InputStreamReader(this.getClass().getResourceAsStream("/parse.js"))); final Invocable invocable = (Invocable) engine; final ScriptObjectMirror obj = (ScriptObjectMirror) invocable.invokeFunction("parseToJson", "\"20141208\",\"Omschrijving\",\"NL01FIRA0001234567\",\"NL01FIRA0009999999\",\"BA\",\"Af\",\"99,99\",\"Betaalautomaat\",\"mededeling\""); assertEquals("20141208", obj.getMember("date")); assertEquals("Omschrijving", obj.getMember("contraAccountName")); assertEquals("NL01FIRA0001234567", obj.getMember("accountNumber")); assertEquals("NL01FIRA0009999999", obj.getMember("contraAccountNumber")); assertEquals("BA", obj.getMember("code")); assertEquals("debit", obj.getMember("debitCreditIndicator")); assertEquals("99,99", obj.getMember("amount")); assertEquals("Betaalautomaat", obj.getMember("mutatiesoort")); assertEquals("mededeling", obj.getMember("description")); }
From source file:org.eclipse.wst.jsdt.internal.esprima.DOMASTConverter.java
License:Open Source License
private void setRange(final ScriptObjectMirror object, final ASTNode node) { Object o = object.getMember("range"); //$NON-NLS-1$ if (ScriptObjectMirror.isUndefined(o)) return;/* w w w. j a v a2 s . c om*/ ScriptObjectMirror range = (ScriptObjectMirror) o; Number x = (Number) range.getSlot(0); Number y = (Number) range.getSlot(1); final int startPosition = x.intValue(); final int length = y.intValue() - x.intValue(); node.setSourceRange(startPosition, length); switch (node.getNodeType()) { case ASTNode.FUNCTION_DECLARATION_STATEMENT: FunctionDeclarationStatement fd = (FunctionDeclarationStatement) node; if (fd.getDeclaration().getJavadoc() == null) fd.getDeclaration().setSourceRange(startPosition, length); else { final int jsdocStart = fd.getDeclaration().getJavadoc().getStartPosition(); fd.getDeclaration().setSourceRange(jsdocStart, length + startPosition - jsdocStart); } break; case ASTNode.FUNCTION_EXPRESSION: FunctionExpression fe = (FunctionExpression) node; if (fe.getMethod().getJavadoc() == null) fe.getMethod().setSourceRange(startPosition, length); else { final int jsdocStart = fe.getMethod().getJavadoc().getStartPosition(); fe.getMethod().setSourceRange(jsdocStart, length + startPosition - jsdocStart); } break; case ASTNode.FUNCTION_DECLARATION: FunctionDeclaration fdec = (FunctionDeclaration) node; if (fdec.getJavadoc() != null) { final int jsdocStart = fdec.getJavadoc().getStartPosition(); fdec.setSourceRange(jsdocStart, length + startPosition - jsdocStart); } break; case ASTNode.VARIABLE_DECLARATION_STATEMENT: VariableDeclarationStatement vds = (VariableDeclarationStatement) node; if (vds.getJavadoc() != null) { final int jsdocStart = vds.getJavadoc().getStartPosition(); vds.setSourceRange(jsdocStart, length + startPosition - jsdocStart); } break; } }
From source file:org.eclipse.wst.jsdt.internal.esprima.DOMASTConverter.java
License:Open Source License
private JSdoc buildJSDoc(ScriptObjectMirror object) { if (!object.hasMember("leadingComments")) //$NON-NLS-1$ return null; Object commentObj = object.getMember("leadingComments"); //$NON-NLS-1$ if (ScriptObjectMirror.isUndefined(commentObj)) return null; ScriptObjectMirror comments = (ScriptObjectMirror) commentObj; Object[] arrayElements = comments.entrySet().toArray(); for (int i = 0; i < arrayElements.length; ++i) { Map.Entry<String, Object> entry = (java.util.Map.Entry<String, Object>) arrayElements[i]; Comment comment = EsprimaParser.createComment((ScriptObjectMirror) entry.getValue(), this.ast); if (comment.isDocComment()) return (JSdoc) comment; }/*from ww w.j av a 2 s . c o m*/ return null; }
From source file:org.eclipse.wst.jsdt.internal.esprima.DOMASTConverter.java
License:Open Source License
private VisitOptions convertLiteral(final ScriptObjectMirror object) { Object value = object.getMember("value"); //$NON-NLS-1$ String raw = (String) object.getMember("raw"); //$NON-NLS-1$ Expression literal = null;//from ww w . j a v a2s .co m if (value instanceof Number) { literal = ast.newNumberLiteral(raw); } else if (value instanceof Boolean) { literal = ast.newBooleanLiteral(raw); } else if (value instanceof String) { literal = ast.newStringLiteral(); ((StringLiteral) literal).setEscapedValue(raw); } else if (object.hasMember("regex")) { //$NON-NLS-1$ literal = ast.newRegularExpressionLiteral(raw); } else if (value == null) { literal = ast.newNullLiteral(); } if (literal == null) { throw new UnimplementedException("Failed to translate Literal " + value); //$NON-NLS-1$ } else { nodes.push(literal); } return VisitOptions.CONTINUE; }
From source file:org.eclipse.wst.jsdt.internal.esprima.DOMASTConverter.java
License:Open Source License
private VisitOptions convertVariableDeclaration(final ScriptObjectMirror object) { String kind = (String) object.getMember("kind"); //$NON-NLS-1$ VariableKind variableKind = VariableKind.VAR; if (kind.equals("let")) //$NON-NLS-1$ variableKind = VariableKind.LET; else if (kind.equals("const")) //$NON-NLS-1$ variableKind = VariableKind.CONST; int parentType = nodes.peek().getNodeType(); if (parentType == FOR_STATEMENT) {// For statements use expression final VariableDeclarationExpression e = ast.newVariableDeclarationExpression(); e.setKind(variableKind);//from w ww . ja v a2s. c o m nodes.push(e); } else { final VariableDeclarationStatement e = ast.newVariableDeclarationStatement(); e.setJavadoc(buildJSDoc(object)); e.setKind(variableKind); nodes.push(e); } return VisitOptions.CONTINUE; }