Ejemplo n.º 1
0
  /*
  	If we want to support multiple commands in the command path we need to
  	change this to not throw the exception.
  */
  private BshMethod loadScriptedCommand(
      InputStream in, String name, Class[] argTypes, String resourcePath, Interpreter interpreter)
      throws UtilEvalError {
    try {
      interpreter.eval(new InputStreamReader(in), this, resourcePath);
    } catch (EvalError e) {
      /*
      	Here we catch any EvalError from the interpreter because we are
      	using it as a tool to load the command, not as part of the
      	execution path.
      */
      Interpreter.debug(e.toString());
      throw new UtilEvalError("Error loading script: " + e.getMessage(), e);
    }

    // Look for the loaded command
    BshMethod meth = getMethod(name, argTypes);
    /*
    if ( meth == null )
    	throw new UtilEvalError("Loaded resource: " + resourcePath +
    		"had an error or did not contain the correct method" );
    */

    return meth;
  }
  public String[][] exec(String[] befehle) {
    String[][] antworten = new String[befehle.length][1];
    Object aktObj;
    for (int i = 0; i < befehle.length; i++) {
      try {
        aktObj = interpreter.eval(befehle[i]);
        if (aktObj != null) antworten[i][0] = aktObj.toString();
        else antworten[i][0] = "null";
      } catch (EvalError e) {
        System.err.println(e.getMessage());
        antworten[i][0] = "ERROR";
      }
      if (verbose) {
        assert !quiet;
        System.out.println("# " + i + ":  " + befehle[i]);
        for (int j = 0; j < antworten[i].length; j++) {
          System.out.println("@ " + antworten[i][j]);
        }
      } else if (!quiet)
        for (int j = 0; j < antworten[i].length; j++) {
          System.out.print("#");
        }
    }

    return antworten;
  }
  public String[] exec(String befehl) {
    String[] antwort = new String[1];
    Object aktObj;
    try {
      aktObj = interpreter.eval(befehl);
      if (aktObj != null) antwort[0] = aktObj.toString();
      else antwort[0] = "null";
    } catch (EvalError e) {
      System.err.println(e.getMessage());
      antwort[0] = "ERROR";
    }

    if (!quiet) System.out.print("#");
    if (verbose) {
      assert !quiet;
      System.out.println(" " + befehl);
      System.out.println("@ " + antwort[0]);
    }

    return antwort;
  }
Ejemplo n.º 4
0
    // -----------------------------------------------------------------
    //  Apply the function (or inverse) defined by the object
    //  to Value 'v'.
    // -----------------------------------------------------------------
    void applyTo(Value v, String inv) {
      v.completereduce();
      if (dimen != null) {
        Value dim;
        try {
          dim = Value.parse(dimen);
        } catch (EvalError e) {
          throw new EvalError(
              "Invalid dimension, "
                  + dimen
                  + ", of function "
                  + inv
                  + name
                  + ". "
                  + e.getMessage());
        }
        dim.completereduce();
        if (!dim.isCompatibleWith(v, Factor.Ignore.NONE))
          throw new EvalError(
              "Argument "
                  + v.asString()
                  + " of function "
                  + inv
                  + name
                  + " is not conformable to "
                  + dim.asString()
                  + ".");
      }

      Value result;
      try {
        result = Value.parse(def, param, v);
      } catch (EvalError e) {
        throw new EvalError(
            "Invalid definition of function '" + inv + name + "'. " + e.getMessage());
      }

      v.copyFrom(result);
    } // end applyTo
