source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorServer.java @ 1384

Last change on this file since 1384 was 1384, checked in by pharms, 10 years ago
  • removed find bugs warning
File size: 3.8 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.httpmonitor;
16
17import javax.servlet.Servlet;
18
19import org.eclipse.jetty.server.Server;
20import org.eclipse.jetty.servlet.ServletContextHandler;
21import org.eclipse.jetty.servlet.ServletHolder;
22
23import de.ugoe.cs.util.console.Console;
24
25/**
26 * <p>
27 * this is the web server, that is used either for the monitor or the proxy. It is initialized with
28 * a port and the servlet to be deployed. It implements the {@link HttpMonitorComponent} to be
29 * simply initialized, started, and stopped.
30 * </p>
31 *
32 * @author Patrick Harms
33 */
34public class HttpMonitorServer implements HttpMonitorComponent {
35
36    /**
37     * the port to listen on
38     */
39    private int port;
40
41    /**
42     * the jetty web server used for receiving messages
43     */
44    private Server server;
45
46    /**
47     * the message listener to forward the messages to
48     */
49    private Servlet servlet;
50
51    /**
52     * <p>
53     * initializes the server with the port to listen on and the servlet to be deployed.
54     * </p>
55     *
56     * @param port    the port to listen on
57     * @param servlet teh servlet to be deployed
58     */
59    public HttpMonitorServer(int port, Servlet servlet) {
60        this.port = port;
61        this.servlet = servlet;
62    }
63
64    /* (non-Javadoc)
65     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init()
66     */
67    @Override
68    public synchronized void init() {
69        if (server != null) {
70            throw new IllegalStateException("already initialized. First call stop()");
71        }
72
73        server = new Server(port);
74       
75        /*
76        // the following code can be used to support SSL directly
77        server = new Server();
78       
79        SslSocketConnector connector = new SslSocketConnector();
80        connector.setPort(port);
81        connector.setKeystore("data/keystore");
82        connector.setPassword("123456");
83        connector.setKeyPassword("123456");
84        connector.setTruststore("data/keystore");
85        connector.setTrustPassword("123456");
86        server.addConnector(connector);*/
87
88        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
89        context.setContextPath("/");
90        server.setHandler(context);
91 
92        context.addServlet(new ServletHolder(servlet), "/*");
93    }
94
95    /* (non-Javadoc)
96     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start()
97     */
98    @Override
99    public synchronized void start() throws HttpMonitorException {
100        if (server == null) {
101            throw new IllegalStateException("server not initialized yet. First call init()");
102        }
103       
104        try {
105            server.start();
106        }
107        catch (Exception e) {
108            throw new HttpMonitorException("could not start server", e);
109        }
110    }
111
112
113    /* (non-Javadoc)
114     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop()
115     */
116    @Override
117    public synchronized void stop() {
118        try {
119            if (server != null) {
120                server.stop();
121            }
122        }
123        catch (Exception e) {
124            Console.printerrln("could not stop server: " + e.getMessage());
125            Console.logException(e);
126        }
127    }
128
129}
Note: See TracBrowser for help on using the repository browser.