View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.handler;
15  
16  
17  import java.io.IOException;
18  
19  import org.eclipse.jetty.server.Handler;
20  import org.eclipse.jetty.server.HandlerContainer;
21  import org.eclipse.jetty.util.LazyList;
22  import org.eclipse.jetty.util.TypeUtil;
23  
24  
25  /* ------------------------------------------------------------ */
26  /** Abstract Handler Container.
27   * This is the base class for handlers that may contain other handlers.
28   *
29   */
30  public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
31  {
32      /* ------------------------------------------------------------ */
33      public AbstractHandlerContainer()
34      {
35      }
36      
37      /* ------------------------------------------------------------ */
38      public Handler[] getChildHandlers()
39      {
40          Object list = expandChildren(null,null);
41          return (Handler[])LazyList.toArray(list, Handler.class);
42      }
43          
44      /* ------------------------------------------------------------ */
45      public Handler[] getChildHandlersByClass(Class<?> byclass)
46      {
47          Object list = expandChildren(null,byclass);
48          return (Handler[])LazyList.toArray(list, byclass);
49      }
50      
51      /* ------------------------------------------------------------ */
52      public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
53      {
54          // TODO this can be more efficient?
55          Object list = expandChildren(null,byclass);
56          if (list==null)
57              return null;
58          return (T)LazyList.get(list, 0);
59      }
60      
61      /* ------------------------------------------------------------ */
62      protected Object expandChildren(Object list, Class<?> byClass)
63      {
64          return list;
65      }
66  
67      /* ------------------------------------------------------------ */
68      protected Object expandHandler(Handler handler, Object list, Class<Handler> byClass)
69      {
70          if (handler==null)
71              return list;
72          
73          if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
74              list=LazyList.add(list, handler);
75  
76          if (handler instanceof AbstractHandlerContainer)
77              list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
78          else if (handler instanceof HandlerContainer)
79          {
80              HandlerContainer container = (HandlerContainer)handler;
81              Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
82              list=LazyList.addArray(list, handlers);
83          }
84          
85          return list;
86      }
87  
88      /* ------------------------------------------------------------ */
89      public void dump(Appendable out,String indent) throws IOException
90      {
91          dumpThis(out);
92          dump(out,indent,TypeUtil.asList(getHandlers()),getBeans());
93      }
94  }