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.genericeventmonitor; |
---|
16 | |
---|
17 | import de.ugoe.cs.util.console.Console; |
---|
18 | |
---|
19 | /** |
---|
20 | * <p> |
---|
21 | * The generic event monitor starts a web server ({@link GenericEventMonitorServer}) that receives |
---|
22 | * events in JSON format. These events are logged using the {@link GenericEventMonitorLogManager}. |
---|
23 | * The class assures that on shutdown e.g. caused by CTRL-C the server and the log manager are |
---|
24 | * stopped correctly. |
---|
25 | * </p> |
---|
26 | * |
---|
27 | * @author Patrick Harms |
---|
28 | */ |
---|
29 | public class GenericEventMonitor implements GenericEventMonitorComponent { |
---|
30 | |
---|
31 | /** |
---|
32 | * the port on which the webserver shall listen. Defaults to 8090. |
---|
33 | */ |
---|
34 | private int port = 8090; |
---|
35 | |
---|
36 | /** |
---|
37 | * the web server receiving the log messages |
---|
38 | */ |
---|
39 | private GenericEventMonitorServer server; |
---|
40 | |
---|
41 | /** |
---|
42 | * the directory into which the log files shall be written |
---|
43 | */ |
---|
44 | private String logFileBaseDir; |
---|
45 | |
---|
46 | /** |
---|
47 | * the log manager being responsible for performing the logging |
---|
48 | */ |
---|
49 | private GenericEventMonitorLogManager logManager; |
---|
50 | |
---|
51 | /** |
---|
52 | * the thread needed to handle CTRL-C events |
---|
53 | */ |
---|
54 | private Thread shutdownHook; |
---|
55 | |
---|
56 | /** |
---|
57 | * <p> |
---|
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 |
---|
60 | * </p> |
---|
61 | * |
---|
62 | * @param commandLineArguments the command line arguments when starting the monitor using |
---|
63 | * the {@link Runner} |
---|
64 | */ |
---|
65 | public GenericEventMonitor(String[] commandLineArguments) { |
---|
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.genericeventmonitor.GenericEventMonitorComponent#init() |
---|
84 | */ |
---|
85 | @Override |
---|
86 | public synchronized void init() throws GenericEventMonitorException { |
---|
87 | if (server != null) { |
---|
88 | throw new IllegalStateException("already initialized."); |
---|
89 | } |
---|
90 | |
---|
91 | try { |
---|
92 | logManager = new GenericEventMonitorLogManager(logFileBaseDir); |
---|
93 | logManager.init(); |
---|
94 | |
---|
95 | server = new GenericEventMonitorServer(port, logManager); |
---|
96 | server.init(); |
---|
97 | |
---|
98 | shutdownHook = new Thread(new ShutdownHook(server, logManager)); |
---|
99 | } |
---|
100 | catch (GenericEventMonitorException e) { |
---|
101 | Console.printerrln("could not initialize generic event monitor: " + e); |
---|
102 | Console.logException(e); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /* (non-Javadoc) |
---|
107 | * @see de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorComponent#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 (GenericEventMonitorException e) { |
---|
121 | Console.printerrln("could not start generic event monitor: " + e); |
---|
122 | Console.logException(e); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /* (non-Javadoc) |
---|
127 | * @see de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorComponent#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 | } |
---|