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  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.eclipse.jetty.server.Handler;
23  import org.eclipse.jetty.server.HandlerContainer;
24  import org.eclipse.jetty.util.LazyList;
25  import org.eclipse.jetty.util.TypeUtil;
26  
27  
28  /* ------------------------------------------------------------ */
29  /** Abstract Handler Container.
30   * This is the base class for handlers that may contain other handlers.
31   *
32   */
33  public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
34  {
35      /* ------------------------------------------------------------ */
36      public AbstractHandlerContainer()
37      {
38      }
39      
40      /* ------------------------------------------------------------ */
41      public Handler[] getChildHandlers()
42      {
43          Object list = expandChildren(null,null);
44          return (Handler[])LazyList.toArray(list, Handler.class);
45      }
46          
47      /* ------------------------------------------------------------ */
48      public Handler[] getChildHandlersByClass(Class<?> byclass)
49      {
50          Object list = expandChildren(null,byclass);
51          return (Handler[])LazyList.toArray(list, byclass);
52      }
53      
54      /* ------------------------------------------------------------ */
55      public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
56      {
57          // TODO this can be more efficient?
58          Object list = expandChildren(null,byclass);
59          if (list==null)
60              return null;
61          return (T)LazyList.get(list, 0);
62      }
63      
64      /* ------------------------------------------------------------ */
65      protected Object expandChildren(Object list, Class<?> byClass)
66      {
67          return list;
68      }
69  
70      /* ------------------------------------------------------------ */
71      protected Object expandHandler(Handler handler, Object list, Class<Handler> byClass)
72      {
73          if (handler==null)
74              return list;
75          
76          if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
77              list=LazyList.add(list, handler);
78  
79          if (handler instanceof AbstractHandlerContainer)
80              list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
81          else if (handler instanceof HandlerContainer)
82          {
83              HandlerContainer container = (HandlerContainer)handler;
84              Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
85              list=LazyList.addArray(list, handlers);
86          }
87          
88          return list;
89      }
90  
91      /* ------------------------------------------------------------ */
92      public void dump(Appendable out,String indent) throws IOException
93      {
94          dumpThis(out);
95          dump(out,indent,TypeUtil.asList(getHandlers()),getBeans());
96      }
97  }