]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / AbstractHandlerContainer.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 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     @SuppressWarnings("unchecked")
62     @Override
63     public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
64     {
65         List<Handler> list=new ArrayList<>();
66         expandChildren(list,byclass);
67         if (list.isEmpty())
68             return null;
69         return (T)list.get(0);
70     }
71
72     /* ------------------------------------------------------------ */
73     protected void expandChildren(List<Handler> list, Class<?> byClass)
74     {
75     }
76
77     /* ------------------------------------------------------------ */
78     protected void expandHandler(Handler handler, List<Handler> list, Class<?> byClass)
79     {
80         if (handler==null)
81             return;
82
83         if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
84             list.add(handler);
85
86         if (handler instanceof AbstractHandlerContainer)
87             ((AbstractHandlerContainer)handler).expandChildren(list, byClass);
88         else if (handler instanceof HandlerContainer)
89         {
90             HandlerContainer container = (HandlerContainer)handler;
91             Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
92             list.addAll(Arrays.asList(handlers));
93         }
94     }
95
96     /* ------------------------------------------------------------ */
97     public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, Handler handler)
98     {
99         if (root==null || handler==null)
100             return null;
101
102         Handler[] branches=root.getChildHandlersByClass(type);
103         if (branches!=null)
104         {
105             for (Handler h:branches)
106             {
107                 @SuppressWarnings("unchecked")
108                 T container = (T)h;
109                 Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
110                 if (candidates!=null)
111                 {
112                     for (Handler c:candidates)
113                         if (c==handler)
114                             return container;
115                 }
116             }
117         }
118         return null;
119     }
120 }