KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > proxy > Daemon


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Daemon.java,v 1.12 2004/02/18 22:50:41 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.proxy;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InterruptedIOException JavaDoc;
23 import java.net.ServerSocket JavaDoc;
24 import java.net.Socket JavaDoc;
25
26 import org.apache.jorphan.logging.LoggingManager;
27 import org.apache.log.Logger;
28
29 /**
30  * Web daemon thread. Creates main socket on port 8080 and listens on it
31  * forever. For each client request, creates a proxy thread to handle the
32  * request.
33  *
34  * @author default
35  * Created June 29, 2001
36  * @version $Revision: 1.12 $ Last updated: $Date: 2004/02/18 22:50:41 $
37  */

38 public class Daemon extends Thread JavaDoc
39 {
40     /** Logging */
41     private static transient Logger log = LoggingManager.getLoggerForClass();
42
43     /** The default port to listen on. */
44     private static final int DEFAULT_DAEMON_PORT = 8080;
45
46     /** The maximum allowed port to listen on. */
47     private static final int MAX_DAEMON_PORT = 65535;
48
49     /**
50      * The time (in milliseconds) to wait when accepting a client connection.
51      * The accept will be retried until the Daemon is told to stop. So this
52      * interval is the longest time that the Daemon will have to wait after
53      * being told to stop.
54      */

55     private static final int ACCEPT_TIMEOUT = 1000;
56
57     /** The port to listen on. */
58     private int daemonPort;
59
60     /** True if the Daemon is currently running. */
61     private boolean running;
62
63     /** The target which will receive the generated JMeter test components. */
64     private ProxyControl target;
65
66     /**
67      * The proxy class which will be used to handle individual requests. This
68      * class must be the {@link Proxy} class or a subclass.
69      */

70     private Class JavaDoc proxyClass = Proxy.class;
71
72
73     /**
74      * Default constructor.
75      */

76     public Daemon()
77     {
78         super("HTTP Proxy Daemon");
79     }
80
81     /**
82      * Create a new Daemon with the specified port and target.
83      *
84      * @param port the port to listen on.
85      * @param target the target which will receive the generated JMeter test
86      * components.
87      */

88     public Daemon(int port, ProxyControl target)
89     {
90         this();
91         this.target = target;
92         configureProxy(port);
93     }
94
95     /**
96      * Create a new Daemon with the specified port and target, using the
97      * specified class to handle individual requests.
98      *
99      * @param port the port to listen on.
100      * @param target the target which will receive the generated JMeter test
101      * components.
102      * @param proxyClass the proxy class to use to handle individual requests.
103      * This class must be the {@link Proxy} class or a
104      * subclass.
105      */

106     public Daemon(int port, ProxyControl target, Class JavaDoc proxyClass)
107     {
108         this(port, target);
109         this.proxyClass = proxyClass;
110     }
111
112     /**
113      * Configure the Daemon to listen on the specified port.
114      * @param daemonPort the port to listen on
115      */

116     public void configureProxy(int daemonPort)
117     {
118         this.daemonPort = daemonPort;
119         log.info("Proxy: OK");
120     }
121
122     /**
123      * Main method which will start the Proxy daemon on the specified port (or
124      * the default port if no port is specified).
125      *
126      * @param args the command-line arguments
127      */

128     public static void main(String JavaDoc 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 JavaDoc 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     /**
164      * Listen on the daemon port and handle incoming requests. This method will
165      * not exit until {@link #stopServer()} is called or an error occurs.
166      */

167     public void run()
168     {
169         running = true;
170         ServerSocket JavaDoc mainSocket = null;
171
172         try
173         {
174             log.info("Creating Daemon Socket... on port " + daemonPort);
175             mainSocket = new ServerSocket JavaDoc(daemonPort);
176             mainSocket.setSoTimeout(ACCEPT_TIMEOUT);
177             log.info("Proxy up and running!");
178
179             while (running)
180             {
181                 try
182                 {
183                     // Listen on main socket
184
Socket JavaDoc clientSocket = mainSocket.accept();
185                     if (running)
186                     {
187                         // Pass request to new proxy thread
188
Proxy thd = (Proxy) proxyClass.newInstance();
189                         thd.configure(clientSocket, target);
190                         thd.start();
191                     }
192                     else
193                     {
194                         // The socket was accepted after we were told to stop.
195
try
196                         {
197                             clientSocket.close();
198                         }
199                         catch (IOException JavaDoc e)
200                         {
201                             // Ignore
202
}
203                     }
204                 }
205                 catch (InterruptedIOException JavaDoc e)
206                 {
207                     // Timeout occurred. Ignore, and keep looping until we're
208
// told to stop running.
209
}
210             }
211             log.info("Proxy Server stopped");
212         }
213         catch (Exception JavaDoc 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 JavaDoc exc)
224             {
225             }
226         }
227     }
228
229     /**
230      * Stop the proxy daemon. The daemon may not stop immediately.
231      *
232      * see #ACCEPT_TIMEOUT
233      */

234     public void stopServer()
235     {
236         running = false;
237     }
238 }
239
Popular Tags