]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / AbstractHandlerContainer.java
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.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.eclipse.jetty.server.Handler;
27 import org.eclipse.jetty.server.HandlerContainer;
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     @Override
44     public Handler[] getChildHandlers()
45     {
46         List<Handler> list=new ArrayList<>();
47         expandChildren(list,null);
48         return list.toArray(new Handler[list.size()]);
49     }
50
51     /* ------------------------------------------------------------ */
52     @Override
53     public Handler[] getChildHandlersByClass(Class<?> byclass)
54     {
55         List<Handler> list=new ArrayList<>();
56         expandChildren(list,byclass);
57         return list.toArray(new Handler[list.size()]);
58     }
59
60     /* ------------------------------------------------------------ */
61     @Override
62     public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
63     {
64         List<Handler> list=new ArrayList<>();
65         expandChildren(list,byclass);
66         if (list.isEmpty())
67             return null;
68         return (T)list.get(0);
69     }
70
71     /* ------------------------------------------------------------ */
72     protected void expandChildren(List<Handler> list, Class<?> byClass)
73     {
74     }
75
76     /* ------------------------------------------------------------ */
77     protected void expandHandler(Handler handler, List<Handler> list, Class<?> byClass)
78     {
79         if (handler==null)
80             return;
81
82         if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
83             list.add(handler);
84
85         if (handler instanceof AbstractHandlerContainer)
86             ((AbstractHandlerContainer)handler).expandChildren(list, byClass);
87         else if (handler instanceof HandlerContainer)
88         {
89             HandlerContainer container = (HandlerContainer)handler;
90             Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
91             list.addAll(Arrays.asList(handlers));
92         }
93     }
94
95     /* ------------------------------------------------------------ */
96     public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, Handler handler)
97     {
98         if (root==null || handler==null)
99             return null;
100
101         Handler[] branches=root.getChildHandlersByClass(type);
102         if (branches!=null)
103         {
104             for (Handler h:branches)
105             {
106                 T container = (T)h;
107                 Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
108                 if (candidates!=null)
109                 {
110                     for (Handler c:candidates)
111                         if (c==handler)
112                             return container;
113                 }
114             }
115         }
116         return null;
117     }
118 }