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

Last change on this file since 1381 was 1381, 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    private static final long serialVersionUID = 1L;
38
39    /**
40     * the port to listen on
41     */
42    private int port;
43
44    /**
45     * the jetty web server used for receiving messages
46     */
47    private Server server;
48
49    /**
50     * the message listener to forward the messages to
51     */
52    private Servlet servlet;
53
54    /**
55     * <p>
56     * initializes the server with the port to listen on and the servlet to be deployed.
57     * </p>
58     *
59     * @param port    the port to listen on
60     * @param servlet teh servlet to be deployed
61     */
62    public HttpMonitorServer(int port, Servlet servlet) {
63        this.port = port;
64        this.servlet = servlet;
65    }
66
67    /* (non-Javadoc)
68     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init()
69     */
70    @Override
71    public synchronized void init() {
72        if (server != null) {
73            throw new IllegalStateException("already initialized. First call stop()");
74        }
75
76        server = new Server(port);
77       
78        /*
79        // the following code can be used to support SSL directly
80        server = new Server();
81       
82        SslSocketConnector connector = new SslSocketConnector();
83        connector.setPort(port);
84        connector.setKeystore("data/keystore");
85        connector.setPassword("123456");
86        connector.setKeyPassword("123456");
87        connector.setTruststore("data/keystore");
88        connector.setTrustPassword("123456");
89        server.addConnector(connector);*/
90
91        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
92        context.setContextPath("/");
93        server.setHandler(context);
94 
95        context.addServlet(new ServletHolder(servlet), "/*");
96    }
97
98    /* (non-Javadoc)
99     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start()
100     */
101    @Override
102    public synchronized void start() throws HttpMonitorException {
103        if (server == null) {
104            throw new IllegalStateException("server not initialized yet. First call init()");
105        }
106       
107        try {
108            server.start();
109        }
110        catch (Exception e) {
111            throw new HttpMonitorException("could not start server", e);
112        }
113    }
114
115
116    /* (non-Javadoc)
117     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop()
118     */
119    @Override
120    public synchronized void stop() {
121        try {
122            if (server != null) {
123                server.stop();
124            }
125        }
126        catch (Exception e) {
127            Console.printerrln("could not stop server: " + e.getMessage());
128            Console.logException(e);
129        }
130    }
131
132}
Note: See TracBrowser for help on using the repository browser.