001: /*
002: * ServletWrapper.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.webserver.servletapi;
054:
055: import java.util.*;
056: import java.io.File;
057: import java.io.IOException;
058: import java.io.PrintStream;
059:
060: import javax.servlet.Servlet;
061: import javax.servlet.ServletConfig;
062: import javax.servlet.ServletContext;
063: import javax.servlet.ServletException;
064: import javax.servlet.SingleThreadModel;
065: import javax.servlet.UnavailableException;
066: import javax.naming.directory.DirContext;
067: import javax.servlet.ServletRequest;
068: import javax.servlet.ServletResponse;
069: import java.beans.PropertyChangeListener;
070:
071: import com.rimfaxe.webserver.seda.SedaHttpResponse;
072: import seda.sandStorm.lib.http.httpRequest;
073:
074: import com.rimfaxe.webserver.*;
075:
076: /**
077: *
078: * @author Lars Andersen
079: */
080: public class ServletWrapper {
081: boolean DEBUG = false;
082:
083: int load_error = 0;
084:
085: RimfaxeServletConfig servletconfig = null;
086: javax.servlet.Servlet servlet = null;
087: String servletclass = "unknown";
088:
089: /** Creates a new instance of ServletWrapper */
090: public ServletWrapper(RimfaxeServletConfig cfg) {
091: this .servletconfig = cfg;
092: }
093:
094: public SedaHttpResponse runservice(httpRequest request,
095: WebContext webcontext) throws ServletException, IOException {
096: RimfaxeHttpServletResponse jRes = null;
097: RimfaxeHttpServletRequest jReq = null;
098:
099: if (DEBUG)
100: System.out
101: .println("ServletWrapper, create new Response Object");
102: jRes = new RimfaxeHttpServletResponse(request, webcontext);
103:
104: if (DEBUG)
105: System.out
106: .println("ServletWrapper, create new Request Object");
107: jReq = new RimfaxeHttpServletRequest(servlet, request, jRes,
108: webcontext);
109:
110: service(jReq, jRes);
111:
112: if (DEBUG)
113: System.out.println("ServletWrapper, return reply");
114: return jRes.getSedaResponse();
115: }
116:
117: public void service(ServletRequest jReq, ServletResponse jRes)
118: throws ServletException, IOException {
119:
120: if (DEBUG)
121: System.out.println("ServletWrapper -> "
122: + jRes.getClass().getName());
123: if (jRes
124: .getClass()
125: .getName()
126: .equalsIgnoreCase(
127: "com.rimfaxe.webserver.servletapi.RimfaxeHttpServletResponse")) {
128: if (DEBUG)
129: System.out
130: .println("ServletWrapper, Assign request to response object");
131: com.rimfaxe.webserver.servletapi.RimfaxeHttpServletResponse rresp = (com.rimfaxe.webserver.servletapi.RimfaxeHttpServletResponse) jRes;
132: rresp
133: .setServletRequest((com.rimfaxe.webserver.servletapi.RimfaxeHttpServletRequest) jReq);
134: }
135:
136: if (DEBUG)
137: System.out
138: .println("ServletWrapper, run service in servlet : "
139: + servletclass);
140:
141: servlet.service(jReq, jRes);
142:
143: jRes.flushBuffer();
144:
145: }
146:
147: public javax.servlet.Servlet getServlet() {
148: return servlet;
149: }
150:
151: /** Return a name string (suitable for use by humans) that describes this
152: * Container. Within the set of child containers belonging to a particular
153: * parent, Container names must be unique.
154: *
155: */
156: public String getName() {
157: return "RWS Container";
158: }
159:
160: public String convertSlashes(String servletClass) {
161: StringBuffer buf = new StringBuffer();
162:
163: StringTokenizer tkz = new StringTokenizer(servletClass, "/",
164: true);
165: while (tkz.hasMoreTokens()) {
166: String tmp = tkz.nextToken();
167: if (tmp.equalsIgnoreCase("/"))
168: buf.append(".");
169: else
170: buf.append(tmp);
171: }
172: return "" + buf;
173: }
174:
175: /** Set the fully qualified servlet class name for this servlet.
176: *
177: * @param servletClass Servlet class name
178: *
179: */
180: public void setServletClass(String servletClass) {
181: String servletClassConverted = convertSlashes(servletClass);
182:
183: load_error = 3;
184: if (DEBUG)
185: System.out.println("Set servlet class to "
186: + servletClassConverted);
187: try {
188: this .servletclass = servletClassConverted;
189: servlet = (javax.servlet.Servlet) Class.forName(
190: servletClassConverted).newInstance();
191: servlet.init(servletconfig);
192: load_error = 0;
193: } catch (ClassNotFoundException cnfe) {
194: System.out.println("ServletWrapper, ClassNotFound " + cnfe);
195: // could not load servlet
196: load_error = 1;
197: //
198: } catch (Exception e) {
199: System.out.println("ServletWrapper, Exception -> " + e);
200: // unknown exception
201: load_error = 2;
202: }
203:
204: }
205:
206: public int getError() {
207: return load_error;
208: }
209:
210: }
|