1 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
2 | |
---|
3 | import de.ugoe.cs.util.console.Console; |
---|
4 | |
---|
5 | /** |
---|
6 | * <p> |
---|
7 | * TODO comment |
---|
8 | * </p> |
---|
9 | * |
---|
10 | * @author Patrick Harms |
---|
11 | */ |
---|
12 | public class HtmlMonitor implements HtmlMonitorComponent { |
---|
13 | |
---|
14 | /** */ |
---|
15 | private int port = 8090; |
---|
16 | |
---|
17 | /** */ |
---|
18 | private HtmlMonitorServer server; |
---|
19 | |
---|
20 | /** */ |
---|
21 | private String logFileBaseDir; |
---|
22 | |
---|
23 | /** */ |
---|
24 | private HtmlMonitorLogManager logManager; |
---|
25 | |
---|
26 | /** */ |
---|
27 | private Thread shutdownHook; |
---|
28 | |
---|
29 | /** |
---|
30 | * <p> |
---|
31 | * TODO: comment |
---|
32 | * </p> |
---|
33 | * |
---|
34 | * @param logFileBaseDir |
---|
35 | */ |
---|
36 | HtmlMonitor(String[] commandLineArguments) { |
---|
37 | if (commandLineArguments.length > 0) { |
---|
38 | this.logFileBaseDir = commandLineArguments[0]; |
---|
39 | Console.println("putting logs into directory " + this.logFileBaseDir); |
---|
40 | } |
---|
41 | |
---|
42 | if (commandLineArguments.length > 1) { |
---|
43 | try { |
---|
44 | this.port = Integer.parseInt(commandLineArguments[1]); |
---|
45 | } |
---|
46 | catch (NumberFormatException e) { |
---|
47 | Console.println("ignoring invalid port specification " + commandLineArguments[1]); |
---|
48 | } |
---|
49 | Console.println("listening on port " + this.port); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /* (non-Javadoc) |
---|
54 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init() |
---|
55 | */ |
---|
56 | @Override |
---|
57 | public synchronized void init() throws HtmlMonitorException { |
---|
58 | if (server != null) { |
---|
59 | throw new IllegalStateException("already initialized."); |
---|
60 | } |
---|
61 | |
---|
62 | try { |
---|
63 | logManager = new HtmlMonitorLogManager(logFileBaseDir); |
---|
64 | logManager.init(); |
---|
65 | |
---|
66 | server = new HtmlMonitorServer(port, logManager); |
---|
67 | server.init(); |
---|
68 | |
---|
69 | shutdownHook = new Thread(new ShutdownHook(server, logManager)); |
---|
70 | } |
---|
71 | catch (HtmlMonitorException e) { |
---|
72 | Console.printerrln("could not initialize HTML monitor: " + e); |
---|
73 | Console.logException(e); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /* (non-Javadoc) |
---|
78 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start() |
---|
79 | */ |
---|
80 | @Override |
---|
81 | public synchronized void start() { |
---|
82 | if (server == null) { |
---|
83 | throw new IllegalStateException("not initialized."); |
---|
84 | } |
---|
85 | |
---|
86 | try { |
---|
87 | Runtime.getRuntime().addShutdownHook(shutdownHook); |
---|
88 | logManager.start(); |
---|
89 | server.start(); |
---|
90 | } |
---|
91 | catch (HtmlMonitorException e) { |
---|
92 | Console.printerrln("could not start HTML monitor: " + e); |
---|
93 | Console.logException(e); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /* (non-Javadoc) |
---|
98 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop() |
---|
99 | */ |
---|
100 | @Override |
---|
101 | public synchronized void stop() { |
---|
102 | if (server == null) { |
---|
103 | throw new IllegalStateException("not initialized."); |
---|
104 | } |
---|
105 | |
---|
106 | Runtime.getRuntime().removeShutdownHook(shutdownHook); |
---|
107 | server.stop(); |
---|
108 | logManager.stop(); |
---|
109 | |
---|
110 | server = null; |
---|
111 | logManager = null; |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|