KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > FilterManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.dispatch;
30
31 import com.caucho.config.BuilderProgram;
32 import com.caucho.config.NodeBuilderProgram;
33 import com.caucho.config.types.InitProgram;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.annotation.PostConstruct;
38 import javax.servlet.Filter JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Manages the servlets.
48  */

49 public class FilterManager {
50   static final Logger JavaDoc log = Log.open(FilterManager.class);
51   
52   static final L10N L = new L10N(FilterManager.class);
53
54   private Hashtable JavaDoc<String JavaDoc,FilterConfigImpl> _filters
55     = new Hashtable JavaDoc<String JavaDoc,FilterConfigImpl>();
56   
57   private Hashtable JavaDoc<String JavaDoc,Filter JavaDoc> _instances
58     = new Hashtable JavaDoc<String JavaDoc,Filter JavaDoc>();
59
60   /**
61    * Adds a filter to the filter manager.
62    */

63   public void addFilter(FilterConfigImpl config)
64   {
65     if (config.getServletContext() == null)
66       throw new NullPointerException JavaDoc();
67     
68     _filters.put(config.getFilterName(), config);
69   }
70
71   /**
72    * Adds a filter to the filter manager.
73    */

74   public FilterConfigImpl getFilter(String JavaDoc filterName)
75   {
76     return _filters.get(filterName);
77   }
78
79   /**
80    * Initialize filters that need starting at server start.
81    */

82   @PostConstruct
83   public void init()
84   {
85     for (String JavaDoc name : _filters.keySet()) {
86       try {
87     createFilter(name);
88       } catch (Exception JavaDoc e) {
89     log.log(Level.WARNING, e.toString(), e);
90       }
91     }
92   }
93
94   /**
95    * Instantiates a filter given its configuration.
96    *
97    * @param filterName the filter
98    *
99    * @return the initialized filter.
100    */

101   public Filter JavaDoc createFilter(String JavaDoc filterName)
102     throws ServletException JavaDoc
103   {
104     FilterConfigImpl config = _filters.get(filterName);
105
106     if (config == null) {
107       throw new ServletException JavaDoc(L.l("`{0}' is not a known filter. Filters must be defined by <filter> before being used.", filterName));
108     }
109     
110     Class JavaDoc filterClass = config.getFilterClass();
111
112     /* XXX:
113     if (! config.isAvailable(Alarm.getCurrentTime()))
114       throw config.getInitException();
115     */

116     
117     synchronized (config) {
118       try {
119         Filter JavaDoc filter = _instances.get(filterName);
120
121         if (filter != null)
122           return filter;
123
124         filter = (Filter JavaDoc) filterClass.newInstance();
125
126     // InjectIntrospector.configure(filter);
127

128         // Initialize bean properties
129
InitProgram init = config.getInit();
130     BuilderProgram program = NodeBuilderProgram.NULL;
131         
132         if (init != null)
133           program = init.getBuilderProgram();
134
135     program.configure(filter);
136     program.init(filter);
137
138         filter.init(config);
139         
140         _instances.put(filterName, filter);
141
142         /*
143         // If the filter has an MBean, register it
144         try {
145           String domain = "web-app";
146           String jmxName = (domain + ":" +
147                             "j2eeType=Filter," +
148                             "WebModule=" + getContextPath() + "," +
149                             "J2EEApplication=default," +
150                             "J2EEServer=" + getJ2EEServerName() + "," +
151                             "name=" + filterName);
152           getJMXServer().conditionalRegisterObject(filter, jmxName);
153         } catch (Throwable e) {
154           dbg.log(e);
155         }
156         */

157         
158         return filter;
159       } catch (ServletException JavaDoc e) {
160         // XXX: log(e.getMessage(), e);
161

162         // XXX: config.setInitException(e);
163

164         throw e;
165       } catch (Throwable JavaDoc e) {
166         // XXX: log(e.getMessage(), e);
167

168         throw new ServletException JavaDoc(e);
169       }
170     }
171   }
172
173   public void destroy()
174   {
175     ArrayList JavaDoc<Filter JavaDoc> filterList = new ArrayList JavaDoc<Filter JavaDoc>();
176     
177     if (_instances != null) {
178       synchronized (_instances) {
179         Enumeration JavaDoc<Filter JavaDoc> en = _instances.elements();
180         while (en.hasMoreElements()) {
181           Filter JavaDoc filter = en.nextElement();
182
183           filterList.add(filter);
184         }
185       }
186     }
187     
188     for (int i = 0; i < filterList.size(); i++) {
189       Filter JavaDoc filter = filterList.get(i);
190
191       try {
192         filter.destroy();
193       } catch (Throwable JavaDoc e) {
194         // log(String.valueOf(e), e);
195
}
196     }
197   }
198 }
199
Popular Tags