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