1 16 17 package org.apache.axis.monitor; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServlet ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.net.ServerSocket ; 28 import java.net.Socket ; 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 32 62 63 public class SOAPMonitorService extends HttpServlet { 64 65 68 private static ServerSocket server_socket = null; 69 private static Vector connections = null; 70 71 74 public SOAPMonitorService() { 75 } 76 77 78 81 public static void publishMessage(Long id, 82 Integer type, 83 String target, 84 String soap) { 85 if (connections != null) { 86 Enumeration e = connections.elements(); 87 while (e.hasMoreElements()) { 88 ConnectionThread ct = (ConnectionThread) e.nextElement(); 89 ct.publishMessage(id,type,target,soap); 90 } 91 } 92 } 93 94 97 public void init() throws ServletException { 98 if (connections == null) { 99 connections = new Vector (); 101 } 102 if (server_socket == null) { 103 ServletConfig config = super.getServletConfig(); 105 String port = config.getInitParameter(SOAPMonitorConstants.SOAP_MONITOR_PORT); 106 if (port == null) { 107 port = "0"; 109 } 110 try { 111 server_socket = new ServerSocket (Integer.parseInt(port)); 113 } catch (Exception e) { 114 server_socket = null; 117 } 118 if (server_socket != null) { 119 new Thread (new ServerSocketThread()).start(); 121 } 122 } 123 } 124 125 128 public void destroy() { 129 Enumeration e = connections.elements(); 131 while (e.hasMoreElements()) { 132 ConnectionThread ct = (ConnectionThread) e.nextElement(); 133 ct.close(); 134 } 135 if (server_socket != null) { 137 try { 138 server_socket.close(); 139 } catch (Exception x) {} 140 server_socket = null; 141 } 142 } 143 144 147 public void doGet(HttpServletRequest request, HttpServletResponse response) 148 throws IOException , ServletException 149 { 150 int port = 0; 152 if (server_socket != null) { 153 port = server_socket.getLocalPort(); 154 } 155 response.setContentType("text/html"); 156 response.getWriter().println("<html>"); 157 response.getWriter().println("<head>"); 158 response.getWriter().println("<title>SOAP Monitor</title>"); 159 response.getWriter().println("</head>"); 160 response.getWriter().println("<body>"); 161 response.getWriter().println("<object classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" width=100% height=100% codebase=\"http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0\">"); 162 response.getWriter().println("<param name=code value=SOAPMonitorApplet.class>"); 163 response.getWriter().println("<param name=\"type\" value=\"application/x-java-applet;version=1.3\">"); 164 response.getWriter().println("<param name=\"scriptable\" value=\"false\">"); 165 response.getWriter().println("<param name=\"port\" value=\""+port+"\">"); 166 response.getWriter().println("<comment>"); 167 response.getWriter().println("<embed type=\"application/x-java-applet;version=1.3\" code=SOAPMonitorApplet.class width=100% height=100% port=\""+port+"\" scriptable=false pluginspage=\"http://java.sun.com/products/plugin/1.3/plugin-install.html\">"); 168 response.getWriter().println("<noembed>"); 169 response.getWriter().println("</comment>"); 170 response.getWriter().println("</noembed>"); 171 response.getWriter().println("</embed>"); 172 response.getWriter().println("</object>"); 173 response.getWriter().println("</body>"); 174 response.getWriter().println("</html>"); 175 } 176 177 180 class ServerSocketThread implements Runnable { 181 182 185 public void run() { 186 while (server_socket != null) { 188 try { 189 Socket socket = server_socket.accept(); 190 new Thread (new ConnectionThread(socket)).start(); 191 } catch (IOException ioe) {} 192 } 193 } 194 } 195 196 199 class ConnectionThread implements Runnable { 200 201 private Socket socket = null; 202 private ObjectInputStream in = null; 203 private ObjectOutputStream out = null; 204 private boolean closed = false; 205 206 209 public ConnectionThread(Socket s) { 210 socket = s; 211 try { 212 out = new ObjectOutputStream (socket.getOutputStream()); 219 out.flush(); 220 in = new ObjectInputStream (socket.getInputStream()); 221 } catch (Exception e) {} 222 synchronized (connections) { 224 connections.addElement(this); 225 } 226 } 227 228 231 public void close() { 232 closed = true; 233 try { 234 socket.close(); 235 } catch (IOException ioe) {} 236 } 237 238 241 public void run() { 242 try { 243 while (!closed) { 244 Object o = in.readObject(); 245 } 246 } catch (Exception e) {} 247 synchronized (connections) { 249 connections.removeElement(this); 250 } 251 if (out != null) { 253 try { 254 out.close(); 255 } catch (IOException ioe) {} 256 out = null; 257 } 258 if (in != null) { 259 try { 260 in.close(); 261 } catch (IOException ioe) {} 262 in = null; 263 } 264 close(); 266 } 267 268 271 public synchronized void publishMessage(Long id, 272 Integer message_type, 273 String target, 274 String soap) { 275 if (out != null) { 278 try { 279 switch (message_type.intValue()) { 280 case SOAPMonitorConstants.SOAP_MONITOR_REQUEST: 281 out.writeObject(message_type); 282 out.writeObject(id); 283 out.writeObject(target); 284 out.writeObject(soap); 285 out.flush(); 286 break; 287 case SOAPMonitorConstants.SOAP_MONITOR_RESPONSE: 288 out.writeObject(message_type); 289 out.writeObject(id); 290 out.writeObject(soap); 291 out.flush(); 292 break; 293 } 294 } catch (Exception e) {} 295 } 296 } 297 } 298 } 299 300 | Popular Tags |