comparison src/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.server.handler;
20
21
22 import java.io.IOException;
23
24 import org.eclipse.jetty.server.Handler;
25 import org.eclipse.jetty.server.HandlerContainer;
26 import org.eclipse.jetty.util.LazyList;
27 import org.eclipse.jetty.util.TypeUtil;
28
29
30 /* ------------------------------------------------------------ */
31 /** Abstract Handler Container.
32 * This is the base class for handlers that may contain other handlers.
33 *
34 */
35 public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
36 {
37 /* ------------------------------------------------------------ */
38 public AbstractHandlerContainer()
39 {
40 }
41
42 /* ------------------------------------------------------------ */
43 public Handler[] getChildHandlers()
44 {
45 Object list = expandChildren(null,null);
46 return (Handler[])LazyList.toArray(list, Handler.class);
47 }
48
49 /* ------------------------------------------------------------ */
50 public Handler[] getChildHandlersByClass(Class<?> byclass)
51 {
52 Object list = expandChildren(null,byclass);
53 return (Handler[])LazyList.toArray(list, byclass);
54 }
55
56 /* ------------------------------------------------------------ */
57 public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
58 {
59 // TODO this can be more efficient?
60 Object list = expandChildren(null,byclass);
61 if (list==null)
62 return null;
63 return (T)LazyList.get(list, 0);
64 }
65
66 /* ------------------------------------------------------------ */
67 protected Object expandChildren(Object list, Class<?> byClass)
68 {
69 return list;
70 }
71
72 /* ------------------------------------------------------------ */
73 protected Object expandHandler(Handler handler, Object list, Class<Handler> byClass)
74 {
75 if (handler==null)
76 return list;
77
78 if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
79 list=LazyList.add(list, handler);
80
81 if (handler instanceof AbstractHandlerContainer)
82 list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
83 else if (handler instanceof HandlerContainer)
84 {
85 HandlerContainer container = (HandlerContainer)handler;
86 Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
87 list=LazyList.addArray(list, handlers);
88 }
89
90 return list;
91 }
92
93 /* ------------------------------------------------------------ */
94 public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, Handler handler)
95 {
96 if (root==null || handler==null)
97 return null;
98
99 Handler[] branches=root.getChildHandlersByClass(type);
100 if (branches!=null)
101 {
102 for (Handler h:branches)
103 {
104 T container = (T)h;
105 Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
106 if (candidates!=null)
107 {
108 for (Handler c:candidates)
109 if (c==handler)
110 return container;
111 }
112 }
113 }
114 return null;
115 }
116
117 /* ------------------------------------------------------------ */
118 public void dump(Appendable out,String indent) throws IOException
119 {
120 dumpThis(out);
121 dump(out,indent,getBeans(),TypeUtil.asList(getHandlers()));
122 }
123 }