All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.server.handler.AbstractHandlerContainer Maven / Gradle / Ivy

There is a newer version: 11.0.0.beta1
Show newest version
// ========================================================================
// Copyright (c) 2004-2009 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.server.handler;


import java.io.IOException;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HandlerContainer;
import org.eclipse.jetty.util.LazyList;


/* ------------------------------------------------------------ */
/** Abstract Handler Container.
 * This is the base class for handlers that may contain other handlers.
 *
 */
public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
{
    /* ------------------------------------------------------------ */
    public AbstractHandlerContainer()
    {
    }
    
    /* ------------------------------------------------------------ */
    public Handler[] getChildHandlers()
    {
        Object list = expandChildren(null,null);
        return (Handler[])LazyList.toArray(list, Handler.class);
    }
        
    /* ------------------------------------------------------------ */
    public Handler[] getChildHandlersByClass(Class byclass)
    {
        Object list = expandChildren(null,byclass);
        return (Handler[])LazyList.toArray(list, byclass);
    }
    
    /* ------------------------------------------------------------ */
    public Handler getChildHandlerByClass(Class byclass)
    {
        // TODO this can be more efficient?
        Object list = expandChildren(null,byclass);
        if (list==null)
            return null;
        return LazyList.get(list, 0);
    }
    
    /* ------------------------------------------------------------ */
    protected Object expandChildren(Object list, Class byClass)
    {
        return list;
    }

    /* ------------------------------------------------------------ */
    protected Object expandHandler(Handler handler, Object list, Class byClass)
    {
        if (handler==null)
            return list;
        
        if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
            list=LazyList.add(list, handler);

        if (handler instanceof AbstractHandlerContainer)
            list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
        else if (handler instanceof HandlerContainer)
        {
            HandlerContainer container = (HandlerContainer)handler;
            Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
            list=LazyList.addArray(list, handlers);
        }
        
        return list;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    protected void dump(Appendable out,String indent) throws IOException
    {
        super.dump(out,indent);
        dumpHandlers(out,indent);
    }
    
    /* ------------------------------------------------------------ */
    protected void dumpHandlers(Appendable out,String indent) throws IOException
    {
        Handler[] handlers = getHandlers();
        if (handlers!=null)
        {   
            int last=handlers.length-1;
            for (int h=0;h<=last;h++)
            {
                if (handlers[h]==null)
                    continue;
                out.append(indent);
                out.append(" +-");
                if (handlers[h] instanceof AbstractHandler)
                    ((AbstractHandler)handlers[h]).dump(out,indent+((h==last)?"   ":" | "));
                else
                {
                    out.append(String.valueOf(handlers[h]));
                    out.append("\n");
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy