Quadcap Embeddable Server

com/quadcap/http/server22/WebServlet.java

Go to the documentation of this file.
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.util.Enumeration; 00044 import java.util.Properties; 00045 00046 import javax.servlet.Servlet; 00047 import javax.servlet.ServletConfig; 00048 import javax.servlet.ServletContext; 00049 import javax.servlet.ServletException; 00050 import javax.servlet.ServletRequest; 00051 import javax.servlet.ServletResponse; 00052 import javax.servlet.SingleThreadModel; 00053 00054 import com.quadcap.util.Debug; 00055 00056 /** 00057 * Represents a configured servlet, whether active or not. If we implemented 00058 * servlet garbage collection, we would free the servlet resources from 00059 * here. 00060 * 00061 * @author Stan Bailes 00062 */ 00063 public class WebServlet implements ServletConfig { 00064 WebApplication app; 00065 Servlet servlet = null; 00066 boolean singleThreadModel = false; 00067 String servletName; 00068 String servletClass; 00069 String jspFile; 00070 int loadOnStartup = -1; 00071 Properties initParams = new Properties(); 00072 00073 public WebServlet() { 00074 } 00075 00076 public String toString() { 00077 return servletName; 00078 } 00079 00080 public void setServletName(String name) { 00081 this.servletName = name; 00082 } 00083 00084 public String getServletName() { 00085 return servletName; 00086 } 00087 00088 public void setServletClass(String name) { 00089 this.servletClass = name; 00090 } 00091 00092 public String getServletClass() { 00093 return servletClass; 00094 } 00095 00096 public void setJspFile(String name) { 00097 this.jspFile = name; 00098 } 00099 00100 public String getJspFile() { 00101 return jspFile; 00102 } 00103 00104 public void setLoadOnStartup(int loadOnStartup) { 00105 this.loadOnStartup = loadOnStartup; 00106 } 00107 00108 public int getLoadOnStartup() { 00109 return loadOnStartup; 00110 } 00111 00112 public void addInitParam(String prop, String val) { 00113 initParams.setProperty(prop, val); 00114 } 00115 00116 public Enumeration getInitParameterNames() { 00117 return initParams.keys(); 00118 } 00119 00120 public String getInitParameter(String name) { 00121 return initParams.getProperty(name); 00122 } 00123 00124 public void setWebApplication(WebApplication app) { 00125 this.app = app; 00126 } 00127 00128 public WebApplication getWebApplication() { 00129 return app; 00130 } 00131 00132 public ServletContext getServletContext() { 00133 return app; 00134 } 00135 00136 // public void setServlet(Servlet servlet) { 00137 // this.servlet = servlet; 00138 // } 00139 00140 // public Servlet getServlet() { 00141 // return servlet; 00142 // } 00143 00144 final void init() throws ServletException { 00145 synchronized (this) { 00146 if (servlet == null) { 00147 try { 00148 Class c = app.getClassLoader().loadClass(servletClass); 00149 Servlet s = (Servlet)c.newInstance(); 00150 singleThreadModel = (s instanceof SingleThreadModel); 00151 s.init(this); 00152 servlet = s; 00153 } catch (ServletException e) { 00154 throw e; 00155 } catch (Throwable t) { 00156 Debug.print(t); 00157 throw new ServletException(t.toString()); 00158 } 00159 } 00160 } 00161 } 00162 00163 public void service(ServletRequest req, ServletResponse res) 00164 throws ServletException, IOException 00165 { 00166 if (servlet == null) init(); 00167 if (singleThreadModel) { 00168 synchronized (servlet) { 00169 servlet.service(req, res); 00170 } 00171 } else { 00172 servlet.service(req, res); 00173 } 00174 } 00175 }