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