Java tutorial
/* * Copyright (c) 1998-2012 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */ package org.ireland.jnetty.dispatch.filter; import javax.servlet.Filter; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.FilterRegistration; import javax.servlet.DispatcherType; import javax.servlet.ServletException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ireland.jnetty.config.ConfigException; import org.ireland.jnetty.webapp.WebApp; import org.springframework.util.Assert; import java.util.*; /** * Configuration for a filter. * * Filter?(web.xml<filter>) <filter> <description>this is EncodeFilter</description> <display-name>EncodeFilter</display-name> <filter-name>EncodeFilter</filter-name> <filter-class>com.ireland.filters.EncodeFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>name</param-name> <param-value>jack</param-value> </init-param> <init-param> <param-name>pwd</param-name> <param-value>1234</param-value> </init-param> </filter> */ public class FilterConfigImpl implements FilterConfig, FilterRegistration.Dynamic { protected static final Log log = LogFactory.getLog(FilterConfigImpl.class.getName()); protected static final boolean debug = log.isDebugEnabled(); private final WebApp _webApp; private final ServletContext _servletContext; private final FilterManager _filterManager; //web.xml ? private String _filterName; private String _filterClassName; private Class<? extends Filter> _filterClass; private String _displayName; private HashMap<String, String> _initParams = new HashMap<String, String>(); private boolean _isAsyncSupported; //Filter(?) private Filter _filter; /** * Creates a new filter configuration object. */ public FilterConfigImpl(WebApp _webApp, ServletContext _servletContext, FilterManager _filterManager) { Assert.notNull(_webApp); Assert.notNull(_servletContext); Assert.notNull(_filterManager); this._webApp = _webApp; this._servletContext = _servletContext; this._filterManager = _filterManager; } /** * Sets the filter name. */ public void setFilterName(String name) { _filterName = name; } /** * Gets the filter name. */ @Override public String getFilterName() { return _filterName; } /** * Sets the filter class. */ public void setFilterClass(String filterClassName) throws ClassNotFoundException { _filterClassName = filterClassName; _filterClass = (Class<? extends Filter>) _webApp.getClassLoader().loadClass(filterClassName); } public void setFilterClass(Class<? extends Filter> filterClass) { _filterClass = filterClass; } /** * Gets the filter name. */ public Class<? extends Filter> getFilterClass() { return _filterClass; } /** * Gets the filter name. */ public String getFilterClassName() { return _filterClassName; } public void setFilter(Filter filter) { _filter = filter; } /** * Instantiates a Filter given its configuration. * * Filter(?,?) * * 1:Filter?,Filter, * ?(Filter#init(FilterConfig config) * * 2:?Filter. * * * @return the initialized servlet. */ public Filter getInstance() throws ServletException { // server/102e if (_filter != null) return _filter; _filter = createFilterAndInit(); return _filter; } /* * * * Filter,Filter,? * * Filter.init(this); */ public Filter createFilterAndInit() throws ServletException { if (_filter != null) return _filter; Class<? extends Filter> filterClass = getFilterClass(); if (filterClass == null) throw new NullPointerException("Null servlet class for " + _filterName); try { _filter = filterClass.newInstance(); } catch (Exception e) { throw new ServletException(e); } // ?Filter configureFilter(_filter); //? _filter.init(this); if (debug) log.debug("Filter[" + _filterName + "] instantiated and inited"); return _filter; } /** * Configure the servlet (everything that is done after instantiation but before servlet.init() */ void configureFilter(Object servlet) { } /** * Sets an init-param */ public void addInitParam(String param, String value) { _initParams.put(param, value); } /** * Gets the init params */ public Map<String, String> getInitParamMap() { return _initParams; } /** * Gets the init params */ @Override public String getInitParameter(String name) { return _initParams.get(name); } /** * Gets the init params */ @Override public Enumeration<String> getInitParameterNames() { return Collections.enumeration(_initParams.keySet()); } public WebApp getWebApp() { return _webApp; } /** * Returns the servlet context. */ public ServletContext getServletContext() { return _servletContext; } public FilterManager getFilterManager() { return _filterManager; } /** * Sets the display name */ public void setDisplayName(String displayName) { _displayName = displayName; } /** * Gets the display name */ public String getDisplayName() { return _displayName; } /** * ServletName <filter-mapping> <filter-name>EncodeFilter</filter-name> <servlet-name>FirstServlet</servlet-name> <servlet-name>SecondServlet</servlet-name> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> */ @Override public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) { try { FilterMapping mapping = new FilterMapping(this); if (dispatcherTypes != null) { for (DispatcherType dispatcherType : dispatcherTypes) { mapping.addDispatcherType(dispatcherType); } } for (String servletName : servletNames) mapping.addServletName(servletName); _webApp.addFilterMapping(mapping); } catch (Exception e) { // XXX: needs better exception handling throw new ConfigException(e.getMessage(), e); } } public Collection<String> getServletNameMappings() { Set<String> names = _filterManager.getServletNameMappings(_filterName); if (names == null) return Collections.EMPTY_SET; return Collections.unmodifiableSet(names); } /** * URL <filter-mapping> <filter-name>EncodeFilter</filter-name> <url-pattern>/FirstServlet</url-pattern> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ASYNC</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> */ @Override public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) { try { FilterMapping mapping = new FilterMapping(this); if (dispatcherTypes != null) { for (DispatcherType dispatcherType : dispatcherTypes) { mapping.addDispatcherType(dispatcherType); } } if (urlPatterns != null) { for (String pattern : urlPatterns) { mapping.addURLPattern(pattern); } } _webApp.addFilterMapping(mapping); } catch (Exception e) { // XXX: needs better exception handling throw new ConfigException(e.getMessage(), e); } } public Collection<String> getUrlPatternMappings() { Set<String> patterns = _filterManager.getUrlPatternMappings(_filterName); if (patterns == null) return Collections.EMPTY_SET; return Collections.unmodifiableSet(patterns); } @Override public String getName() { return _filterName; } public String getClassName() { return _filterClassName; } public boolean setInitParameter(String name, String value) { if (_initParams.containsKey(name)) return false; _initParams.put(name, value); return true; } public Set<String> setInitParameters(Map<String, String> initParameters) { Set<String> conflicts = new HashSet<String>(); for (Map.Entry<String, String> parameter : initParameters.entrySet()) { if (_initParams.containsKey(parameter.getKey())) conflicts.add(parameter.getKey()); else _initParams.put(parameter.getKey(), parameter.getValue()); } return conflicts; } public Map<String, String> getInitParameters() { return _initParams; } public void setAsyncSupported(boolean isAsyncSupported) { _isAsyncSupported = isAsyncSupported; } public boolean isAsyncSupported() { return _isAsyncSupported; } /** * Sets the description */ public void setDescription(String description) { } /** * Sets the icon */ public void setIcon(String icon) { } /** * Returns a printable representation of the filter config object. */ public String toString() { return "FilterConfigImpl[name=" + _filterName + ",class=" + _filterClass + "]"; } }