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 | |
---|
15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
16 | |
---|
17 | import javax.servlet.Servlet; |
---|
18 | |
---|
19 | import de.ugoe.cs.util.console.Console; |
---|
20 | |
---|
21 | /** |
---|
22 | * <p> |
---|
23 | * The HTTP monitor starts a web server ({@link HttpMonitorServer}) that receives exchanges |
---|
24 | * of the HTTP proxy. In addition, it provides via get a unique ordering id for requests and |
---|
25 | * responses that can be used by the proxies when creating the exchanges. The exchanges are |
---|
26 | * logged using the {@link HttpMonitorLogManager}. The class assures that on shutdown e.g. caused |
---|
27 | * by CTRL-C the server and the log manager are stopped correctly. |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @author Patrick Harms |
---|
31 | */ |
---|
32 | public class HttpMonitor implements HttpMonitorComponent { |
---|
33 | |
---|
34 | /** |
---|
35 | * the port on which the webserver shall listen. Defaults to 8090. |
---|
36 | */ |
---|
37 | private int port = 8090; |
---|
38 | |
---|
39 | /** |
---|
40 | * the web server receiving the exchanges |
---|
41 | */ |
---|
42 | private HttpMonitorServer server; |
---|
43 | |
---|
44 | /** |
---|
45 | * the directory into which the log files shall be written |
---|
46 | */ |
---|
47 | private String logFileBaseDir; |
---|
48 | |
---|
49 | /** |
---|
50 | * the log manager being responsible for performing the logging |
---|
51 | */ |
---|
52 | private HttpMonitorLogManager logManager; |
---|
53 | |
---|
54 | /** |
---|
55 | * the id generator used for getting new unique ids |
---|
56 | */ |
---|
57 | private IdGenerator idGenerator; |
---|
58 | |
---|
59 | /** |
---|
60 | * the thread needed to handle CTRL-C events |
---|
61 | */ |
---|
62 | private Thread shutdownHook; |
---|
63 | |
---|
64 | /** |
---|
65 | * <p> |
---|
66 | * initializes the monitor with the command line arguments. Those may be the log directory |
---|
67 | * as first argument and the port to listen on as second |
---|
68 | * </p> |
---|
69 | * |
---|
70 | * @param commandLineArguments the command line arguments when starting the monitor using |
---|
71 | * the {@link Runner} |
---|
72 | */ |
---|
73 | public HttpMonitor(String[] commandLineArguments) { |
---|
74 | if (commandLineArguments.length > 0) { |
---|
75 | this.logFileBaseDir = commandLineArguments[0]; |
---|
76 | Console.println("putting logs into directory " + this.logFileBaseDir); |
---|
77 | } |
---|
78 | |
---|
79 | if (commandLineArguments.length > 1) { |
---|
80 | try { |
---|
81 | this.port = Integer.parseInt(commandLineArguments[1]); |
---|
82 | } |
---|
83 | catch (NumberFormatException e) { |
---|
84 | Console.println("ignoring invalid port specification " + commandLineArguments[1]); |
---|
85 | } |
---|
86 | Console.println("listening on port " + this.port); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /* (non-Javadoc) |
---|
91 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init() |
---|
92 | */ |
---|
93 | @Override |
---|
94 | public synchronized void init() throws HttpMonitorException { |
---|
95 | if (server != null) { |
---|
96 | throw new IllegalStateException("already initialized."); |
---|
97 | } |
---|
98 | |
---|
99 | try { |
---|
100 | idGenerator = new SimpleIdGenerator(); |
---|
101 | idGenerator.init(); |
---|
102 | |
---|
103 | logManager = new HttpMonitorLogManager(logFileBaseDir); |
---|
104 | logManager.init(); |
---|
105 | |
---|
106 | Servlet servlet = new HttpMonitorServlet(logManager, idGenerator); |
---|
107 | server = new HttpMonitorServer(port, servlet); |
---|
108 | server.init(); |
---|
109 | |
---|
110 | shutdownHook = new Thread(new ShutdownHook(server, logManager)); |
---|
111 | } |
---|
112 | catch (HttpMonitorException e) { |
---|
113 | Console.printerrln("could not initialize HTTP monitor: " + e); |
---|
114 | Console.logException(e); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /* (non-Javadoc) |
---|
119 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start() |
---|
120 | */ |
---|
121 | @Override |
---|
122 | public synchronized void start() { |
---|
123 | if (server == null) { |
---|
124 | throw new IllegalStateException("not initialized."); |
---|
125 | } |
---|
126 | |
---|
127 | try { |
---|
128 | Runtime.getRuntime().addShutdownHook(shutdownHook); |
---|
129 | idGenerator.start(); |
---|
130 | logManager.start(); |
---|
131 | server.start(); |
---|
132 | } |
---|
133 | catch (HttpMonitorException e) { |
---|
134 | Console.printerrln("could not start HTTP monitor: " + e); |
---|
135 | Console.logException(e); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /* (non-Javadoc) |
---|
140 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop() |
---|
141 | */ |
---|
142 | @Override |
---|
143 | public synchronized void stop() { |
---|
144 | if (server == null) { |
---|
145 | throw new IllegalStateException("not initialized."); |
---|
146 | } |
---|
147 | |
---|
148 | Runtime.getRuntime().removeShutdownHook(shutdownHook); |
---|
149 | server.stop(); |
---|
150 | logManager.stop(); |
---|
151 | idGenerator.stop(); |
---|
152 | |
---|
153 | server = null; |
---|
154 | logManager = null; |
---|
155 | idGenerator = null; |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|