View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  import org.eclipse.jetty.server.Server;
29  
30  
31  /* ------------------------------------------------------------ */
32  /** Abstract Handler Container.
33   * This is the base class for handlers that may contain other handlers.
34   *
35   */
36  public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
37  {
38      /* ------------------------------------------------------------ */
39      public AbstractHandlerContainer()
40      {
41      }
42  
43      /* ------------------------------------------------------------ */
44      @Override
45      public Handler[] getChildHandlers()
46      {
47          List<Handler> list=new ArrayList<>();
48          expandChildren(list,null);
49          return list.toArray(new Handler[list.size()]);
50      }
51  
52      /* ------------------------------------------------------------ */
53      @Override
54      public Handler[] getChildHandlersByClass(Class<?> byclass)
55      {
56          List<Handler> list=new ArrayList<>();
57          expandChildren(list,byclass);
58          return list.toArray(new Handler[list.size()]);
59      }
60  
61      /* ------------------------------------------------------------ */
62      @SuppressWarnings("unchecked")
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                 @SuppressWarnings("unchecked")
109                 T container = (T)h;
110                 Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
111                 if (candidates!=null)
112                 {
113                     for (Handler c:candidates)
114                         if (c==handler)
115                             return container;
116                 }
117             }
118         }
119         return null;
120     }
121 
122     /* ------------------------------------------------------------ */
123     @Override
124     public void setServer(Server server)
125     {
126         if (server==getServer())
127             return;
128         
129         if (isStarted())
130             throw new IllegalStateException(STARTED);
131 
132         super.setServer(server);
133         Handler[] handlers=getHandlers();
134         if (handlers!=null)
135             for (Handler h : handlers)
136                 h.setServer(server);
137     }
138 }