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