Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.core;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.util.Map;
 import java.util.Set;
 
 
Implementation of a javax.servlet.FilterConfig useful in managing the filter instances instantiated when a web application is first started.

Author(s):
Craig R. McClanahan
Remy Maucherat
Version:
$Revision: 1673 $ $Date: 2011-03-12 18:58:07 +0100 (Sat, 12 Mar 2011) $
 
 
 public final class ApplicationFilterConfig implements FilterConfigSerializable {
 
 
     // ----------------------------------------------------------- Constructors
 

    
Construct a new ApplicationFilterConfig for the specified filter definition.

Parameters:
context The context with which we are associated
filterDef Filter definition for which a FilterConfig is to be constructed
 
     public ApplicationFilterConfig(Context contextFilterDef filterDef) {
         this. = context;
         this. = filterDef;
     }
 
 
     // ----------------------------------------------------- Instance Variables
 

    
The facade associated with this wrapper.
 
     protected ApplicationFilterConfigFacade facade =
         new ApplicationFilterConfigFacade(this);


    
The Context with which we are associated.
 
     private transient Context context = null;


    
Dynamic flag.
    protected boolean dynamic = false;
    
    
    
The application Filter we are configured for.
    private transient Filter filter = null;


    
The application Filter we are configured for.
    private transient Filter filterInstance = null;


    
The FilterDef that defines our associated Filter.
    private FilterDef filterDef = null;


    
JMX registration name
    private ObjectName oname;
    // --------------------------------------------------- FilterConfig Methods


    
Return the name of the filter we are configuring.
    public String getFilterName() {
        return (.getFilterName());
    }


    
Return the class of the filter we are configuring.
    public String getFilterClass() {
        return .getFilterClass();
    }


    
Return a String containing the value of the named initialization parameter, or null if the parameter does not exist.

Parameters:
name Name of the requested initialization parameter
    public String getInitParameter(String name) {
        Map<StringStringmap = .getParameterMap();
        if (map == null) {
            return (null);
        } else {
            return map.get(name);
        }
    }


    
Return an Enumeration of the names of the initialization parameters for this Filter.
        Map<StringStringmap = .getParameterMap();
        if (map == null) {
            return (new Enumerator(new ArrayList<String>()));
        } else {
            return (new Enumerator(map.keySet()));
        }
    }


    
Return the ServletContext of our associated web application.
    public ServletContext getServletContext() {
        return (this..getServletContext());
    }


    
Get the facade FilterRegistration.
    public FilterRegistration getFacade() {
        return ;
    }
    
    public boolean isDynamic() {
        return ;
    }
    public void setDynamic(boolean dynamic) {
        this. = dynamic;
        if (dynamic) {
            // Change the facade (normally, this happens when the Wrapper is created)
             = new ApplicationFilterConfigFacade.Dynamic(this);
        }
    }


    
Return a String representation of this object.
    public String toString() {
        StringBuilder sb = new StringBuilder("ApplicationFilterConfig[");
        sb.append("name=");
        sb.append(.getFilterName());
        sb.append(", filterClass=");
        sb.append(.getFilterClass());
        sb.append("]");
        return (sb.toString());
    }
    public boolean addMappingForServletNames(EnumSet<DispatcherTypedispatcherTypes
            boolean isMatchAfterString... servletNames) {
        if (!.isStarting()) {
        }
        if (servletNames == null || servletNames.length == 0) {
            throw .invalidFilterRegistrationArguments();
        }
        FilterMap filterMap = new FilterMap(); 
        for (String servletName : servletNames) {
            filterMap.addServletName(servletName);
        }
        filterMap.setFilterName(.getFilterName());
        if (dispatcherTypes != null) {
            for (DispatcherType dispatcherTypedispatcherTypes) {
                filterMap.setDispatcher(dispatcherType.name());
            }
        }
        if (isMatchAfter) {
            .addFilterMap(filterMap);
        } else {
            .addFilterMapBefore(filterMap);
        }
        return true;
    }
    public boolean addMappingForUrlPatterns(
            EnumSet<DispatcherTypedispatcherTypesboolean isMatchAfter,
            String... urlPatterns) {
        if (!.isStarting()) {
        }
        if (urlPatterns == null || urlPatterns.length == 0) {
            throw .invalidFilterRegistrationArguments();
        }
        FilterMap filterMap = new FilterMap(); 
        for (String urlPattern : urlPatterns) {
            filterMap.addURLPattern(urlPattern);
        }
        filterMap.setFilterName(.getFilterName());
        if (dispatcherTypes != null) {
            for (DispatcherType dispatcherTypedispatcherTypes) {
                filterMap.setDispatcher(dispatcherType.name());
            }
        }
        if (isMatchAfter) {
            .addFilterMap(filterMap);
        } else {
            .addFilterMapBefore(filterMap);
        }
        return true;
    }
        HashSet<Stringresult = new HashSet<String>();
        FilterMap[] filterMaps = .findFilterMaps();
        for (int i = 0; i < filterMaps.lengthi++) {
            if (.getFilterName().equals(filterMaps[i].getFilterName())) {
                FilterMap filterMap = filterMaps[i];
                String[] maps = filterMap.getServletNames();
                for (int j = 0; j < maps.lengthj++) {
                    result.add(maps[j]);
                }
                if (filterMap.getMatchAllServletNames()) {
                    result.add("*");
                }
            }
        }
        return Collections.unmodifiableSet(result);
    }
        HashSet<Stringresult = new HashSet<String>();
        FilterMap[] filterMaps = .findFilterMaps();
        for (int i = 0; i < filterMaps.lengthi++) {
            if (.getFilterName().equals(filterMaps[i].getFilterName())) {
                FilterMap filterMap = filterMaps[i];
                String[] maps = filterMap.getURLPatterns();
                for (int j = 0; j < maps.lengthj++) {
                    result.add(maps[j]);
                }
                if (filterMap.getMatchAllUrlPatterns()) {
                    result.add("*");
                }
            }
        }
        return Collections.unmodifiableSet(result);
    }
    public void setAsyncSupported(boolean asyncSupported) {
        if (!.isStarting()) {
        }
        .setAsyncSupported(asyncSupported);
    }
    public void setDescription(String description) {
        .setDescription(description);
    }
    public boolean setInitParameter(String nameString value) {
        if (!.isStarting()) {
        }
        if (name == null || value == null) {
            throw .invalidFilterRegistrationArguments();
        }
        if (.getInitParameter(name) != null) {
            return false;
        }
        .addInitParameter(namevalue);
        return true;
    }
    public Set<StringsetInitParameters(Map<StringStringinitParameters) {
        if (!.isStarting()) {
        }
        if (initParameters == null) {
            throw .invalidFilterRegistrationArguments();
        }
        Set<Stringconflicts = new HashSet<String>();
        Iterator<StringparameterNames = initParameters.keySet().iterator();
        while (parameterNames.hasNext()) {
            String parameterName = parameterNames.next();
            if (.getInitParameter(parameterName) != null) {
                conflicts.add(parameterName);
            } else {
                String value = initParameters.get(parameterName);
                if (value == null) {
                    throw .invalidFilterRegistrationArguments();
                }
                .addInitParameter(parameterNamevalue);
            }
        }
        return conflicts;
    }
    public Map<StringStringgetInitParameters() {
        return Collections.unmodifiableMap(.getParameterMap());
    }
    // -------------------------------------------------------- Package Methods


    
Return the application Filter we are configured for.

Throws:
java.lang.ClassCastException if the specified class does not implement the javax.servlet.Filter interface
java.lang.ClassNotFoundException if the filter class cannot be found
java.lang.IllegalAccessException if the filter class cannot be publicly instantiated
java.lang.InstantiationException if an exception occurs while instantiating the filter object
javax.servlet.ServletException if thrown by the filter's init() method
javax.naming.NamingException
java.lang.reflect.InvocationTargetException
        // Return the existing filter instance, if any
        if (this. != null)
            return (this.);
        // Identify the class loader we will be using
        if ( == null) {
            String filterClass = .getFilterClass();
            this. = (Filter.getInstanceManager().newInstance(filterClass);
        } else {
            this. = ;
             = null;
        }
        .init(this);
        
        // Expose filter via JMX
        if (.....) {
            registerJMX();
        }
        
        return (this.);
    }

    
    
Set the filter instance programmatically.
    public void setFilter(Filter filter) {
         = filter;
    }
    

    
Return the filter instance.
    public Filter getFilterInstance() {
        return ( != null) ?  : ;
    }
    

    
Return the filter definition we are configured for.
    public FilterDef getFilterDef() {
        return (this.);
    }

    
Release the Filter instance associated with this FilterConfig, if there is one.
    void release() {
        if (.....) {
            unregsiterJMX();
        }
        
        if (this. != null)
        {
            try {
                if (.) {
                    SecurityUtil.doAsPrivilege("destroy");
                    SecurityUtil.remove();
                } else {
                    .destroy();
                }
            } catch(java.lang.Exception ex){
                .getLogger().error(.errorDestroyingFilter(getFilterName()), ex);
            }
            try {
                ((StandardContext).getInstanceManager().destroyInstance(this.);
            } catch (Exception e) {
                .getLogger().error(.preDestroyException(), e);
            }
        }
        this. = null;
     }
    // -------------------------------------------------------- Private Methods
    private void registerJMX() {
        String parentName = .getName();
        parentName = ("".equals(parentName)) ? "/" : parentName;
        String hostName = .getParent().getName();
        hostName = (hostName == null) ? "DEFAULT" : hostName;
        // domain == engine name
        String domain = .getParent().getParent().getName();
        String webMod = "//" + hostName + parentName;
        String onameStr = null;
        if ( instanceof StandardContext) {
            StandardContext standardContext = (StandardContext;
            onameStr = domain + ":j2eeType=Filter,name=" +
                 .getFilterName() + ",WebModule=" + webMod +
                 ",J2EEApplication=" +
                 standardContext.getJ2EEApplication() + ",J2EEServer=" +
                 standardContext.getJ2EEServer();
        } else {
            onameStr = domain + ":j2eeType=Filter,name=" +
                 .getFilterName() + ",WebModule=" + webMod;
        }
        try {
             = new ObjectName(onameStr);
            Registry.getRegistry(nullnull).registerComponent(this,
                    null);
        } catch (Exception ex) {
        }
    }
    
    private void unregsiterJMX() {
        // unregister this component
        if ( != null) {
            try {
                Registry.getRegistry(nullnull).unregisterComponent();
            } catch(Exception ex) {
            }
        }
    }
New to GrepCode? Check out our FAQ X