View Javadoc

1   // ========================================================================
2   // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.jetty.servlet;
16  
17  import java.util.Enumeration;
18  
19  import javax.servlet.Filter;
20  import javax.servlet.FilterConfig;
21  import javax.servlet.ServletContext;
22  
23  import org.mortbay.jetty.Handler;
24  import org.mortbay.log.Log;
25  
26  /* --------------------------------------------------------------------- */
27  /** 
28   * @author Greg Wilkins
29   */
30  public class FilterHolder
31      extends Holder
32  {
33  
34      /* ------------------------------------------------------------ */
35      /** Dispatch type from name
36       */
37      public static int dispatch(String type)
38      {
39          if ("request".equalsIgnoreCase(type))
40              return Handler.REQUEST;
41          if ("forward".equalsIgnoreCase(type))
42              return Handler.FORWARD;
43          if ("include".equalsIgnoreCase(type))
44              return Handler.INCLUDE;
45          if ("error".equalsIgnoreCase(type))
46              return Handler.ERROR;
47          throw new IllegalArgumentException(type);
48      }
49      
50      /* ------------------------------------------------------------ */
51      private transient Filter _filter;
52      private transient Config _config;
53          
54      /* ---------------------------------------------------------------- */
55      /** Constructor for Serialization.
56       */
57      public FilterHolder()
58      {
59      }   
60      
61      /* ---------------------------------------------------------------- */
62      /** Constructor for Serialization.
63       */
64      public FilterHolder(Class filter)
65      {
66          super (filter);
67      }
68  
69      /* ---------------------------------------------------------------- */
70      /** Constructor for existing filter.
71       */
72      public FilterHolder(Filter filter)
73      {
74          setFilter(filter);
75      }
76      
77      /* ------------------------------------------------------------ */
78      public void doStart()
79          throws Exception
80      {
81          super.doStart();
82          
83          if (!javax.servlet.Filter.class
84              .isAssignableFrom(_class))
85          {
86              String msg = _class+" is not a javax.servlet.Filter";
87              super.stop();
88              throw new IllegalStateException(msg);
89          }
90  
91          if (_filter==null)
92              _filter=(Filter)newInstance();
93          
94          _filter = getServletHandler().customizeFilter(_filter);
95          
96          _config=new Config();
97          _filter.init(_config);
98      }
99  
100     /* ------------------------------------------------------------ */
101     public void doStop()
102     {      
103         if (_filter!=null)
104         {
105             try
106             {
107                 destroyInstance(_filter);
108             }
109             catch (Exception e)
110             {
111                 Log.warn(e);
112             }
113         }
114         if (!_extInstance)
115             _filter=null;
116         
117         _config=null;
118         super.doStop();   
119     }
120     
121     public void destroyInstance (Object o)
122     throws Exception
123     {
124         if (o==null)
125             return;
126         Filter f = (Filter)o;
127         f.destroy();
128         getServletHandler().customizeFilterDestroy(f);
129     }
130 
131     /* ------------------------------------------------------------ */
132     public synchronized void setFilter(Filter filter)
133     {
134         _filter=filter;
135         _extInstance=true;
136         setHeldClass(filter.getClass());
137         if (getName()==null)
138             setName(filter.getClass().getName());
139     }
140     
141     /* ------------------------------------------------------------ */
142     public Filter getFilter()
143     {
144         return _filter;
145     }
146 
147     /* ------------------------------------------------------------ */
148     public String toString()
149     {
150         return getName();
151     }
152     
153     /* ------------------------------------------------------------ */
154     /* ------------------------------------------------------------ */
155     /* ------------------------------------------------------------ */
156     class Config implements FilterConfig
157     {
158         /* ------------------------------------------------------------ */
159         public String getFilterName()
160         {
161             return _name;
162         }
163 
164         /* ------------------------------------------------------------ */
165         public ServletContext getServletContext()
166         {
167             return _servletHandler.getServletContext();
168         }
169         
170         /* -------------------------------------------------------- */
171         public String getInitParameter(String param)
172         {
173             return FilterHolder.this.getInitParameter(param);
174         }
175     
176         /* -------------------------------------------------------- */
177         public Enumeration getInitParameterNames()
178         {
179             return FilterHolder.this.getInitParameterNames();
180         }
181     }
182     
183 
184 
185 }
186 
187 
188 
189 
190