001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.dispatch;
030:
031: import com.caucho.config.Config;
032: import com.caucho.config.program.ContainerProgram;
033: import com.caucho.log.Log;
034: import com.caucho.util.L10N;
035: import com.caucho.webbeans.component.*;
036: import com.caucho.webbeans.manager.*;
037:
038: import javax.annotation.PostConstruct;
039: import javax.servlet.Filter;
040: import javax.servlet.ServletException;
041: import javax.webbeans.*;
042: import java.util.ArrayList;
043: import java.util.Enumeration;
044: import java.util.Hashtable;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: /**
049: * Manages the servlets.
050: */
051: public class FilterManager {
052: static final Logger log = Logger.getLogger(FilterManager.class
053: .getName());
054:
055: static final L10N L = new L10N(FilterManager.class);
056:
057: private Hashtable<String, FilterConfigImpl> _filters = new Hashtable<String, FilterConfigImpl>();
058:
059: private ComponentImpl _comp;
060:
061: private Hashtable<String, Filter> _instances = new Hashtable<String, Filter>();
062:
063: /**
064: * Adds a filter to the filter manager.
065: */
066: public void addFilter(FilterConfigImpl config) {
067: if (config.getServletContext() == null)
068: throw new NullPointerException();
069:
070: _filters.put(config.getFilterName(), config);
071: }
072:
073: /**
074: * Adds a filter to the filter manager.
075: */
076: public FilterConfigImpl getFilter(String filterName) {
077: return _filters.get(filterName);
078: }
079:
080: /**
081: * Initialize filters that need starting at server start.
082: */
083: @PostConstruct
084: public void init() {
085: for (String name : _filters.keySet()) {
086: try {
087: createFilter(name);
088: } catch (Exception e) {
089: log.log(Level.WARNING, e.toString(), e);
090: }
091: }
092: }
093:
094: /**
095: * Instantiates a filter given its configuration.
096: *
097: * @param filterName the filter
098: *
099: * @return the initialized filter.
100: */
101: public Filter createFilter(String filterName)
102: throws ServletException {
103: FilterConfigImpl config = _filters.get(filterName);
104:
105: if (config == null) {
106: throw new ServletException(
107: L
108: .l(
109: "`{0}' is not a known filter. Filters must be defined by <filter> before being used.",
110: filterName));
111: }
112:
113: Class filterClass = config.getFilterClass();
114:
115: /* XXX:
116: if (! config.isAvailable(Alarm.getCurrentTime()))
117: throw config.getInitException();
118: */
119:
120: synchronized (config) {
121: try {
122: Filter filter = _instances.get(filterName);
123:
124: if (filter != null)
125: return filter;
126:
127: WebBeansContainer webBeans = WebBeansContainer.create();
128:
129: _comp = (ComponentImpl) webBeans
130: .createTransient(filterClass);
131:
132: filter = (Filter) _comp.createNoInit();
133:
134: // InjectIntrospector.configure(filter);
135:
136: // Initialize bean properties
137: ContainerProgram init = config.getInit();
138:
139: if (init != null)
140: init.configure(filter);
141:
142: Config.init(filter);
143:
144: filter.init(config);
145:
146: _instances.put(filterName, filter);
147:
148: /*
149: // If the filter has an MBean, register it
150: try {
151: String domain = "web-app";
152: String jmxName = (domain + ":" +
153: "j2eeType=Filter," +
154: "WebModule=" + getContextPath() + "," +
155: "J2EEApplication=default," +
156: "J2EEServer=" + getJ2EEServerName() + "," +
157: "name=" + filterName);
158: getJMXServer().conditionalRegisterObject(filter, jmxName);
159: } catch (Throwable e) {
160: dbg.log(e);
161: }
162: */
163:
164: return filter;
165: } catch (ServletException e) {
166: // XXX: log(e.getMessage(), e);
167:
168: // XXX: config.setInitException(e);
169:
170: throw e;
171: } catch (Throwable e) {
172: // XXX: log(e.getMessage(), e);
173:
174: throw new ServletException(e);
175: }
176: }
177: }
178:
179: public void destroy() {
180: ArrayList<Filter> filterList = new ArrayList<Filter>();
181:
182: if (_instances != null) {
183: synchronized (_instances) {
184: Enumeration<Filter> en = _instances.elements();
185: while (en.hasMoreElements()) {
186: Filter filter = en.nextElement();
187:
188: filterList.add(filter);
189: }
190: }
191: }
192:
193: for (int i = 0; i < filterList.size(); i++) {
194: Filter filter = filterList.get(i);
195:
196: try {
197: if (_comp != null)
198: _comp.destroy(filter);
199:
200: filter.destroy();
201: } catch (Throwable e) {
202: log.log(Level.WARNING, e.toString(), e);
203: }
204: }
205: }
206: }
|