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

Last change on this file since 1374 was 1374, checked in by pharms, 10 years ago

Initial import.

File size: 7.7 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     * the port on which the webserver shall listen.
43     */
44    private int port;
45   
46    /**
47     * the web server receiving the log messages
48     */
49    private HttpMonitorServer server;
50   
51    /**
52     * the web server receiving the log messages
53     */
54    private ExchangeListenerManager exchangeListenerManager;
55   
56    /**
57     *
58     */
59    private HttpMonitorExchangeHandler exchangeHandler;
60   
61    /**
62     * the directory into which the log files shall be written
63     */
64    private String logFileBaseDir;
65
66    /**
67     * the thread needed to handle CTRL-C events
68     */
69    private Thread shutdownHook;
70
71    /** */
72    private String proxiedServer;
73   
74    /** */
75    private int proxiedPort;
76   
77    /** */
78    private String httpMonitorServer;
79   
80    /** */
81    private int httpMonitorPort;
82   
83    /**
84     * <p>
85     * initializes the monitor with the command line arguments. Those may be the log directory
86     * as first argument and the port to listen on as second
87     * </p>
88     *
89     * @param commandLineArguments the command line arguments when starting the monitor using
90     *                             the {@link Runner}
91     */
92    public HttpMonitoringProxy(String[] commandLineArguments) {
93        if (commandLineArguments.length != 4) {
94            Console.printerrln("invalid number of command line parameters. Three are required:");
95            Console.printerrln("  1. the port on which to listen");
96            Console.printerrln("  2. the server and port to be proxied (format \"host:port\")");
97            Console.printerrln
98                ("  3. variant a: the value \"local\" for logging recorded exchanges locally");
99            Console.printerrln
100                ("     variant b: the server and port of the HTTP monitor to send the recorded\n" +
101                 "                exchanges to (format \"host:port\")");
102            throw new IllegalArgumentException();
103        }
104       
105        this.logFileBaseDir = commandLineArguments[0];
106
107        try {
108            this.port = Integer.parseInt(commandLineArguments[1]);
109        }
110        catch (NumberFormatException e) {
111            Console.printerrln("invalid port specification " + commandLineArguments[1]);
112        }
113        Console.println("listening on port " + this.port);
114
115        proxiedServer = commandLineArguments[2];
116       
117        int index = proxiedServer.indexOf(':');
118        if (index > -1) {
119            try {
120                proxiedPort = Integer.parseInt(proxiedServer.substring(index + 1));
121            }
122            catch (NumberFormatException e) {
123                Console.printerrln
124                    ("invalid port specification " + proxiedServer.substring(index + 1));
125            }
126           
127            proxiedServer = proxiedServer.substring(0, index);
128        }
129        else {
130            proxiedPort = 80;
131        }
132       
133        Console.println("proxing " + proxiedServer + ":" + proxiedPort);
134       
135        httpMonitorServer = commandLineArguments[3];
136       
137        index = httpMonitorServer.indexOf(':');
138        if (index > -1) {
139            try {
140                httpMonitorPort = Integer.parseInt(httpMonitorServer.substring(index + 1));
141            }
142            catch (NumberFormatException e) {
143                Console.printerrln
144                    ("invalid port specification " + httpMonitorServer.substring(index + 1));
145            }
146           
147            httpMonitorServer = httpMonitorServer.substring(0, index);
148        }
149        else if ("local".equals(httpMonitorServer)) {
150            httpMonitorServer = null;
151        }
152        else {
153            httpMonitorPort = 80;
154        }
155       
156        if (httpMonitorServer != null) {
157            Console.println("sending log data to " + httpMonitorServer + ":" + httpMonitorPort);
158            exchangeHandler =
159                new HttpMonitorRemoteExchangeHandler(httpMonitorServer, httpMonitorPort);
160        }
161        else {
162            Console.println("storing logs locally into directory " + logFileBaseDir);
163            exchangeHandler = new HttpMonitorLogManager(logFileBaseDir);
164        }
165    }
166
167    /* (non-Javadoc)
168     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init()
169     */
170    @Override
171    public synchronized void init() throws HttpMonitorException {
172        if (server != null) {
173            throw new IllegalStateException("already initialized.");
174        }
175       
176        exchangeHandler.init();
177       
178        exchangeListenerManager = new ExchangeListenerManager(exchangeHandler);
179        exchangeListenerManager.init();
180       
181        Servlet servlet =
182            new HttpMonitoringProxyServlet(proxiedServer, proxiedPort, exchangeListenerManager);
183        server = new HttpMonitorServer(port, servlet);
184        server.init();
185     
186        shutdownHook = new Thread
187            (new ShutdownHook(server, exchangeListenerManager, exchangeHandler));
188    }
189
190    /* (non-Javadoc)
191     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start()
192     */
193    @Override
194    public synchronized void start() {
195        if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) {
196            throw new IllegalStateException("not initialized.");
197        }
198       
199        try {
200            Runtime.getRuntime().addShutdownHook(shutdownHook);
201            exchangeHandler.start();
202            exchangeListenerManager.start();
203            server.start();
204        }
205        catch (HttpMonitorException e) {
206            Console.printerrln("could not start HTTP monitoring proxy: " + e);
207            Console.logException(e);
208        }
209    }
210
211    /* (non-Javadoc)
212     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop()
213     */
214    @Override
215    public synchronized void stop() {
216        if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) {
217            throw new IllegalStateException("not initialized.");
218        }
219       
220        Runtime.getRuntime().removeShutdownHook(shutdownHook);
221        server.stop();
222        exchangeListenerManager.stop();
223        exchangeHandler.stop();
224       
225        server = null;
226        exchangeListenerManager = null;
227        exchangeHandler = null;
228    }
229
230}
Note: See TracBrowser for help on using the repository browser.