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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 import org.eclipse.jetty.server.Handler;
27 import org.eclipse.jetty.server.HandlerContainer;
30 /* ------------------------------------------------------------ */
31 /** Abstract Handler Container.
32 * This is the base class for handlers that may contain other handlers.
35 public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
37 /* ------------------------------------------------------------ */
38 public AbstractHandlerContainer()
42 /* ------------------------------------------------------------ */
44 public Handler[] getChildHandlers()
46 List<Handler> list=new ArrayList<>();
47 expandChildren(list,null);
48 return list.toArray(new Handler[list.size()]);
51 /* ------------------------------------------------------------ */
53 public Handler[] getChildHandlersByClass(Class<?> byclass)
55 List<Handler> list=new ArrayList<>();
56 expandChildren(list,byclass);
57 return list.toArray(new Handler[list.size()]);
60 /* ------------------------------------------------------------ */
62 public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
64 List<Handler> list=new ArrayList<>();
65 expandChildren(list,byclass);
68 return (T)list.get(0);
71 /* ------------------------------------------------------------ */
72 protected void expandChildren(List<Handler> list, Class<?> byClass)
76 /* ------------------------------------------------------------ */
77 protected void expandHandler(Handler handler, List<Handler> list, Class<?> byClass)
82 if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
85 if (handler instanceof AbstractHandlerContainer)
86 ((AbstractHandlerContainer)handler).expandChildren(list, byClass);
87 else if (handler instanceof HandlerContainer)
89 HandlerContainer container = (HandlerContainer)handler;
90 Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
91 list.addAll(Arrays.asList(handlers));
95 /* ------------------------------------------------------------ */
96 public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, Handler handler)
98 if (root==null || handler==null)
101 Handler[] branches=root.getChildHandlersByClass(type);
104 for (Handler h:branches)
107 Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
108 if (candidates!=null)
110 for (Handler c:candidates)