Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.servlet;
 
 import java.util.List;
 
 
 
 /* --------------------------------------------------------------------- */
 
 public class FilterHolder extends Holder<Filter>
 {
     private static final Logger LOG = Log.getLogger(FilterHolder.class);
     
     /* ------------------------------------------------------------ */
     private transient Filter _filter;
     private transient Config _config;
     private transient FilterRegistration.Dynamic _registration;
     
     /* ---------------------------------------------------------------- */
    
Constructor
 
     public FilterHolder()
     {
         super (.);
     }   
  
     
     /* ---------------------------------------------------------------- */
    
Constructor
 
     public FilterHolder(Holder.Source source)
     {
         super (source);
     }   
     
     /* ---------------------------------------------------------------- */
    
Constructor
 
     public FilterHolder(Class<? extends Filterfilter)
     {
         super (.);
         setHeldClass(filter);
     }
 
     /* ---------------------------------------------------------------- */
    
Constructor for existing filter.
 
     public FilterHolder(Filter filter)
     {
         super (.);
         setFilter(filter);
     }
     
     /* ------------------------------------------------------------ */
     @Override
     public void doStart()
         throws Exception
     {
         super.doStart();
         
         if (!javax.servlet.Filter.class
             .isAssignableFrom())
         {
             String msg = +" is not a javax.servlet.Filter";
             super.stop();
             throw new IllegalStateException(msg);
         }
        if (==null)
        {
            try
            {
            }
            catch (ServletException se)
            {
                Throwable cause = se.getRootCause();
                if (cause instanceof InstantiationException)
                    throw (InstantiationException)cause;
                if (cause instanceof IllegalAccessException)
                    throw (IllegalAccessException)cause;
                throw se;
            }
        }
        
        =new Config();
        .init();
    }
    /* ------------------------------------------------------------ */
    @Override
    public void doStop()
        throws Exception
    {      
        if (!=null)
        {
            try
            {
                destroyInstance();
            }
            catch (Exception e)
            {
                .warn(e);
            }
        }
        if (!)
            =null;
        
        =null;
        super.doStop();   
    }
    /* ------------------------------------------------------------ */
    @Override
    public void destroyInstance (Object o)
        throws Exception
    {
        if (o==null)
            return;
        Filter f = (Filter)o;
        f.destroy();
        getServletHandler().destroyFilter(f);
    }
    /* ------------------------------------------------------------ */
    public synchronized void setFilter(Filter filter)
    {
        =filter;
        =true;
        setHeldClass(filter.getClass());
        if (getName()==null)
            setName(filter.getClass().getName());
    }
    
    /* ------------------------------------------------------------ */
    public Filter getFilter()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        return getName();
    }
    
    /* ------------------------------------------------------------ */
    {
        if ( == null)
             = new Registration();
        return ;
    }
    
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    protected class Registration extends HolderRegistration implements FilterRegistration.Dynamic
    {
        public void addMappingForServletNames(EnumSet<DispatcherTypedispatcherTypesboolean isMatchAfterString... servletNames)
        {
            illegalStateIfContextStarted();
            FilterMapping mapping = new FilterMapping();
            mapping.setFilterHolder(FilterHolder.this);
            mapping.setServletNames(servletNames);
            mapping.setDispatcherTypes(dispatcherTypes);
            if (isMatchAfter)
                .addFilterMapping(mapping);
            else
                .prependFilterMapping(mapping);
        }
        public void addMappingForUrlPatterns(EnumSet<DispatcherTypedispatcherTypesboolean isMatchAfterString... urlPatterns)
        {
            illegalStateIfContextStarted();
            FilterMapping mapping = new FilterMapping();
            mapping.setFilterHolder(FilterHolder.this);
            mapping.setPathSpecs(urlPatterns);
            mapping.setDispatcherTypes(dispatcherTypes);
            if (isMatchAfter)
                .addFilterMapping(mapping);
            else
                .prependFilterMapping(mapping);
        }
        public Collection<StringgetServletNameMappings()
        {
            FilterMapping[] mappings =.getFilterMappings();
            List<Stringnames=new ArrayList<String>();
            for (FilterMapping mapping : mappings)
            {
                if (mapping.getFilterHolder()!=FilterHolder.this)
                    continue;
                String[] servlets=mapping.getServletNames();
                if (servlets!=null && servlets.length>0)
                    names.addAll(Arrays.asList(servlets));
            }
            return names;
        }
        public Collection<StringgetUrlPatternMappings()
        {
            FilterMapping[] mappings =.getFilterMappings();
            List<Stringpatterns=new ArrayList<String>();
            for (FilterMapping mapping : mappings)
            {
                if (mapping.getFilterHolder()!=FilterHolder.this)
                    continue;
                String[] specs=mapping.getPathSpecs();
                patterns.addAll(TypeUtil.asList(specs));
            }
            return patterns;
        }
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    class Config extends HolderConfig implements FilterConfig
    {
        /* ------------------------------------------------------------ */
        public String getFilterName()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X