1 /**
2 * Copyright 2006 Matthew Purland (m.purland@gmail.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.mud4j.web.server;
17
18 import java.io.IOException;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.mortbay.jetty.Connector;
25 import org.mortbay.jetty.Handler;
26 import org.mortbay.jetty.Request;
27 import org.mortbay.jetty.Server;
28 import org.mortbay.jetty.handler.AbstractHandler;
29 import org.mortbay.jetty.nio.SelectChannelConnector;
30 import org.mortbay.thread.BoundedThreadPool;
31
32 /**
33 * Provide a Jetty webserver that will use classes on the classpath or as
34 * specified to configure the container.
35 *
36 * @author Matthew Purland
37 */
38 public class JettyWebServer implements WebServer {
39
40
41 private Server server;
42
43
44 private int port;
45
46 /**
47 * Constructor to provide configurable port listener.
48 *
49 * @param port Port to listen on.
50 */
51 public JettyWebServer(int port) {
52 this.port = port;
53 }
54
55 /**
56 * Configure server servlets and configurations.
57 *
58 * @throws IOException in case of Jetty configuration setup problem
59 */
60 protected void configureAndStartServer() throws IOException {
61 try {
62 server = new Server();
63 Connector connector = new SelectChannelConnector();
64 connector.setPort(port);
65
66
67 BoundedThreadPool threadPool = new BoundedThreadPool();
68 threadPool.setMaxThreads(100);
69 server.setThreadPool(threadPool);
70
71 Handler handler = new AbstractHandler()
72 {
73
74 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
75 response.setContentType("text/html");
76 response.setStatus(HttpServletResponse.SC_OK);
77 response.getWriter().println("<h1>testing</h1>");
78 ((Request)request).setHandled(true);
79
80 }
81 };
82 server.setHandler(handler);
83
84 server.setStopAtShutdown(true);
85 server.setConnectors(new Connector[] { connector });
86
87 server.start();
88
89 }
90 catch (Exception e) {
91 throw new IOException(e.getMessage());
92 }
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 public void start() throws IOException {
99 configureAndStartServer();
100 }
101
102 /**
103 * {@inheritDoc}
104 */
105 public void stop() throws IOException {
106 if (server != null) {
107 try {
108 server.stop();
109 }
110 catch (Exception e) {
111 throw new IOException(e.getMessage());
112 }
113 }
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 public boolean isStarted() {
120 return server.isStarted();
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public boolean isStopped() {
127 return server.isStopped();
128 }
129
130 }