001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.httpd;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.Socket;
022: import java.util.Properties;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.openejb.server.ServiceException;
030: import org.mortbay.jetty.Connector;
031: import org.mortbay.jetty.Handler;
032: import org.mortbay.jetty.Request;
033: import org.mortbay.jetty.Server;
034: import org.mortbay.jetty.handler.AbstractHandler;
035: import org.mortbay.jetty.handler.ContextHandler;
036: import org.mortbay.jetty.nio.SelectChannelConnector;
037:
038: /**
039: * Jetty based http server implementation
040: */
041: public class JettyHttpServer implements HttpServer {
042: private final HttpListener listener;
043: private Server server;
044: private int port;
045:
046: public JettyHttpServer() {
047: this (OpenEJBHttpServer.getHttpListenerRegistry());
048: }
049:
050: public JettyHttpServer(HttpListener listener) {
051: this .listener = listener;
052: }
053:
054: public HttpListener getListener() {
055: return listener;
056: }
057:
058: public void service(Socket socket) throws ServiceException,
059: IOException {
060: throw new UnsupportedOperationException();
061: }
062:
063: public void service(InputStream in, OutputStream out)
064: throws ServiceException, IOException {
065: throw new UnsupportedOperationException();
066: }
067:
068: public String getName() {
069: return "jetty";
070: }
071:
072: public int getPort() {
073: return port;
074: }
075:
076: public String getIP() {
077: return "0.0.0.0";
078: }
079:
080: public void init(Properties props) throws Exception {
081: port = Integer.parseInt(props.getProperty("port", "8080"));
082:
083: // Create all the Jetty objects but dont' start them
084: server = new Server();
085: Connector connector = new SelectChannelConnector();
086: connector.setPort(port);
087: server.setConnectors(new Connector[] { connector });
088:
089: ContextHandler context = new ContextHandler();
090: context.setContextPath("/");
091: final ServletContext servletContext = context
092: .getServletContext();
093: server.setHandler(context);
094:
095: Handler handler = new AbstractHandler() {
096: public void handle(String target, HttpServletRequest req,
097: HttpServletResponse res, int dispatch)
098: throws IOException, ServletException {
099: try {
100: ((Request) req).setHandled(true);
101: HttpRequest httpRequest = new ServletRequestAdapter(
102: req, res, servletContext);
103: HttpResponse httpResponse = new ServletResponseAdapter(
104: res);
105: JettyHttpServer.this .listener.onMessage(
106: httpRequest, httpResponse);
107: } catch (IOException e) {
108: throw e;
109: } catch (ServletException e) {
110: throw e;
111: } catch (Exception e) {
112: throw new ServletException(e);
113: }
114: }
115: };
116:
117: context.setHandler(handler);
118: }
119:
120: public void start() throws ServiceException {
121: try {
122: server.start();
123: } catch (Exception e) {
124: throw new ServiceException(e);
125: }
126: }
127:
128: public void stop() throws ServiceException {
129: try {
130: server.stop();
131: } catch (Exception e) {
132: throw new ServiceException(e);
133: }
134: }
135: }
|