| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class JettyWebServer implements WebServer { |
| 39 | |
|
| 40 | |
|
| 41 | |
private Server server; |
| 42 | |
|
| 43 | |
|
| 44 | |
private int port; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 2 | public JettyWebServer(int port) { |
| 52 | 2 | this.port = port; |
| 53 | 2 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
protected void configureAndStartServer() throws IOException { |
| 61 | |
try { |
| 62 | 2 | server = new Server(); |
| 63 | 2 | Connector connector = new SelectChannelConnector(); |
| 64 | 2 | connector.setPort(port); |
| 65 | |
|
| 66 | |
|
| 67 | 2 | BoundedThreadPool threadPool = new BoundedThreadPool(); |
| 68 | 2 | threadPool.setMaxThreads(100); |
| 69 | 2 | server.setThreadPool(threadPool); |
| 70 | |
|
| 71 | 2 | Handler handler = new AbstractHandler() |
| 72 | |
{ |
| 73 | |
|
| 74 | 2 | 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 | 2 | server.setHandler(handler); |
| 83 | |
|
| 84 | 2 | server.setStopAtShutdown(true); |
| 85 | 2 | server.setConnectors(new Connector[] { connector }); |
| 86 | |
|
| 87 | 2 | server.start(); |
| 88 | |
|
| 89 | |
} |
| 90 | |
catch (Exception e) { |
| 91 | |
throw new IOException(e.getMessage()); |
| 92 | 2 | } |
| 93 | 2 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public void start() throws IOException { |
| 99 | 2 | configureAndStartServer(); |
| 100 | 2 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public void stop() throws IOException { |
| 106 | 3 | if (server != null) { |
| 107 | |
try { |
| 108 | 3 | server.stop(); |
| 109 | |
} |
| 110 | |
catch (Exception e) { |
| 111 | |
throw new IOException(e.getMessage()); |
| 112 | 3 | } |
| 113 | |
} |
| 114 | 3 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public boolean isStarted() { |
| 120 | 1 | return server.isStarted(); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public boolean isStopped() { |
| 127 | 1 | return server.isStopped(); |
| 128 | |
} |
| 129 | |
|
| 130 | |
} |