source: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitor.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 4.4 KB
Line 
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
15package de.ugoe.cs.autoquest.htmlmonitor;
16
17import de.ugoe.cs.util.console.Console;
18
19/**
20 * <p>
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.
25 * </p>
26 *
27 * @author Patrick Harms
28 */
29public class HtmlMonitor implements HtmlMonitorComponent {
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 HtmlMonitorServer 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 HtmlMonitorLogManager 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 HtmlMonitor(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.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}
Note: See TracBrowser for help on using the repository browser.