KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > FilterConfiguration


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.io.IOException JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.servlet.Filter JavaDoc;
16 import javax.servlet.FilterChain JavaDoc;
17 import javax.servlet.ServletContext JavaDoc;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.ServletRequest JavaDoc;
20 import javax.servlet.ServletResponse JavaDoc;
21 import javax.servlet.UnavailableException JavaDoc;
22
23 import org.w3c.dom.Node JavaDoc;
24
25 /**
26  * Corresponds to a filter object in the web app. Holds one instance only.
27  *
28  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
29  */

30 public class FilterConfiguration implements javax.servlet.FilterConfig JavaDoc {
31     final String JavaDoc ELEM_NAME = "filter-name";
32     final String JavaDoc ELEM_DISPLAY_NAME = "display-name";
33     final String JavaDoc ELEM_CLASS = "filter-class";
34     final String JavaDoc ELEM_DESCRIPTION = "description";
35     final String JavaDoc ELEM_INIT_PARAM = "init-param";
36     final String JavaDoc ELEM_INIT_PARAM_NAME = "param-name";
37     final String JavaDoc ELEM_INIT_PARAM_VALUE = "param-value";
38     
39     private String JavaDoc filterName;
40     private String JavaDoc classFile;
41     private Filter JavaDoc instance;
42     private Map JavaDoc initParameters;
43     private ServletContext JavaDoc context;
44     private ClassLoader JavaDoc loader;
45     private boolean unavailableException;
46     private Object JavaDoc filterSemaphore = new Boolean JavaDoc(true);
47
48     protected FilterConfiguration(ServletContext JavaDoc context, ClassLoader JavaDoc loader) {
49         this.context = context;
50         this.loader = loader;
51         this.initParameters = new Hashtable JavaDoc();
52     }
53
54     /**
55      * Constructor
56      */

57     public FilterConfiguration(ServletContext JavaDoc context, ClassLoader JavaDoc loader, Node JavaDoc elm) {
58         this(context, loader);
59
60         // Parse the web.xml file entry
61
for (int n = 0; n < elm.getChildNodes().getLength(); n++) {
62             Node JavaDoc child = elm.getChildNodes().item(n);
63             if (child.getNodeType() != Node.ELEMENT_NODE)
64                 continue;
65             String JavaDoc nodeName = child.getNodeName();
66
67             // Construct the servlet instances
68
if (nodeName.equals(ELEM_NAME))
69                 this.filterName = WebAppConfiguration.getTextFromNode(child);
70             else if (nodeName.equals(ELEM_CLASS))
71                 this.classFile = WebAppConfiguration.getTextFromNode(child);
72             else if (nodeName.equals(ELEM_INIT_PARAM)) {
73                 String JavaDoc paramName = null;
74                 String JavaDoc paramValue = null;
75                 for (int k = 0; k < child.getChildNodes().getLength(); k++) {
76                     Node JavaDoc paramNode = child.getChildNodes().item(k);
77                     if (paramNode.getNodeType() != Node.ELEMENT_NODE)
78                         continue;
79                     else if (paramNode.getNodeName().equals(
80                             ELEM_INIT_PARAM_NAME))
81                         paramName = WebAppConfiguration.getTextFromNode(paramNode);
82                     else if (paramNode.getNodeName().equals(
83                             ELEM_INIT_PARAM_VALUE))
84                         paramValue = WebAppConfiguration.getTextFromNode(paramNode);
85                 }
86                 if ((paramName != null) && (paramValue != null))
87                     this.initParameters.put(paramName, paramValue);
88             }
89         }
90         Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
91                 "FilterConfiguration.DeployedInstance", new String JavaDoc[] {
92                         this.filterName, this.classFile });
93     }
94
95     public String JavaDoc getFilterName() {
96         return this.filterName;
97     }
98
99     public String JavaDoc getInitParameter(String JavaDoc paramName) {
100         return (String JavaDoc) this.initParameters.get(paramName);
101     }
102
103     public Enumeration JavaDoc getInitParameterNames() {
104         return Collections.enumeration(this.initParameters.keySet());
105     }
106
107     public ServletContext JavaDoc getServletContext() {
108         return this.context;
109     }
110
111     /**
112      * Implements the first-time-init of an instance, and wraps it in a
113      * dispatcher.
114      */

