Example #1
0
  public boolean shutdown(int port, boolean ssl) {
    try {
      String protocol = "http" + (ssl ? "s" : "");
      URL url = new URL(protocol, "127.0.0.1", port, "shutdown");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("servicemanager", "shutdown");
      conn.connect();

      StringBuffer sb = new StringBuffer();
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      int n;
      char[] cbuf = new char[1024];
      while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sb.append(cbuf, 0, n);
      br.close();
      String message = sb.toString().replace("<br>", "\n");
      if (message.contains("Goodbye")) {
        cp.appendln("Shutting down the server:");
        String[] lines = message.split("\n");
        for (String line : lines) {
          cp.append("...");
          cp.appendln(line);
        }
        return true;
      }
    } catch (Exception ex) {
    }
    cp.appendln("Unable to shutdown CTP");
    return false;
  }
Example #2
0
 private String replace(String string, Properties table) {
   try {
     Pattern pattern = Pattern.compile("\\$\\{\\w+\\}");
     Matcher matcher = pattern.matcher(string);
     StringBuffer sb = new StringBuffer();
     while (matcher.find()) {
       String group = matcher.group();
       String key = group.substring(2, group.length() - 1).trim();
       String repl = table.getProperty(key);
       if (repl == null) repl = matcher.quoteReplacement(group);
       matcher.appendReplacement(sb, repl);
     }
     matcher.appendTail(sb);
     return sb.toString();
   } catch (Exception ex) {
     return string;
   }
 }
Example #3
0
 private static String toString(Node node) {
   StringBuffer sb = new StringBuffer();
   renderNode(sb, node);
   return sb.toString();
 }
Example #4
0
  /*
   * Code lifted from Apache ANT with thanks
   */
  private static String[] translateCommandline(String toProcess) {
    if (toProcess == null || toProcess.length() == 0) {
      // no command? no string
      return new String[0];
    }
    // parse with a simple finite state machine

    final int normal = 0;
    final int inQuote = 1;
    final int inDoubleQuote = 2;
    int state = normal;
    StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
    Vector<String> v = new Vector<String>();
    StringBuffer current = new StringBuffer();
    boolean lastTokenHasBeenQuoted = false;

    while (tok.hasMoreTokens()) {
      String nextTok = tok.nextToken();
      switch (state) {
        case inQuote:
          if ("\'".equals(nextTok)) {
            lastTokenHasBeenQuoted = true;
            state = normal;
          } else {
            current.append(nextTok);
          }
          break;
        case inDoubleQuote:
          if ("\"".equals(nextTok)) {
            lastTokenHasBeenQuoted = true;
            state = normal;
          } else {
            current.append(nextTok);
          }
          break;
        default:
          if ("\'".equals(nextTok)) {
            state = inQuote;
          } else if ("\"".equals(nextTok)) {
            state = inDoubleQuote;
          } else if (" ".equals(nextTok)) {
            if (lastTokenHasBeenQuoted || current.length() != 0) {
              v.addElement(current.toString());
              current = new StringBuffer();
            }
          } else {
            current.append(nextTok);
          }
          lastTokenHasBeenQuoted = false;
          break;
      }
    }
    if (lastTokenHasBeenQuoted || current.length() != 0) {
      v.addElement(current.toString());
    }
    if (state == inQuote || state == inDoubleQuote) {
      throw new MDCCException("unbalanced quotes in " + toProcess);
    }
    String[] args = new String[v.size()];
    v.copyInto(args);
    return args;
  }