Ejemplo n.º 5
0
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      // A good request looks like /mljam/contextid/verb?name=varname
      // The extra path info includes the context id and verb
      String extra = req.getPathInfo(); // "/contextid/verb"
      if (extra == null || extra.equals("")) {
        throw new ClientProblemException(
            "Request requires a context id and verb in its extra path info");
      }
      String[] parts = extra.split("/"); // { "", "contextid", "verb" }
      if (parts.length < 2) {
        throw new ClientProblemException(
            "Request requires a context id and verb in its extra path info");
      } else if (parts.length < 3) {
        throw new ClientProblemException("Request requires a verb in its extra path info");
      }

      String contextId = parts[1];
      String verb = parts[2];
      String method = req.getMethod();

      if (method.equalsIgnoreCase("get")) {

        // We have three GET verbs: get, get-stdout, get-stderr.
        // These are all idempotent, while the POST verbs aren't.  The get
        // verb accept a "name" query string parameter.  The get verb returns
        // either XQuery to evaluate (indicated by x-marklogic/xquery content type)
        // or a raw binary (indicated by an application/binary-encoded content type).

        if (verb.equalsIgnoreCase("get")) {
          String name = req.getParameter("name");
          if (name == null || name.equals("")) {
            throw new ClientProblemException("The get verb requires a name parameter");
          }
          Interpreter i = getInterpreter(contextId);
          Object o = i.get(name);
          if (o instanceof byte[]) {
            sendBinaryResponse(res, (byte[]) o);
          } else if (o instanceof String) {
            sendStringResponse(res, (String) o);
          } else {
            sendXQueryResponse(res, o);
          }
        } else if (verb.equalsIgnoreCase("get-stdout")) {
          Interpreter i = getInterpreter(contextId);
          i.getOut().flush();
          CircularByteArrayOutputStream circ = (CircularByteArrayOutputStream) i.get("mljamout");
          if (circ != null) {
            sendStringResponse(res, circ.toString());
            circ.reset();
          } else {
            throw new ServerProblemException("Could not fetch mljamout from interpreter context");
          }
        } else if (verb.equalsIgnoreCase("get-stderr")) {
          Interpreter i = getInterpreter(contextId);
          i.getErr().flush();
          CircularByteArrayOutputStream circ = (CircularByteArrayOutputStream) i.get("mljamerr");
          if (circ != null) {
            sendStringResponse(res, circ.toString());
            circ.reset();
          } else {
            throw new ServerProblemException("Could not fetch mljamerr from interpreter context");
          }
        } else {
          throw new ClientProblemException("Unrecognized GET verb: " + verb);
        }
      } else if (method.equalsIgnoreCase("post")) {
        // We have six POST verbs: eval, unset, end, source, set-string, and set-binary.
        // These are POST verbs because they aren't idempotent.
        // The set-string, set-binary, unset, and source verbs accept a "name"
        // query string parameter.  The set-string and set-binary verbs accept
        // a value in their post body.  The eval verb accepts code in its post body.

        if (verb.equalsIgnoreCase("set-string")) {
          String name = req.getParameter("name");
          if (name == null || name.equals("")) {
            throw new ClientProblemException("The set-string verb requires a name parameter");
          }
          String body = getBody(req); // a value of "" is legit
          Interpreter i = getInterpreter(contextId);
          i.unset(name);
          i.set(name, body);
          sendNoResponse(res);
        } else if (verb.equalsIgnoreCase("set-binary")) {
          String name = req.getParameter("name");
          if (name == null || name.equals("")) {
            throw new ClientProblemException("The set-binary verb requires a name parameter");
          }
          String body = getBody(req); // a value of "" is legit
          byte[] bodyBytes = hexDecode(body); // later could do this streaming for speed
          Interpreter i = getInterpreter(contextId);
          i.unset(name);
          i.set(name, bodyBytes);
          sendNoResponse(res);
        } else if (verb.equalsIgnoreCase("eval")) {
          String body = getBody(req);
          if (body == null || body.equals("")) {
            throw new ClientProblemException(
                "The eval verb requires a post body containing code to eval");
          }
          Interpreter i = getInterpreter(contextId);
          i.eval(body);
          sendNoResponse(res);
        } else if (verb.equalsIgnoreCase("eval-get")) {
          String body = getBody(req);
          if (body == null || body.equals("")) {
            throw new ClientProblemException(
                "The eval-get verb requires a post body containing code to eval");
          }
          Interpreter i = getInterpreter(contextId);
          Object o = i.eval(body);
          if (o instanceof byte[]) {
            sendBinaryResponse(res, (byte[]) o);
          } else if (o instanceof String) {
            sendStringResponse(res, (String) o);
          } else {
            sendXQueryResponse(res, o);
          }
        } else if (verb.equalsIgnoreCase("unset")) {
          String name = req.getParameter("name");
          if (name == null || name.equals("")) {
            throw new ClientProblemException("The unset verb requires a name parameter");
          }
          Interpreter i = getInterpreter(contextId);
          i.unset(name);
          sendNoResponse(res);
        } else if (verb.equalsIgnoreCase("end")) {
          endInterpreter(contextId);
          sendNoResponse(res);
        } else if (verb.equalsIgnoreCase("source")) {
          String name = req.getParameter("name");
          if (name == null || name.equals("")) {
            throw new ClientProblemException("The source verb requires a name parameter");
          }
          Interpreter i = getInterpreter(contextId);
          i.source(name);
          sendNoResponse(res);
        } else {
          throw new ClientProblemException("Unrecognized POST verb: " + verb);
        }
      }
    } catch (TargetError e) {
      Throwable target = e.getTarget();
      Log.log(e);
      Log.log("Target: " + target);
      sendServerProblemResponse(
          res,
          target.getClass().getName()
              + ": "
              + target.getMessage()
              + " when executing Java code: "
              + e.getErrorText()); // include full trace?
    } catch (EvalError e) {
      Log.log(e);
      sendServerProblemResponse(
          res, e.getClass().getName() + ": " + e.getMessage()); // include full trace?
    } catch (ClientProblemException e) {
      Log.log(e);
      sendClientProblemResponse(res, e.getMessage());
    } catch (ServerProblemException e) {
      Log.log(e);
      sendServerProblemResponse(res, e.getMessage());
    }
  }