001: /*
002: * ServletFactory.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 com.rimfaxe.webserver.VirtualHost;
057: import com.rimfaxe.webserver.WebContext;
058: import com.rimfaxe.webserver.servletapi.jsp.*;
059:
060: import com.rimfaxe.webserver.servletapi.RimfaxeFilterConfig;
061:
062: /**
063: *
064: * @author Lars Andersen
065: */
066: public class ServletFactory {
067:
068: HashMap servlet_wrappers = new HashMap();
069:
070: HashMap filters = new HashMap();
071:
072: // Static includes to force linking in binary
073: private static Class c1 = javax.servlet.GenericServlet.class;
074: private static Class c2 = javax.servlet.ServletConfig.class;
075: //
076:
077: static ServletFactory instance = null;
078:
079: /** Creates a new instance of ServletFactory */
080: public ServletFactory() {
081: }
082:
083: public static ServletFactory getInstance() {
084: if (instance == null) {
085: instance = new ServletFactory();
086: }
087: return instance;
088: }
089:
090: public com.rimfaxe.webserver.servletapi.ServletWrapper getWrapper(
091: VirtualHost vh, WebContext webcontext, String name,
092: String cls, String urlpath,
093: com.rimfaxe.webserver.webapp.Servlet servlet_def) {
094: //System.out.println("ServletFactory, get ServletWrapper");
095: int res = 0;
096: String pretty_name = name;
097: String pretty_class = cls;
098:
099: if ((!name.equalsIgnoreCase("jsp"))
100: && (!name.equalsIgnoreCase("invoker"))
101: && (!name.equalsIgnoreCase("default"))) {
102: com.rimfaxe.webserver.servletapi.ServletWrapper sw = (com.rimfaxe.webserver.servletapi.ServletWrapper) servlet_wrappers
103: .get("servlet__" + vh.getName() + "__"
104: + webcontext.getUrlpath() + "__" + name);
105: if (sw != null) {
106: System.out.println("Get used ServletWrapper -> "
107: + "servlet__" + vh.getName() + "__"
108: + webcontext.getUrlpath() + "__" + name);
109: return sw;
110: }
111: }
112:
113: JSPhandler jsphandler = null;
114: JSPclass jsp_class = null;
115:
116: if ((name.equalsIgnoreCase("jsp"))) {
117: //System.out.println("ServletFactory, JSP ServletWrapper");
118: jsphandler = new JSPhandler();
119: jsp_class = jsphandler.getServletClass(vh, webcontext,
120: urlpath);
121:
122: if (jsp_class != null) {
123: com.rimfaxe.webserver.servletapi.ServletWrapper sw = (com.rimfaxe.webserver.servletapi.ServletWrapper) servlet_wrappers
124: .get("jsp__" + vh.getName() + "__"
125: + webcontext.getUrlpath() + "__"
126: + jsp_class.getFullClassName());
127: if (sw != null) {
128: //System.out.println("Get used JSP ServletWrapper -> "+""+jsp_class.getFullClassName());
129: return sw;
130: }
131: }
132: }
133:
134: if ((name.equalsIgnoreCase("invoker"))) {
135:
136: String invoker_class = servletInvoker(urlpath);
137:
138: com.rimfaxe.webserver.servletapi.ServletWrapper sw = (com.rimfaxe.webserver.servletapi.ServletWrapper) servlet_wrappers
139: .get("invoker__" + vh.getName() + "__"
140: + webcontext.getUrlpath() + "__"
141: + invoker_class);
142: if (sw != null) {
143: //System.out.println("Get used invoker ServletWrapper");
144: return sw;
145: }
146: }
147:
148: if ((name.equalsIgnoreCase("default"))) {
149:
150: com.rimfaxe.webserver.servletapi.ServletWrapper sw = (com.rimfaxe.webserver.servletapi.ServletWrapper) servlet_wrappers
151: .get("default__" + vh.getName() + "__"
152: + webcontext.getUrlpath());
153: if (sw != null) {
154: //System.out.println("Get used default ServletWrapper");
155: return sw;
156: }
157: }
158:
159: RimfaxeServletConfig rsc = new RimfaxeServletConfig(name,
160: webcontext.getServletContext(), servlet_def);
161: com.rimfaxe.webserver.servletapi.ServletWrapper swrap = new com.rimfaxe.webserver.servletapi.ServletWrapper(
162: rsc);
163:
164: StringBuffer msg = new StringBuffer();
165:
166: // Is this a JSP page
167: if (cls.equalsIgnoreCase("nativeJSP")) {
168:
169: if (jsp_class == null) {
170: res = 3;
171:
172: rsc.setInitParameter("com.rimfaxe.error_found",
173: "JSP page not found");
174: msg.append("Error opening JSP page <b>" + urlpath
175: + "</b> <br>");
176: msg.append("JSP page <b>" + urlpath
177: + "</b> could not be found. <br>");
178: } else {
179: //System.out.println("JSP error = "+jsp_class.getError());
180: if (jsp_class.getError() > 0) // JSP Not Found
181: {
182:
183: res = 2;
184:
185: if (jsp_class.getError() == jsp_class.JSP_ERROR) {
186: rsc.setInitParameter("com.rimfaxe.error_found",
187: "JSP compile error");
188: msg.append("Error compiling JSP page <b>"
189: + urlpath + "</b> <br>");
190: }
191: if (jsp_class.getError() == jsp_class.COMPILE_ERROR) {
192: rsc.setInitParameter("com.rimfaxe.error_found",
193: "JSP compile error");
194: msg.append("Error compiling JSP page <b>"
195: + urlpath + "</b> <br>");
196: } else if (jsp_class.getError() == jsp_class.LINK_ERROR) {
197: rsc.setInitParameter("com.rimfaxe.error_found",
198: "JSP link error");
199: msg.append("Error linking JSP page <b>"
200: + urlpath + "</b> <br>");
201: } else if (jsp_class.getError() == jsp_class.EXECUTION_ERROR) {
202: rsc.setInitParameter("com.rimfaxe.error_found",
203: "JSP execution error");
204: msg.append("Error executing JSP page <b>"
205: + urlpath + "</b> <br>");
206: }
207: msg.append(jsp_class.getErrorMsg());
208: } else {
209: cls = jsp_class.getFullClassName();
210: pretty_class = cls;
211: swrap.setServletClass(cls);
212: res = swrap.getError();
213:
214: if (res == 0)
215: servlet_wrappers.put("jsp__" + vh.getName()
216: + "__" + webcontext.getUrlpath() + "__"
217: + cls, swrap);
218: }
219: }
220:
221: }
222:
223: // Is this the standard servlet invoker
224: else if (cls.equalsIgnoreCase("nativeInvokerServlet")) {
225:
226: cls = servletInvoker(urlpath);
227:
228: pretty_class = cls;
229:
230: swrap.setServletClass(cls);
231: res = swrap.getError();
232:
233: if (res == 1) // ClassNotFound
234: {
235: rsc.setInitParameter("com.rimfaxe.error_found",
236: "Servlet class not found");
237: msg.append("Error running servlet <b>" + pretty_name
238: + "</b> <br>");
239: msg.append("Servlet class <b>" + pretty_class
240: + "</b> could not be found. <br>");
241: }
242:
243: if (res == 0)
244: servlet_wrappers.put("invoker__" + vh.getName() + "__"
245: + webcontext.getUrlpath() + "__" + cls, swrap);
246:
247: }
248: // Is this the standard servlet invoker
249: else if (cls.equalsIgnoreCase("nativeDefaultServlet")) {
250:
251: swrap
252: .setServletClass("com.rimfaxe.webserver.servletapi.DefaultServlet");
253:
254: res = swrap.getError();
255:
256: if (res == 1) // ClassNotFound
257: {
258: rsc.setInitParameter("com.rimfaxe.error_found",
259: "Servlet class not found");
260: msg
261: .append("Error running servlet <b>nativeDefaultServlet</b> <br>");
262: msg
263: .append("Servlet class <b>nativeDefaultServlet</b> not found. <br>");
264: }
265:
266: if (res == 0)
267: servlet_wrappers.put("default__" + vh.getName() + "__"
268: + webcontext.getUrlpath(), swrap);
269:
270: } else {
271:
272: swrap.setServletClass(cls);
273:
274: res = swrap.getError();
275:
276: if (res == 1) // ClassNotFound
277: {
278: rsc.setInitParameter("com.rimfaxe.error_found",
279: "Servlet class not found");
280: msg.append("Error running servlet <b>" + pretty_name
281: + "</b> <br>");
282: msg.append("Servlet class <b>" + pretty_class
283: + "</b> not found. <br>");
284: }
285:
286: if (res == 0)
287: servlet_wrappers.put("servlet__" + vh.getName() + "__"
288: + webcontext.getUrlpath() + "__" + name, swrap);
289:
290: }
291:
292: if (res > 0) {
293:
294: rsc.setInitParameter("com.rimfaxe.error_msg", "" + msg);
295: swrap
296: .setServletClass("com.rimfaxe.webserver.servletapi.standard.ErrorServlet");
297: }
298:
299: return swrap;
300: }
301:
302: public void loadServlet(String name, WebContext webcontext,
303: com.rimfaxe.webserver.webapp.Servlet servlet_def) {
304:
305: RimfaxeServletConfig rsc = new RimfaxeServletConfig(name,
306: webcontext.getServletContext(), servlet_def);
307: com.rimfaxe.webserver.servletapi.ServletWrapper swrap = new com.rimfaxe.webserver.servletapi.ServletWrapper(
308: rsc);
309: swrap.setServletClass(servlet_def.getClassName());
310:
311: //servlet_wrappers.put("servlet__"+vh.getName()+"__"+webcontext.getUrlpath()+"__"+name,swrap);
312: }
313:
314: public String servletInvoker(String urlpath) {
315: //System.out.println("Extract servletname from "+urlpath);
316:
317: String cls = "unknown";
318: StringTokenizer tkz = new StringTokenizer(urlpath, "/?&", true);
319: while (tkz.hasMoreTokens()) {
320: String tmp = tkz.nextToken();
321: if (tmp.equalsIgnoreCase("/")) {
322: } else if (tmp.equalsIgnoreCase("?")) {
323: return cls;
324: } else {
325: cls = tmp;
326: }
327: }
328:
329: //System.out.println("Extract servletname "+cls);
330: return cls;
331: }
332:
333: public javax.servlet.Filter getFilterInstance(
334: WebContext webcontext,
335: com.rimfaxe.webserver.webapp.Filter filter_desc) {
336: String id = "filter_" + webcontext.getVirtualHost().getName()
337: + "_" + webcontext.getName() + "_"
338: + filter_desc.getKey();
339:
340: javax.servlet.Filter filter = null;
341: try {
342: filter = (javax.servlet.Filter) this .filters.get(id);
343: } catch (Exception e) {
344: }
345:
346: if (filter != null) {
347: System.out.println("Reuse filter = " + id);
348: } else {
349:
350: try {
351: RimfaxeFilterConfig rfc = new RimfaxeFilterConfig(
352: filter_desc.getKey(), webcontext
353: .getServletContext(), filter_desc);
354:
355: filter = (javax.servlet.Filter) Class.forName(
356: filter_desc.getClassName()).newInstance();
357: filter.init(rfc);
358: filters.put(id, filter);
359: } catch (ClassNotFoundException cnfe) {
360: System.out.println("Filter, ClassNotFound : "
361: + filter_desc.getClassName());
362:
363: } catch (Exception e) {
364: System.out.println("Filter, Exception -> " + e);
365: }
366: }
367:
368: return filter;
369: }
370: }
|