001: package com.quadcap.http.server22;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.Enumeration;
044: import java.util.Properties;
045:
046: import javax.servlet.Servlet;
047: import javax.servlet.ServletConfig;
048: import javax.servlet.ServletContext;
049: import javax.servlet.ServletException;
050: import javax.servlet.ServletRequest;
051: import javax.servlet.ServletResponse;
052: import javax.servlet.SingleThreadModel;
053:
054: import com.quadcap.util.Debug;
055:
056: /**
057: * Represents a configured servlet, whether active or not. If we implemented
058: * servlet garbage collection, we would free the servlet resources from
059: * here.
060: *
061: * @author Stan Bailes
062: */
063: public class WebServlet implements ServletConfig {
064: WebApplication app;
065: Servlet servlet = null;
066: boolean singleThreadModel = false;
067: String servletName;
068: String servletClass;
069: String jspFile;
070: int loadOnStartup = -1;
071: Properties initParams = new Properties();
072:
073: public WebServlet() {
074: }
075:
076: public String toString() {
077: return servletName;
078: }
079:
080: public void setServletName(String name) {
081: this .servletName = name;
082: }
083:
084: public String getServletName() {
085: return servletName;
086: }
087:
088: public void setServletClass(String name) {
089: this .servletClass = name;
090: }
091:
092: public String getServletClass() {
093: return servletClass;
094: }
095:
096: public void setJspFile(String name) {
097: this .jspFile = name;
098: }
099:
100: public String getJspFile() {
101: return jspFile;
102: }
103:
104: public void setLoadOnStartup(int loadOnStartup) {
105: this .loadOnStartup = loadOnStartup;
106: }
107:
108: public int getLoadOnStartup() {
109: return loadOnStartup;
110: }
111:
112: public void addInitParam(String prop, String val) {
113: initParams.setProperty(prop, val);
114: }
115:
116: public Enumeration getInitParameterNames() {
117: return initParams.keys();
118: }
119:
120: public String getInitParameter(String name) {
121: return initParams.getProperty(name);
122: }
123:
124: public void setWebApplication(WebApplication app) {
125: this .app = app;
126: }
127:
128: public WebApplication getWebApplication() {
129: return app;
130: }
131:
132: public ServletContext getServletContext() {
133: return app;
134: }
135:
136: // public void setServlet(Servlet servlet) {
137: // this.servlet = servlet;
138: // }
139:
140: // public Servlet getServlet() {
141: // return servlet;
142: // }
143:
144: final void init() throws ServletException {
145: synchronized (this ) {
146: if (servlet == null) {
147: try {
148: Class c = app.getClassLoader().loadClass(
149: servletClass);
150: Servlet s = (Servlet) c.newInstance();
151: singleThreadModel = (s instanceof SingleThreadModel);
152: s.init(this );
153: servlet = s;
154: } catch (ServletException e) {
155: throw e;
156: } catch (Throwable t) {
157: Debug.print(t);
158: throw new ServletException(t.toString());
159: }
160: }
161: }
162: }
163:
164: public void service(ServletRequest req, ServletResponse res)
165: throws ServletException, IOException {
166: if (servlet == null)
167: init();
168: if (singleThreadModel) {
169: synchronized (servlet) {
170: servlet.service(req, res);
171: }
172: } else {
173: servlet.service(req, res);
174: }
175: }
176: }
|