Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » httpd » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.server.httpd;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   import java.io.OutputStream;
   22   import java.net.Socket;
   23   import java.util.Properties;
   24   
   25   import javax.servlet.ServletContext;
   26   import javax.servlet.ServletException;
   27   import javax.servlet.http.HttpServletRequest;
   28   import javax.servlet.http.HttpServletResponse;
   29   
   30   import org.apache.openejb.server.ServiceException;
   31   import org.apache.openejb.loader.Options;
   32   import org.mortbay.jetty.Connector;
   33   import org.mortbay.jetty.Handler;
   34   import org.mortbay.jetty.Request;
   35   import org.mortbay.jetty.Server;
   36   import org.mortbay.jetty.handler.AbstractHandler;
   37   import org.mortbay.jetty.handler.ContextHandler;
   38   import org.mortbay.jetty.nio.SelectChannelConnector;
   39   
   40   /**
   41    * Jetty based http server implementation
   42    */
   43   public class JettyHttpServer implements HttpServer {
   44       private final HttpListener listener;
   45       private Server server;
   46       private int port;
   47   
   48       public JettyHttpServer() {
   49           this(OpenEJBHttpServer.getHttpListenerRegistry());
   50       }
   51   
   52       public JettyHttpServer(HttpListener listener) {
   53           this.listener = listener;
   54       }
   55   
   56       public HttpListener getListener() {
   57           return listener;
   58       }
   59   
   60       public void service(Socket socket) throws ServiceException, IOException {
   61           throw new UnsupportedOperationException();
   62       }
   63   
   64       public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
   65           throw new UnsupportedOperationException();
   66       }
   67   
   68       public String getName() {
   69           return "jetty";
   70       }
   71   
   72       public int getPort() {
   73           return port;
   74       }
   75   
   76       public String getIP() {
   77           return "0.0.0.0";
   78       }
   79   
   80       public void init(Properties props) throws Exception {
   81           Options options = new Options(props);
   82   
   83           port = options.get("port", 8080);
   84           
   85           // Create all the Jetty objects but dont' start them
   86           server = new Server();
   87           Connector connector = new SelectChannelConnector();
   88           connector.setPort(port);
   89           server.setConnectors(new Connector[]{connector});
   90   
   91           ContextHandler context = new ContextHandler();
   92           context.setContextPath("/");
   93           final ServletContext servletContext = context.getServletContext();
   94           server.setHandler(context);
   95   
   96           Handler handler = new AbstractHandler() {
   97               public void handle(String target, HttpServletRequest req, HttpServletResponse res, int dispatch) throws IOException, ServletException {
   98                   try {
   99                       ((Request) req).setHandled(true);
  100                       HttpRequest httpRequest = new ServletRequestAdapter(req, res, servletContext);
  101                       HttpResponse httpResponse = new ServletResponseAdapter(res);
  102                       JettyHttpServer.this.listener.onMessage(httpRequest, httpResponse);
  103                   } catch (IOException e) {
  104                       throw e;
  105                   } catch (ServletException e) {
  106                       throw e;
  107                   } catch (Exception e) {
  108                       throw new ServletException(e);
  109                   }
  110               }
  111           };
  112   
  113           context.setHandler(handler);
  114       }
  115   
  116       public void start() throws ServiceException {
  117           try {
  118               server.start();
  119           } catch (Exception e) {
  120               throw new ServiceException(e);
  121           }
  122       }
  123   
  124       public void stop() throws ServiceException {
  125           try {
  126               server.stop();
  127           } catch (Exception e) {
  128               throw new ServiceException(e);
  129           }
  130       }
  131   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » httpd » [javadoc | source]