115     public Filter JavaDoc getFilter() throws ServletException JavaDoc {
116         synchronized (this.filterSemaphore) {
117             if (isUnavailable())
118                 throw new WinstoneException(Launcher.RESOURCES
119                         .getString("FilterConfiguration.FilterUnavailable"));
120             else if (this.instance == null)
121                 try {
122                     ClassLoader JavaDoc cl = Thread.currentThread()
123                             .getContextClassLoader();
124                     Thread.currentThread().setContextClassLoader(this.loader);
125
126                     Class JavaDoc filterClass = Class.forName(classFile, true,
127                             this.loader);
128                     this.instance = (Filter JavaDoc) filterClass.newInstance();
129                     Logger.log(Logger.DEBUG, Launcher.RESOURCES,
130                             "FilterConfiguration.init", this.filterName);
131
132                     // Initialise with the correct classloader
133
this.instance.init(this);
134                     Thread.currentThread().setContextClassLoader(cl);
135                 } catch (ClassNotFoundException JavaDoc err) {
136                     Logger.log(Logger.ERROR, Launcher.RESOURCES,
137                             "FilterConfiguration.ClassLoadError",
138                             this.classFile, err);
139                 } catch (IllegalAccessException JavaDoc err) {
140                     Logger.log(Logger.ERROR, Launcher.RESOURCES,
141                             "FilterConfiguration.ClassLoadError",
142                             this.classFile, err);
143                 } catch (InstantiationException JavaDoc err) {
144                     Logger.log(Logger.ERROR, Launcher.RESOURCES,
145                             "FilterConfiguration.ClassLoadError",
146                             this.classFile, err);
147                 } catch (ServletException JavaDoc err) {
148                     this.instance = null;
149                     if (err instanceof UnavailableException JavaDoc)
150                         setUnavailable();
151                     throw err;
152 // throw new WinstoneException(resources
153
// .getString("FilterConfiguration.InitError"), err);
154
}
155         }
156         return this.instance;
157     }
158
159     /**
160      * Called when it's time for the container to shut this servlet down.
161      */

162     public void destroy() {
163         synchronized (this.filterSemaphore) {
164             setUnavailable();
165         }
166     }
167
168     public String JavaDoc toString() {
169         return Launcher.RESOURCES.getString("FilterConfiguration.Description",
170                 new String JavaDoc[] { this.filterName, this.classFile });
171     }
172
173     public boolean isUnavailable() {
174         return this.unavailableException;
175     }
176
177     protected void setUnavailable() {
178         this.unavailableException = true;
179         if (this.instance != null) {
180             Logger.log(Logger.DEBUG, Launcher.RESOURCES,
181                     "FilterConfiguration.destroy", this.filterName);
182             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
183             Thread.currentThread().setContextClassLoader(this.loader);
184             this.instance.destroy();
185             Thread.currentThread().setContextClassLoader(cl);
186             this.instance = null;
187         }
188     }
189     
190     public void execute(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
191             throws ServletException JavaDoc, IOException JavaDoc {
192         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
193         Thread.currentThread().setContextClassLoader(this.loader);
194         try {
195             getFilter().doFilter(request, response, chain);
196         } catch (UnavailableException JavaDoc err) {
197             setUnavailable();
198             throw new ServletException JavaDoc(Launcher.RESOURCES
199                     .getString("RequestDispatcher.FilterError"), err);
200         } finally {
201             Thread.currentThread().setContextClassLoader(cl);
202         }
203     }
204 }
205
Popular Tags