📄 httpdebug.java
字号:
/* * Copyright (C) 2001 - 2003 ScalAgent Distributed Technologies * Copyright (C) 1996 - 2000 BULL * Copyright (C) 1996 - 2000 INRIA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package fr.dyade.aaa.agent;import java.io.*;import java.net.*;import java.text.*;import java.util.*;import org.objectweb.util.monolog.api.BasicLevel;import org.objectweb.util.monolog.api.Logger;import fr.dyade.aaa.util.Daemon;/** * A <code>HttpDebug</code> service provides an HTTP interface to * access to debugging functions in running agent servers. * <p> * The <code>HttpDebug</code> service needs an argument: the TCP port number. * Moreover, The <code>HttpDebug</code> service exports its connecting * parameters in a file named server<serverId>HttpProxy.cnx. * It may be accessed using a HTTP browser client. * <p> * Actually, there is only one thread running, which reads and analyses * commands from the input flow,and writes results synchronously onto the * output flow. */public class HttpDebug { static HttpDebug httpd = null; static int port = 8090; HttpDebugMonitor monitors[] = null; ServerSocket listen = null; static boolean debug = true; DebugMonitor dmon = null; static Logger xlogmon = null; /** * Initializes the package as a well known service. * <p> * Creates a <code>HttpDebug</code> proxy. * * @param args parameters from the configuration file * @param firstTime <code>true</code> when service starts anew */ public static void init(String args, boolean firstTime) throws Exception { if (args.length()!=0) { try { port = Integer.parseInt(args); } catch (NumberFormatException exc) {} } // Get the logging monitor from current server MonologLoggerFactory xlogmon = Debug.getLogger(Debug.A3Service + ".HttpDebug"); if (httpd == null) httpd = new HttpDebug(port); startService(); } public static void startService() { for (int i=0; i<httpd.monitors.length; i++) { httpd.monitors[i].start(); }// if (debug) dmon.start(); } public static void stopService() { for (int i=0; i<httpd.monitors.length; i++) { if (httpd.monitors[i] != null) httpd.monitors[i].stop(); }// if (debug && (dmon != null)) dmon.stop(); httpd = null; } String host = null; String base = null; /** * Creates an HttpDebug service. * * @param port TCP listen port of this proxy. */ private HttpDebug(int port) throws IOException { if (port != 0) this.port = port; host = InetAddress.getLocalHost().getHostName(); base = "http://" + host + ":" + port; listen = new ServerSocket(port); monitors = new HttpDebugMonitor[1]; for (int i=0; i<monitors.length; i++) { monitors[i] = new HttpDebugMonitor("HttpDebug#" + AgentServer.getServerId() + '.' + i); }// if (debug) dmon = new DebugMonitor(); } /** * Provides a string image for this object. */ public String toString() { StringBuffer strBuf = new StringBuffer(); strBuf.append("(").append(super.toString()); strBuf.append(",port=").append(port); strBuf.append(",monitors=["); for (int i=0; i<monitors.length; i++) { strBuf.append(monitors[i].toString()).append(","); } strBuf.append("]"); strBuf.append(")"); return strBuf.toString(); } final static String CMD_HELP = "/HELP"; final static String CMD_ADMIN = "/ADMIN"; final static String CMD_AGENTS = CMD_ADMIN + "/AGENTS"; final static String CMD_SERVERS = CMD_ADMIN + "/SERVERS"; final static String CMD_MSG_CONS = CMD_ADMIN + "/MSGCONS"; final static String CMD_SERVICES = CMD_ADMIN + "/SERVICES"; final static String CMD_DUMP_AGENT = CMD_ADMIN + "/DUMP_AGENT"; final static String CMD_DEBUG = "/DEBUG"; final static String CMD_DEBUG_WAIT = "/XEBUGWAIT"; final static String CMD_DEBUG_RUN = "/XEBUGRUN"; final static String CMD_CLASS = "/CLASS"; final static String CMD_STOP = "/STOP"; final static String CMD_START = "/START"; final static String CMD_QUEUE = "/QUEUE"; final static String CMD_DUMP = "/DUMP"; final static String CMD_REMOVE = "/REMOVE"; final static String CMD_THREADS = "/THREADS"; class HttpDebugMonitor extends Daemon { Socket socket = null; BufferedReader reader = null; PrintWriter writer = null; /** * Constructor. */ protected HttpDebugMonitor(String name) { super(name, HttpDebug.xlogmon); // Get the logging monitor from HttpDebug (overload Daemon setup) this.setThreadGroup(AgentServer.getThreadGroup()); } /** * Provides a string image for this object. * * @return printable image of this object */ public String toString() { return "(" + super.toString() + ",socket=" + socket + ")"; } public void run() { try { while (running) { canStop = true; try { socket = listen.accept(); canStop = false; } catch (IOException exc) { if (running) logmon.log(BasicLevel.ERROR, getName() + ", error during accept", exc); } if (! running) break; try { // Get the streams reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream(), true); // Reads then parses the request doRequest(reader.readLine()); writer.flush(); } catch (Exception exc) { logmon.log(BasicLevel.ERROR, getName() + ", error during connection", exc); } finally { // Closes the connection try { reader.close(); } catch (Exception exc) {} reader = null; try { writer.close(); } catch (Exception exc) {} writer = null; try { socket.close(); } catch (Exception exc) {} socket = null; } } } finally { finish(); } } protected void close() { try { listen.close(); } catch (Exception exc) {} listen = null; } protected void shutdown() { close (); } void header(String title) throws IOException { writer.println("<HTML>"); writer.println("<HEAD>"); writer.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>"); writer.println("<STYLE TYPE=\"text/css\">"); writer.println("<!-- "); writer.println(" /* Style definitions */"); writer.println(" H1 {font-family:Arial,Helvetica;font-weight:bold;font-size:36pt}"); writer.println(" H2 {font-family:Arial,Helvetica;font-weight:bold;font-size:20pt}"); writer.println(" H3 {font-family:Arial,Helvetica;font-weight:bold;font-size:16pt}"); writer.println(" CODE {font-size:14}"); writer.println(" .NORMAL {font-family:Arial,Helvetica;font-style:normal;font-size:14}"); writer.println(" .BOTTOM {font-family:Arial,Helvetica;font-style:italic;font-size:10pt}"); writer.println(" .FONT1 {font-family:Arial,Helvetica;font-weight:normal;font-size:18pt}"); writer.println(" .FONT2 {font-family:Arial,Helvetica;font-weight:normal;font-size:14pt}"); writer.println(" .FONT3 {font-family:Arial,Helvetica;font-weight:normal;font-size:12pt}"); writer.println(" a:link {color:white}"); writer.println(" a:visited{color:white}"); writer.println(" a:active{color:white}"); writer.println("-->"); writer.println("</STYLE>"); writer.println("<TITLE>" + title + "</TITLE>"); writer.println("</HEAD>"); writer.println("<BODY bgcolor=\"#CCCCCC\">"); writer.println("<H1>" + title + "</H1>"); writer.println("<HR WIDTH=\"100%\"/>"); } void menu() throws IOException { writer.println("<TABLE BORDER=\"1\" WIDTH=\"100%\" BGCOLOR=\"#3366FF\">"); writer.println("<TR>"); writer.println("<TD CLASS=FONT1><CENTER>"); writer.println("<A href=\"" + base + CMD_SERVERS + "\">Servers</A>"); writer.println("</CENTER></TD>"); writer.println("<TD CLASS=FONT1><CENTER>"); writer.println("<A href=\"" + base + CMD_AGENTS + "\">Agents</A>"); writer.println("</CENTER></TD>"); writer.println("<TD CLASS=FONT1><CENTER>"); writer.println("<A href=\"" + base + CMD_MSG_CONS + "\">Consumers</A>"); writer.println("</CENTER></TD>"); writer.println("<TD CLASS=FONT1><CENTER>"); writer.println("<A href=\"" + base + CMD_SERVICES + "\">Services</A>"); writer.println("</CENTER></TD>"); if (debug) {// writer.println("<TD CLASS=FONT1><CENTER>");// writer.println("<A href=\"" + base + // CMD_DEBUG + "\" target=\"A3Debug\">Debug</A>");// writer.println("</CENTER></TD>"); writer.println("<TD CLASS=FONT1><CENTER>"); writer.println("<A href=\"" + base + CMD_THREADS + "\">Threads</A>"); writer.println("</CENTER></TD>"); } writer.println("</TR>"); writer.println("</TABLE>"); writer.println("<HR WIDTH=\"100%\"/>"); } void error(Exception exc) throws IOException { writer.println("<H2>Error</H2>"); writer.println("<BLOCKQUOTE><PRE>"); exc.printStackTrace(writer); writer.println("</PRE></BLOCKQUOTE>"); } void help() throws IOException { writer.println("<H2>Help</H2>"); usage(); } void unknown(String cmd) throws IOException { writer.println("<H2>Error</H2>"); writer.println("<P CLASS=FONT2>"); writer.println("Unknown command \"" + cmd + "\""); writer.println("</P>"); usage(); } void usage() throws IOException { writer.println("<H2>Usage</H2>"); writer.println("<P CLASS=FONT2>"); writer.println("To be provided."); writer.println("</P>"); } void footer() throws IOException { writer.println("<HR WIDTH=\"100%\"/>"); writer.println("<P CLASS=BOTTOM>"); writer.println("Generated by fr.dyade.aaa.agent.HttpDebug, version 1.0, date:" + new Date().toString()); writer.println("</P>"); writer.println("</BODY>"); writer.println("</HTML>"); } public void doRequest(String request) { String cmd = null;// System.out.println("request=" + request); try { StringTokenizer st = new StringTokenizer(request); if ((st.countTokens() >= 2) && st.nextToken().equals("GET")) { cmd = st.nextToken(); StringBuffer buf = new StringBuffer(); try { if ((cmd.equals("/")) || (cmd.equals(CMD_HELP))) { // first page header("A3 AgentServer #" + AgentServer.getServerId()); menu(); help(); } else if (cmd.startsWith(CMD_HELP)) { header("A3 AgentServer #" + AgentServer.getServerId()); // help(cmd.substring(CMD_HELP.length())); } else if (cmd.startsWith(CMD_ADMIN)) { header("A3 AgentServer #" + AgentServer.getServerId() + " Administration"); menu(); if (cmd.equals(CMD_AGENTS)) { // a list of agents has been requested listAgents(buf); } else if (cmd.startsWith(CMD_SERVERS)) { // a list of servers has been requested
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -