1 18 19 package org.apache.jmeter.protocol.http.proxy; 20 21 import java.io.IOException ; 22 import java.io.InterruptedIOException ; 23 import java.net.ServerSocket ; 24 import java.net.Socket ; 25 26 import org.apache.jorphan.logging.LoggingManager; 27 import org.apache.log.Logger; 28 29 38 public class Daemon extends Thread 39 { 40 41 private static transient Logger log = LoggingManager.getLoggerForClass(); 42 43 44 private static final int DEFAULT_DAEMON_PORT = 8080; 45 46 47 private static final int MAX_DAEMON_PORT = 65535; 48 49 55 private static final int ACCEPT_TIMEOUT = 1000; 56 57 58 private int daemonPort; 59 60 61 private boolean running; 62 63 64 private ProxyControl target; 65 66 70 private Class proxyClass = Proxy.class; 71 72 73 76 public Daemon() 77 { 78 super("HTTP Proxy Daemon"); 79 } 80 81 88 public Daemon(int port, ProxyControl target) 89 { 90 this(); 91 this.target = target; 92 configureProxy(port); 93 } 94 95 106 public Daemon(int port, ProxyControl target, Class proxyClass) 107 { 108 this(port, target); 109 this.proxyClass = proxyClass; 110 } 111 112 116 public void configureProxy(int daemonPort) 117 { 118 this.daemonPort = daemonPort; 119 log.info("Proxy: OK"); 120 } 121 122 128 public static void main(String args[]) 129 { 130 if (args.length > 1) 131 { 132 System.err.println ("Usage: Daemon [daemon port]"); 133 log.info("Usage: Daemon [daemon port]"); 134 return; 135 } 136 137 int daemonPort = DEFAULT_DAEMON_PORT; 138 if (args.length > 0) 139 { 140 try 141 { 142 daemonPort = Integer.parseInt(args[0]); 143 } 144 catch (NumberFormatException e) 145 { 146 System.err.println ("Invalid daemon port: " + e); 147 log.error("Invalid daemon port", e); 148 return; 149 } 150 if (daemonPort <= 0 || daemonPort > MAX_DAEMON_PORT) 151 { 152 System.err.println ("Invalid daemon port"); 153 log.error("Invalid daemon port"); 154 return; 155 } 156 } 157 158 Daemon demon = new Daemon(); 159 demon.configureProxy(daemonPort); 160 demon.start(); 161 } 162 163 167 public void run() 168 { 169 running = true; 170 ServerSocket mainSocket = null; 171 172 try 173 { 174 log.info("Creating Daemon Socket... on port " + daemonPort); 175 mainSocket = new ServerSocket (daemonPort); 176 mainSocket.setSoTimeout(ACCEPT_TIMEOUT); 177 log.info("Proxy up and running!"); 178 179 while (running) 180 { 181 try 182 { 183 Socket clientSocket = mainSocket.accept(); 185 if (running) 186 { 187 Proxy thd = (Proxy) proxyClass.newInstance(); 189 thd.configure(clientSocket, target); 190 thd.start(); 191 } 192 else 193 { 194 try 196 { 197 clientSocket.close(); 198 } 199 catch (IOException e) 200 { 201 } 203 } 204 } 205 catch (InterruptedIOException e) 206 { 207 } 210 } 211 log.info("Proxy Server stopped"); 212 } 213 catch (Exception e) 214 { 215 log.warn("Proxy Server stopped", e); 216 } 217 finally 218 { 219 try 220 { 221 if (mainSocket != null) mainSocket.close(); 222 } 223 catch (Exception exc) 224 { 225 } 226 } 227 } 228 229 234 public void stopServer() 235 { 236 running = false; 237 } 238 } 239 | Popular Tags |