View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.eclipse.jetty.server.Handler;
28  import org.eclipse.jetty.server.HandlerContainer;
29  import org.eclipse.jetty.util.TypeUtil;
30  
31  
32  /* ------------------------------------------------------------ */
33  /** Abstract Handler Container.
34   * This is the base class for handlers that may contain other handlers.
35   *
36   */
37  public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
38  {
39      /* ------------------------------------------------------------ */
40      public AbstractHandlerContainer()
41      {
42      }
43  
44      /* ------------------------------------------------------------ */
45      @Override
46      public Handler[] getChildHandlers()
47      {
48          List<Handler> list=new ArrayList<>();
49          expandChildren(list,null);
50          return list.toArray(new Handler[list.size()]);
51      }
52  
53      /* ------------------------------------------------------------ */
54      @Override
55      public Handler[] getChildHandlersByClass(Class<?> byclass)
56      {
57          List<Handler> list=new ArrayList<>();
58          expandChildren(list,byclass);
59          return list.toArray(new Handler[list.size()]);
60      }
61  
62      /* ------------------------------------------------------------ */
63      @Override
64      public <T extends Handler> T getChildHandlerByClass(Class<T> byclass)
65      {
66          List<Handler> list=new ArrayList<>();
67          expandChildren(list,byclass);
68          if (list.isEmpty())
69              return null;
70          return (T)list.get(0);
71      }
72  
73      /* ------------------------------------------------------------ */
74      protected void expandChildren(List<Handler> list, Class<?> byClass)
75      {
76      }
77  
78      /* ------------------------------------------------------------ */
79      protected void expandHandler(Handler handler, List<Handler> list, Class<?> byClass)
80      {
81          if (handler==null)
82              return;
83  
84          if (byClass==null || byClass.isAssignableFrom(handler.getClass()))
85              list.add(handler);
86  
87          if (handler instanceof AbstractHandlerContainer)
88              ((AbstractHandlerContainer)handler).expandChildren(list, byClass);
89          else if (handler instanceof HandlerContainer)
90          {
91              HandlerContainer container = (HandlerContainer)handler;
92              Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
93              list.addAll(Arrays.asList(handlers));
94          }
95      }
96  
97      /* ------------------------------------------------------------ */
98      public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root,Class<T>type, Handler handler)
99      {
100         if (root==null || handler==null)
101             return null;
102 
103         Handler[] branches=root.getChildHandlersByClass(type);
104         if (branches!=null)
105         {
106             for (Handler h:branches)
107             {
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 }