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; }
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; } }
private boolean checkServer(int port, boolean ssl) { try { URL url = new URL("http://127.0.0.1:" + port); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int length = conn.getContentLength(); StringBuffer text = new StringBuffer(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int size = 256; char[] buf = new char[size]; int len; while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len); isr.close(); if (programName.equals("ISN")) return !shutdown(port, ssl); return true; } catch (Exception ex) { return false; } }
// Recursively walk the tree and write the nodes to a StringWriter. private static void renderNode(StringBuffer sb, Node node) { if (node == null) { sb.append("null"); return; } switch (node.getNodeType()) { case Node.DOCUMENT_NODE: sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); Node root = ((Document) node).getDocumentElement(); renderNode(sb, root); break; case Node.ELEMENT_NODE: String name = getNodeNameWithNamespace(node); NamedNodeMap attributes = node.getAttributes(); if (attributes.getLength() == 0) { sb.append("<" + name + ">"); } else { sb.append("<" + name + " "); int attrlen = attributes.getLength(); for (int i = 0; i < attrlen; i++) { Node attr = attributes.item(i); String attrName = getNodeNameWithNamespace(attr); sb.append(attrName + "=\"" + escapeChars(attr.getNodeValue())); if (i < attrlen - 1) sb.append("\" "); else sb.append("\">"); } } NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { renderNode(sb, children.item(i)); } } sb.append("</" + name + ">"); break; case Node.TEXT_NODE: sb.append(escapeChars(node.getNodeValue())); break; case Node.CDATA_SECTION_NODE: sb.append("<![CDATA[" + node.getNodeValue() + "]]>"); break; case Node.PROCESSING_INSTRUCTION_NODE: sb.append("<?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?>"); break; case Node.ENTITY_REFERENCE_NODE: sb.append("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: // Ignore document type nodes break; case Node.COMMENT_NODE: sb.append("<!--" + node.getNodeValue() + "-->"); break; } return; }
private static String toString(Node node) { StringBuffer sb = new StringBuffer(); renderNode(sb, node); return sb.toString(); }