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 org.eclipse.jetty.server.Handler;
18  import org.eclipse.jetty.server.HandlerContainer;
19  import org.eclipse.jetty.util.LazyList;
20  
21  
22  /* ------------------------------------------------------------ */
23  /** Abstract Handler Container.
24   * This is the base class for handlers that may contain other handlers.
25   *
26   */
27  public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
28  {
29      /* ------------------------------------------------------------ */
30      public AbstractHandlerContainer()
31      {
32      }
33      
34      /* ------------------------------------------------------------ */
35      public Handler[] getChildHandlers()
36      {
37          Object list = expandChildren(null,null);
38          return (Handler[])LazyList.toArray(list, Handler.class);
39      }
40          
41      /* ------------------------------------------------------------ */
42      public Handler[] getChildHandlersByClass(Class<?> byclass)
43      {
44          Object list = expandChildren(null,byclass);
45          return (Handler[])LazyList.toArray(list, byclass);
46      }
47      
48      /* ------------------------------------------------------------ */
49      public Handler getChildHandlerByClass(Class<?> byclass)
50      {
51          // TODO this can be more efficient?
52          Object list = expandChildren(null,byclass);
53          if (list==null)
54              return null;
55          return LazyList.get(list, 0);
56      }
57      
58      /* ------------------------------------------------------------ */
59      protected Object expandChildren(Object list, Class<?> byClass)
60      {
61          return list;
62      }
63  
64      /* ------------------------------------------------------------ */
65      protected Object expandHandler(Handler handler, Object list, Class<Handler> byClass)
66      {
67          if (handler==null)
68              return list;
69          
70          if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
71              list=LazyList.add(list, handler);
72  
73          if (handler instanceof AbstractHandlerContainer)
74              list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
75          else if (handler instanceof HandlerContainer)
76          {
77              HandlerContainer container = (HandlerContainer)handler;
78              Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
79              list=LazyList.addArray(list, handlers);
80          }
81          
82          return list;
83      }
84      
85      /* ------------------------------------------------------------ */
86      @Override
87      protected void dump(StringBuilder b,String indent)
88      {
89          super.dump(b,indent);
90  
91          Handler[] handlers = getHandlers();
92          if (handlers!=null)
93          {   
94              int last=handlers.length-1;
95              for (int h=0;h<=last;h++)
96              {
97                  b.append(indent);
98                  b.append(" +-");
99                  if (handlers[h] instanceof AbstractHandler)
100                     ((AbstractHandler)handlers[h]).dump(b,indent+((h==last)?"   ":" | "));
101                 else
102                 {
103                     b.append(handlers[h]);
104                     b.append("\n");
105                 }
106             }
107         }
108     }
109 }