FileDocCategorySizeDatePackage
ApplicationFilterConfig.javaAPI DocGlassfish v2 API12211Fri May 04 22:31:54 BST 2007org.apache.catalina.core

ApplicationFilterConfig

public final class ApplicationFilterConfig extends Object implements Serializable, FilterConfig
Implementation of a javax.servlet.FilterConfig useful in managing the filter instances instantiated when a web application is first started.
author
Craig R. McClanahan
version
$Revision: 1.7 $ $Date: 2007/05/05 05:31:53 $

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
private StandardContext
context
The Context with which we are associated.
private transient Filter
filter
The application Filter we are configured for.
private org.apache.catalina.deploy.FilterDef
filterDef
The FilterDef that defines our associated Filter.
Constructors Summary
public ApplicationFilterConfig(StandardContext context, org.apache.catalina.deploy.FilterDef filterDef)
Construct a new ApplicationFilterConfig for the specified filter definition.

param
context The context with which we are associated
param
filterDef Filter definition for which a FilterConfig is to be constructed
exception
ClassCastException if the specified class does not implement the javax.servlet.Filter interface
exception
ClassNotFoundException if the filter class cannot be found
exception
IllegalAccessException if the filter class cannot be publicly instantiated
exception
InstantiationException if an exception occurs while instantiating the filter object
exception
ServletException if thrown by the filter's init() method

 
    // ----------------------------------------------------------- Constructors


                                                                                              
        
          
                
                

        super();
        this.context = context;
        setFilterDef(filterDef);

    
Methods Summary
javax.servlet.FiltergetFilter()
Return the application Filter we are configured for.

exception
ClassCastException if the specified class does not implement the javax.servlet.Filter interface
exception
ClassNotFoundException if the filter class cannot be found
exception
IllegalAccessException if the filter class cannot be publicly instantiated
exception
InstantiationException if an exception occurs while instantiating the filter object
exception
ServletException if thrown by the filter's init() method


        // Return the existing filter instance, if any
        if (this.filter != null)
            return (this.filter);

        // Identify the class loader we will be using
        String filterClass = filterDef.getFilterClass();
        ClassLoader classLoader = null;
        if (filterClass.startsWith("org.apache.catalina."))
            classLoader = this.getClass().getClassLoader();
        else
            classLoader = context.getLoader().getClassLoader();

        ClassLoader oldCtxClassLoader =
            Thread.currentThread().getContextClassLoader();

        // Instantiate a new instance of this filter and return it
        Class clazz = classLoader.loadClass(filterClass);
        this.filter = (Filter) clazz.newInstance();

        // START PWC 1.2
        if (context != null) {
            context.fireContainerEvent(
                ContainerEvent.BEFORE_FILTER_INITIALIZED,
                filter);
        }
        // END PWC 1.2

        if (context.getSwallowOutput()) {
            try {
                SystemLogHandler.startCapture();
                filter.init(this);
            } finally {
                String log = SystemLogHandler.stopCapture();
                if (log != null && log.length() > 0) {
                    getServletContext().log(log);
                }
            }
        } else {
            filter.init(this);
        }

        // START PWC 1.2
        if (context != null) {
            context.fireContainerEvent(ContainerEvent.AFTER_FILTER_INITIALIZED,
                                       filter);
        }
        // END PWC 1.2

        return (this.filter);

    
org.apache.catalina.deploy.FilterDefgetFilterDef()
Return the filter definition we are configured for.


        return (this.filterDef);

    
public java.lang.StringgetFilterName()
Return the name of the filter we are configuring.



    // --------------------------------------------------- FilterConfig Methods


                  
       

        return (filterDef.getFilterName());

    
public java.lang.StringgetInitParameter(java.lang.String name)
Return a String containing the value of the named initialization parameter, or null if the parameter does not exist.

param
name Name of the requested initialization parameter


        Map map = filterDef.getParameterMap();
        if (map == null)
            return (null);
        else
            return ((String) map.get(name));

    
public java.util.EnumerationgetInitParameterNames()
Return an Enumeration of the names of the initialization parameters for this Filter.


        Map map = filterDef.getParameterMap();
        if (map == null)
            return (new Enumerator(new ArrayList()));
        else
            return (new Enumerator(map.keySet()));

    
public javax.servlet.ServletContextgetServletContext()
Return the ServletContext of our associated web application.


        return (this.context.getServletContext());

    
voidrelease()
Release the Filter instance associated with this FilterConfig, if there is one.


        if (this.filter != null){

            if (context != null) {
                context.fireContainerEvent(
                    ContainerEvent.BEFORE_FILTER_DESTROYED,
                    filter);
            }

            // START SJS WS 7.0 6236329
            //if( System.getSecurityManager() != null) {
            if ( SecurityUtil.executeUnderSubjectDoAs() ){
            // END OF SJS WS 7.0 6236329
                try{
                    SecurityUtil.doAsPrivilege("destroy",
                                               filter); 
                    SecurityUtil.remove(filter);
                } catch(java.lang.Exception ex){                    
                    log.error("ApplicationFilterConfig.doAsPrivilege", ex);
                }
            } else { 
                filter.destroy();
            }

            if (context != null) {
                context.fireContainerEvent(
                    ContainerEvent.AFTER_FILTER_DESTROYED,
                    filter);
            }

        }

        this.filter = null;

     
voidsetFilterDef(org.apache.catalina.deploy.FilterDef filterDef)
Set the filter definition we are configured for. This has the side effect of instantiating an instance of the corresponding filter class.

param
filterDef The new filter definition
exception
ClassCastException if the specified class does not implement the javax.servlet.Filter interface
exception
ClassNotFoundException if the filter class cannot be found
exception
IllegalAccessException if the filter class cannot be publicly instantiated
exception
InstantiationException if an exception occurs while instantiating the filter object
exception
ServletException if thrown by the filter's init() method


        this.filterDef = filterDef;
        if (filterDef == null) {

            // Release any previously allocated filter instance
            if (this.filter != null){
                // START SJS WS 7.0 6236329
                //if( System.getSecurityManager() != null) {
                if ( SecurityUtil.executeUnderSubjectDoAs() ){
                // END OF SJS WS 7.0 6236329
                    try{
                        SecurityUtil.doAsPrivilege("destroy",
                                                   filter);  
                        SecurityUtil.remove(filter);
                    } catch(java.lang.Exception ex){    
                        log.error("ApplicationFilterConfig.doAsPrivilege", ex);
                    }
                } else { 
                    filter.destroy();
                }
            }
            this.filter = null;

        } else {

            // Allocate a new filter instance
            Filter filter = getFilter();

        }

    
public java.lang.StringtoString()
Return a String representation of this object.


        StringBuffer sb = new StringBuffer("ApplicationFilterConfig[");
        sb.append("name=");
        sb.append(filterDef.getFilterName());
        sb.append(", filterClass=");
        sb.append(filterDef.getFilterClass());
        sb.append("]");
        return (sb.toString());