1
2
3
4
5
6
7
8
9
10
11
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
26
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
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 }