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