source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitoringProxy.java @ 1381

Last change on this file since 1381 was 1381, checked in by pharms, 10 years ago
  • removed find bugs warning
File size: 7.8 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.httpmonitor.proxy;
16
17import javax.servlet.Servlet;
18
19import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent;
20import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException;
21import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler;
22import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorLogManager;
23import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorServer;
24import de.ugoe.cs.autoquest.httpmonitor.Runner;
25import de.ugoe.cs.autoquest.httpmonitor.ShutdownHook;
26import de.ugoe.cs.util.console.Console;
27
28/**
29 * <p>
30 * TODO comment
31 * The HTML monitor starts a web server ({@link HttpMonitorServer}) that receives log messages
32 * of the HTML monitor java script. These messages are logged using the
33 * {@link ExchangeListenerManager}. The class assures that on shutdown e.g. caused by CTRL-C the
34 * server and the log manager are stopped correctly.
35 * </p>
36 *
37 * @author Patrick Harms
38 */
39public class HttpMonitoringProxy implements HttpMonitorComponent {
40
41    /**  */
42    private static final long serialVersionUID = 1L;
43
44    /**
45     * the port on which the webserver shall listen.
46     */
47    private int port;
48   
49    /**
50     * the web server receiving the log messages
51     */
52    private HttpMonitorServer server;
53   
54    /**
55     * the web server receiving the log messages
56     */
57    private ExchangeListenerManager exchangeListenerManager;
58   
59    /**
60     *
61     */
62    private HttpMonitorExchangeHandler exchangeHandler;
63   
64    /**
65     * the directory into which the log files shall be written
66     */
67    private String logFileBaseDir;
68
69    /**
70     * the thread needed to handle CTRL-C events
71     */
72    private Thread shutdownHook;
73
74    /** */
75    private String proxiedServer;
76   
77    /** */
78    private int proxiedPort;
79   
80    /** */
81    private String httpMonitorServer;
82   
83    /** */
84    private int httpMonitorPort;
85   
86    /**
87     * <p>
88     * initializes the monitor with the command line arguments. Those may be the log directory
89     * as first argument and the port to listen on as second
90     * </p>
91     *
92     * @param commandLineArguments the command line arguments when starting the monitor using
93     *                             the {@link Runner}
94     */
95    public HttpMonitoringProxy(String[] commandLineArguments) {
96        if (commandLineArguments.length != 4) {
97            Console.printerrln("invalid number of command line parameters. Three are required:");
98            Console.printerrln("  1. the port on which to listen");
99            Console.printerrln("  2. the server and port to be proxied (format \"host:port\")");
100            Console.printerrln
101                ("  3. variant a: the value \"local\" for logging recorded exchanges locally");
102            Console.printerrln
103                ("     variant b: the server and port of the HTTP monitor to send the recorded\n" +
104                 "                exchanges to (format \"host:port\")");
105            throw new IllegalArgumentException();
106        }
107       
108        this.logFileBaseDir = commandLineArguments[0];
109
110        try {
111            this.port = Integer.parseInt(commandLineArguments[1]);
112        }
113        catch (NumberFormatException e) {
114            Console.printerrln("invalid port specification " + commandLineArguments[1]);
115        }
116        Console.println("listening on port " + this.port);
117
118        proxiedServer = commandLineArguments[2];
119       
120        int index = proxiedServer.indexOf(':');
121        if (index > -1) {
122            try {
123                proxiedPort = Integer.parseInt(proxiedServer.substring(index + 1));
124            }
125            catch (NumberFormatException e) {
126                Console.printerrln
127                    ("invalid port specification " + proxiedServer.substring(index + 1));
128            }
129           
130            proxiedServer = proxiedServer.substring(0, index);
131        }
132        else {
133            proxiedPort = 80;
134        }
135       
136        Console.println("proxing " + proxiedServer + ":" + proxiedPort);
137       
138        httpMonitorServer = commandLineArguments[3];
139       
140        index = httpMonitorServer.indexOf(':');
141        if (index > -1) {
142            try {
143                httpMonitorPort = Integer.parseInt(httpMonitorServer.substring(index + 1));
144            }
145            catch (NumberFormatException e) {
146                Console.printerrln
147                    ("invalid port specification " + httpMonitorServer.substring(index + 1));
148            }
149           
150            httpMonitorServer = httpMonitorServer.substring(0, index);
151        }
152        else if ("local".equals(httpMonitorServer)) {
153            httpMonitorServer = null;
154        }
155        else {
156            httpMonitorPort = 80;
157        }
158       
159        if (httpMonitorServer != null) {
160            Console.println("sending log data to " + httpMonitorServer + ":" + httpMonitorPort);
161            exchangeHandler =
162                new HttpMonitorRemoteExchangeHandler(httpMonitorServer, httpMonitorPort);
163        }
164        else {
165            Console.println("storing logs locally into directory " + logFileBaseDir);
166            exchangeHandler = new HttpMonitorLogManager(logFileBaseDir);
167        }
168    }
169
170    /* (non-Javadoc)
171     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init()
172     */
173    @Override
174    public synchronized void init() throws HttpMonitorException {
175        if (server != null) {
176            throw new IllegalStateException("already initialized.");
177        }
178       
179        exchangeHandler.init();
180       
181        exchangeListenerManager = new ExchangeListenerManager(exchangeHandler);
182        exchangeListenerManager.init();
183       
184        Servlet servlet =
185            new HttpMonitoringProxyServlet(proxiedServer, proxiedPort, exchangeListenerManager);
186        server = new HttpMonitorServer(port, servlet);
187        server.init();
188     
189        shutdownHook = new Thread
190            (new ShutdownHook(server, exchangeListenerManager, exchangeHandler));
191    }
192
193    /* (non-Javadoc)
194     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start()
195     */
196    @Override
197    public synchronized void start() {
198        if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) {
199            throw new IllegalStateException("not initialized.");
200        }
201       
202        try {
203            Runtime.getRuntime().addShutdownHook(shutdownHook);
204            exchangeHandler.start();
205            exchangeListenerManager.start();
206            server.start();
207        }
208        catch (HttpMonitorException e) {
209            Console.printerrln("could not start HTTP monitoring proxy: " + e);
210            Console.logException(e);
211        }
212    }
213
214    /* (non-Javadoc)
215     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop()
216     */
217    @Override
218    public synchronized void stop() {
219        if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) {
220            throw new IllegalStateException("not initialized.");
221        }
222       
223        Runtime.getRuntime().removeShutdownHook(shutdownHook);
224        server.stop();
225        exchangeListenerManager.stop();
226        exchangeHandler.stop();
227       
228        server = null;
229        exchangeListenerManager = null;
230        exchangeHandler = null;
231    }
232
233}
Note: See TracBrowser for help on using the repository browser.