001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.servlet;
030:
031: import java.util.Enumeration;
032: import java.util.logging.Logger;
033:
034: /**
035: * GenericServlet is a convenient abstract class for defining Servlets.
036: * Servlets which need more control than HttpServlet can extend
037: * GenericServlet.
038: *
039: * <p>In addition, GenericServlet implements ServletConfig to make
040: * it easier to get configuration information.
041: *
042: * <p>Servlet configuration looks like the following:
043: *
044: * <pre><code>
045: * <servlet servlet-name='myservlet'
046: * servlet-class='test.MyServlet'>
047: * <init-param param1='value1'/>
048: * <init-param param2='value2'/>
049: * </servlet>
050: * </code></pre>
051: */
052: public abstract class GenericServlet implements Servlet, ServletConfig,
053: java.io.Serializable {
054: private static final Logger log = Logger
055: .getLogger(GenericServlet.class.getName());
056:
057: private transient ServletConfig _config;
058:
059: /**
060: * Initialize the servlet. Most servlets should override the zero
061: * parameter <code>init()</code> instead.
062: *
063: * @param config the servlet's configuration
064: */
065: public void init(ServletConfig config) throws ServletException {
066: _config = config;
067: // log("init");
068: init();
069: }
070:
071: /**
072: * Initialize the servlet. Servlets should override this method
073: * if they need any initialization like opening pooled
074: * database connections.
075: */
076: public void init() throws ServletException {
077: }
078:
079: /**
080: * Returns this servlet's configuration.
081: */
082: public ServletConfig getServletConfig() {
083: return _config;
084: }
085:
086: /**
087: * Returns the servlet name for this configuration. For example,
088: * 'myservlet' in the following configuration:
089: *
090: * <pre><code>
091: * <servlet servlet-name='myservlet'
092: * servlet-class='test.MyServlet'/>
093: * </code></pre>
094: */
095: public String getServletName() {
096: ServletConfig config = getServletConfig();
097:
098: return config != null ? config.getServletName() : getClass()
099: .getName();
100: }
101:
102: /**
103: * Returns an initialization parameter. Initialization parameters
104: * are defined in the servlet configuration (in resin.conf) as follows:
105: *
106: * <pre><code>
107: * <servlet servlet-name='myservlet'
108: * servlet-class='test.MyServlet'>
109: * <init-param param1='value1'/>
110: * <init-param param2='value2'/>
111: * </servlet>
112: * </code></pre>
113: *
114: * @param name of the parameter
115: * @return the init parameter value
116: */
117: public String getInitParameter(String name) {
118: ServletConfig config = getServletConfig();
119:
120: return config != null ? config.getInitParameter(name) : null;
121: }
122:
123: /**
124: * Enumerates all the initialization parameter.
125: */
126: public Enumeration getInitParameterNames() {
127: ServletConfig config = getServletConfig();
128:
129: return config != null ? config.getInitParameterNames() : null;
130: }
131:
132: /**
133: * Returns the application (servlet context) that the servlet
134: * belongs to. The application provides several useful methods, e.g.
135: * including other files, forwarding, and translating physical paths.
136: */
137: public ServletContext getServletContext() {
138: ServletConfig config = getServletConfig();
139:
140: return config != null ? config.getServletContext() : null;
141: }
142:
143: /**
144: * Returns a string describing the servlet.
145: */
146: public String getServletInfo() {
147: return "";
148: }
149:
150: /**
151: * Logs an error message in the application's log.
152: */
153: public void log(String message) {
154: log.config(getServletName() + ": " + message);
155: }
156:
157: /**
158: * Logs an error message and an exception trace in the application's log.
159: */
160: public void log(String message, Throwable cause) {
161: ServletContext app = getServletContext();
162:
163: if (app != null)
164: app.log(getClass().getName() + ": " + message, cause);
165: }
166:
167: /**
168: * Called when the servlet (and the application) shuts down.
169: */
170: public void destroy() {
171: // log("destroy");
172: }
173:
174: public String toString() {
175: return getClass().getSimpleName() + "[" + getServletContext()
176: + "]";
177: }
178: }
|