001: // ========================================================================
002: // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.jetty.servlet;
016:
017: import java.util.Enumeration;
018:
019: import javax.servlet.Filter;
020: import javax.servlet.FilterConfig;
021: import javax.servlet.Servlet;
022: import javax.servlet.ServletContext;
023: import javax.servlet.SingleThreadModel;
024:
025: import org.mortbay.jetty.Handler;
026: import org.mortbay.log.Log;
027:
028: /* --------------------------------------------------------------------- */
029: /**
030: * @author Greg Wilkins
031: */
032: public class FilterHolder extends Holder {
033:
034: /* ------------------------------------------------------------ */
035: /** Dispatch type from name
036: */
037: public static int dispatch(String type) {
038: if ("request".equalsIgnoreCase(type))
039: return Handler.REQUEST;
040: if ("forward".equalsIgnoreCase(type))
041: return Handler.FORWARD;
042: if ("include".equalsIgnoreCase(type))
043: return Handler.INCLUDE;
044: if ("error".equalsIgnoreCase(type))
045: return Handler.ERROR;
046: throw new IllegalArgumentException(type);
047: }
048:
049: /* ------------------------------------------------------------ */
050: private transient Filter _filter;
051: private transient Config _config;
052:
053: /* ---------------------------------------------------------------- */
054: /** Constructor for Serialization.
055: */
056: public FilterHolder() {
057: }
058:
059: /* ---------------------------------------------------------------- */
060: /** Constructor for Serialization.
061: */
062: public FilterHolder(Class filter) {
063: super (filter);
064: }
065:
066: /* ---------------------------------------------------------------- */
067: /** Constructor for existing filter.
068: */
069: public FilterHolder(Filter filter) {
070: setFilter(filter);
071: }
072:
073: /* ------------------------------------------------------------ */
074: public void doStart() throws Exception {
075: super .doStart();
076:
077: if (!javax.servlet.Filter.class.isAssignableFrom(_class)) {
078: String msg = _class + " is not a javax.servlet.Filter";
079: super .stop();
080: throw new IllegalStateException(msg);
081: }
082:
083: if (_filter == null)
084: _filter = (Filter) newInstance();
085:
086: _filter = getServletHandler().customizeFilter(_filter);
087:
088: _config = new Config();
089: _filter.init(_config);
090: }
091:
092: /* ------------------------------------------------------------ */
093: public void doStop() {
094: if (_filter != null) {
095: try {
096: getServletHandler().customizeFilterDestroy(_filter);
097: } catch (Exception e) {
098: Log.warn(e);
099: }
100: _filter.destroy();
101: }
102: if (!_extInstance)
103: _filter = null;
104:
105: _config = null;
106: super .doStop();
107: }
108:
109: /* ------------------------------------------------------------ */
110: public synchronized void setFilter(Filter filter) {
111: _filter = filter;
112: _extInstance = true;
113: setHeldClass(filter.getClass());
114: if (getName() == null)
115: setName(filter.getClass().getName());
116: }
117:
118: /* ------------------------------------------------------------ */
119: public Filter getFilter() {
120: return _filter;
121: }
122:
123: /* ------------------------------------------------------------ */
124: public String toString() {
125: return getName();
126: }
127:
128: /* ------------------------------------------------------------ */
129: /* ------------------------------------------------------------ */
130: /* ------------------------------------------------------------ */
131: class Config implements FilterConfig {
132: /* ------------------------------------------------------------ */
133: public String getFilterName() {
134: return _name;
135: }
136:
137: /* ------------------------------------------------------------ */
138: public ServletContext getServletContext() {
139: return _servletHandler.getServletContext();
140: }
141:
142: /* -------------------------------------------------------- */
143: public String getInitParameter(String param) {
144: return FilterHolder.this .getInitParameter(param);
145: }
146:
147: /* -------------------------------------------------------- */
148: public Enumeration getInitParameterNames() {
149: return FilterHolder.this.getInitParameterNames();
150: }
151: }
152:
153: }
|