001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 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: package org.cougaar.lib.web.tomcat;
027:
028: import java.io.IOException;
029: import java.io.PrintWriter;
030:
031: import javax.servlet.Servlet;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletException;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.ServletResponse;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: /**
040: * Servlet that is loaded by both <code>TomcatServletEngine</code>
041: * and Tomcat itself -- this is used to delegate requests within
042: * Tomcat back into the (launching) TomcatServletEngine's
043: * "gateway" Servlet.
044: * <p>
045: * This kluge depends upon a static variable, which means that
046: * only one TomcatServletEngine can run at a time. In talking
047: * to "tomcat-user@jakarta.apache.org" it sounds like this is
048: * the easiest way to embed Tomcat. It's not pretty, but okay
049: * for now.
050: */
051: public final class HookServlet implements Servlet {
052:
053: private static Servlet NO_HOOK_SERVLET = new NoHookServlet();
054: private static Servlet servlet = NO_HOOK_SERVLET;
055: private static ServletConfig config;
056:
057: public HookServlet() {
058: }
059:
060: public void init(ServletConfig newConfig) throws ServletException {
061: if (config != null) {
062: System.err.println("Init when config is non-null!");
063: } else {
064: config = newConfig;
065: servlet.init(config);
066: }
067: }
068:
069: public String getServletInfo() {
070: return "A Hook to the Gateway Servlet";
071: }
072:
073: public ServletConfig getServletConfig() {
074: return config;
075: }
076:
077: public void destroy() {
078: if (config == null) {
079: System.err.println("Destroy when config is null!");
080: } else {
081: servlet.destroy();
082: servlet = NO_HOOK_SERVLET;
083: config = null;
084: }
085: }
086:
087: public void service(ServletRequest req, ServletResponse res)
088: throws ServletException, IOException {
089: servlet.service(req, res);
090: }
091:
092: public static void setServlet(Servlet newServlet)
093: throws ServletException {
094: if (config == null) {
095: // before init or after destroy
096: if (newServlet == null) {
097: // user stops requests
098: servlet = NO_HOOK_SERVLET;
099: } else {
100: // user wants requests
101: servlet = newServlet;
102: }
103: } else {
104: // running
105: if (newServlet == null) {
106: // user stops requests
107: servlet.destroy();
108: servlet = NO_HOOK_SERVLET;
109: servlet.init(config);
110: } else {
111: // user wants requests
112: newServlet.init(config);
113: servlet.destroy();
114: servlet = newServlet;
115: }
116: }
117: }
118:
119: /**
120: * Default servlet when no hook servlet has been configured.
121: */
122: private static final class NoHookServlet implements Servlet {
123: public void service(ServletRequest req, ServletResponse res)
124: throws ServletException, IOException {
125: if (!(req instanceof HttpServletRequest)
126: || !(res instanceof HttpServletResponse)) {
127: throw new ServletException(
128: "non-HTTP request or response");
129: }
130: HttpServletResponse httpRes = (HttpServletResponse) res;
131:
132: // write an error message.
133: //
134: // see "ServletEngine" for notes on disabling Internet Explorer's
135: // "friendly error page" option, which would hide this message.
136: httpRes.setStatus(HttpServletResponse.SC_NOT_FOUND);
137:
138: httpRes.setContentType("text/html");
139:
140: String msg = "<html><body>"
141: + "<b>Internal error</b>: Tomcat \"hook\" not configured yet."
142: + "</body></html>";
143: httpRes.setContentLength(msg.length());
144: PrintWriter out = httpRes.getWriter();
145: out.println(msg);
146: }
147:
148: private ServletConfig config;
149:
150: public void init(ServletConfig config) {
151: this .config = config;
152: }
153:
154: public ServletConfig getServletConfig() {
155: return config;
156: }
157:
158: public String getServletInfo() {
159: return "no hook";
160: }
161:
162: public void destroy() {
163: config = null;
164: }
165: }
166:
167: }
|