Coverage Report - net.sf.mud4j.web.server.JettyWebServer
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyWebServer
100%
24/24
N/A
1.714
JettyWebServer$1
100%
1/1
N/A
1.714
 
 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  
     // Jetty server
 41  
     private Server server;
 42  
     
 43  
     // Port to listen on
 44  
     private int port;
 45  
 
 46  
     /**
 47  
      * Constructor to provide configurable port listener.
 48  
      * 
 49  
      * @param port Port to listen on.
 50  
      */
 51  2
     public JettyWebServer(int port) {
 52  2
         this.port = port;
 53  2
     }
 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  2
             server = new Server();
 63  2
             Connector connector = new SelectChannelConnector();
 64  2
             connector.setPort(port);
 65  
             
 66  
             // Create a thread pool to control number of threads for requests
 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  
             //server.join();
 89  
         }
 90  
         catch (Exception e) {
 91  
             throw new IOException(e.getMessage());
 92  2
         }
 93  2
     }
 94  
 
 95  
     /**
 96  
      * {@inheritDoc}
 97  
      */
 98  
     public void start() throws IOException {
 99  2
         configureAndStartServer();
 100  2
     }
 101  
 
 102  
     /**
 103  
      * {@inheritDoc}
 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  
      * {@inheritDoc}
 118  
      */
 119  
     public boolean isStarted() {
 120  1
         return server.isStarted();
 121  
     }
 122  
 
 123  
     /**
 124  
      * {@inheritDoc}
 125  
      */
 126  
     public boolean isStopped() {
 127  1
         return server.isStopped();
 128  
     }
 129  
 
 130  
 }