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