001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.web.micro.base;
028:
029: import java.io.InputStream;
030: import java.io.IOException;
031: import java.io.OutputStream;
032: import java.net.BindException;
033: import java.util.Collections;
034: import java.util.Map;
035:
036: import javax.servlet.Servlet;
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletContext;
039: import javax.servlet.ServletException;
040: import javax.servlet.ServletInputStream;
041: import javax.servlet.ServletOutputStream;
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044:
045: /**
046: * A servlet engine backed by our {@link ServerFactory} support.
047: */
048: public class ServletEngineImpl implements ServletEngine {
049:
050: private final ServerFactory factory;
051:
052: private Map settings;
053: private boolean running;
054: private ServerFactory.ListenerControl controller;
055: private Servlet servlet;
056:
057: public ServletEngineImpl() {
058: this (new SocketServerFactory());
059: }
060:
061: public ServletEngineImpl(ServerFactory factory) {
062: this .factory = factory;
063: if (factory == null) {
064: throw new IllegalArgumentException("null factory");
065: }
066: }
067:
068: /** A minimal command-line test. */
069: public static void main(String[] args) throws Exception {
070: int httpPort;
071: try {
072: httpPort = Integer.parseInt(args[0]);
073: } catch (Exception e) {
074: System.err.println("Usage: HTTP_PORT");
075: return;
076: }
077:
078: ServletEngine engine = new ServletEngineImpl();
079: engine.configure(httpPort, -1, null);
080: engine.start();
081: engine.setGateway(new SnoopServlet());
082: }
083:
084: //
085: // ServletEngine API:
086: //
087:
088: public void configure(int httpPort, int httpsPort, Map options) {
089: this .settings = Collections.singletonMap("port", Integer
090: .toString(httpPort));
091: }
092:
093: public void start() throws BindException, IOException {
094: running = true;
095:
096: ServerFactory.AcceptCallback callback = new ServerFactory.AcceptCallback() {
097: public void accept(Connection con) throws IOException {
098: ServletEngineImpl.this .accept(con);
099: }
100: };
101: controller = factory.listen(settings, callback);
102: }
103:
104: public boolean isRunning() {
105: return running;
106: }
107:
108: public void setGateway(Servlet s) {
109: this .servlet = s;
110: if (s == null)
111: return;
112: // init servlet
113: try {
114: // TODO take name & params from options
115: String name = "cougaar";
116: Map params = Collections.EMPTY_MAP;
117: ServletContext context = new ServletContextImpl();
118: ServletConfig config = new ServletConfigImpl(name, context,
119: params);
120: s.init(config);
121: } catch (Exception e) {
122: throw new RuntimeException("Unable to init servlet", e);
123: }
124: }
125:
126: public void stop() {
127: if (controller != null) {
128: controller.stop();
129: controller = null;
130: }
131: }
132:
133: //
134: // impl:
135: //
136:
137: private void accept(Connection con) throws IOException {
138: try {
139: // read request
140: InputStream is = con.getInputStream();
141: HttpServletRequest req = new HttpServletRequestImpl(
142: new RequestCallbackImpl(is), con.getMetaData());
143:
144: // prepare response
145: OutputStream out = con.getOutputStream();
146: ResponseCallback rc = new ResponseCallbackImpl(out);
147: HttpServletResponse res = new HttpServletResponseImpl(rc);
148:
149: // invoke servlet
150: try {
151: servlet.service(req, res);
152: } catch (ServletException se) {
153: throw new RuntimeException("Servlet threw exception",
154: se);
155: }
156:
157: // flush response
158: rc.finishResponse();
159: } finally {
160: con.close();
161: }
162: }
163: }
|