View Javadoc

1   // ========================================================================
2   // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.servlet;
15  
16  import javax.servlet.Filter;
17  import javax.servlet.FilterConfig;
18  
19  import org.eclipse.jetty.util.log.Log;
20  
21  /* --------------------------------------------------------------------- */
22  /** 
23   * 
24   */
25  public class FilterHolder extends Holder
26  {    
27      /* ------------------------------------------------------------ */
28      private transient Filter _filter;
29      private transient Config _config;
30          
31      /* ---------------------------------------------------------------- */
32      /** Constructor for Serialization.
33       */
34      public FilterHolder()
35      {
36      }   
37      
38      /* ---------------------------------------------------------------- */
39      /** Constructor for Serialization.
40       */
41      public FilterHolder(Class filter)
42      {
43          super (filter);
44      }
45  
46      /* ---------------------------------------------------------------- */
47      /** Constructor for existing filter.
48       */
49      public FilterHolder(Filter filter)
50      {
51          setFilter(filter);
52      }
53      
54      /* ------------------------------------------------------------ */
55      public void doStart()
56          throws Exception
57      {
58          super.doStart();
59          
60          if (!javax.servlet.Filter.class
61              .isAssignableFrom(_class))
62          {
63              String msg = _class+" is not a javax.servlet.Filter";
64              super.stop();
65              throw new IllegalStateException(msg);
66          }
67  
68          if (_filter==null)
69              _filter=(Filter)newInstance();
70          
71          _filter = getServletHandler().customizeFilter(_filter);
72          
73          _config=new Config();
74          _filter.init(_config);
75      }
76  
77      /* ------------------------------------------------------------ */
78      public void doStop()
79          throws Exception
80      {      
81          if (_filter!=null)
82          {
83              try
84              {
85                  destroyInstance(_filter);
86              }
87              catch (Exception e)
88              {
89                  Log.warn(e);
90              }
91          }
92          if (!_extInstance)
93              _filter=null;
94          
95          _config=null;
96          super.doStop();   
97      }
98  
99      /* ------------------------------------------------------------ */
100     public void destroyInstance (Object o)
101     throws Exception
102     {
103         if (o==null)
104             return;
105         Filter f = (Filter)o;
106         f.destroy();
107         getServletHandler().customizeFilterDestroy(f);
108     }
109 
110     /* ------------------------------------------------------------ */
111     public synchronized void setFilter(Filter filter)
112     {
113         _filter=filter;
114         _extInstance=true;
115         setHeldClass(filter.getClass());
116         if (getName()==null)
117             setName(filter.getClass().getName());
118     }
119     
120     /* ------------------------------------------------------------ */
121     public Filter getFilter()
122     {
123         return _filter;
124     }
125 
126     /* ------------------------------------------------------------ */
127     public String toString()
128     {
129         return getName();
130     }
131     
132     /* ------------------------------------------------------------ */
133     /* ------------------------------------------------------------ */
134     /* ------------------------------------------------------------ */
135     class Config extends HolderConfig implements FilterConfig
136     {
137         /* ------------------------------------------------------------ */
138         public String getFilterName()
139         {
140             return _name;
141         }
142     }
143 }
144 
145 
146 
147 
148