View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import java.io.IOException;
22  import java.util.List;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.eclipse.jetty.server.Handler;
29  import org.eclipse.jetty.server.Request;
30  import org.eclipse.jetty.server.Server;
31  import org.eclipse.jetty.util.ArrayUtil;
32  import org.eclipse.jetty.util.MultiException;
33  import org.eclipse.jetty.util.annotation.ManagedAttribute;
34  import org.eclipse.jetty.util.annotation.ManagedObject;
35  
36  /* ------------------------------------------------------------ */
37  /** A collection of handlers.
38   * <p>
39   * The default implementations  calls all handlers in list order,
40   * regardless of the response status or exceptions. Derived implementation
41   * may alter the order or the conditions of calling the contained
42   * handlers.
43   * <p>
44   *
45   */
46  @ManagedObject("Handler of multiple handlers")
47  public class HandlerCollection extends AbstractHandlerContainer
48  {
49      private final boolean _mutableWhenRunning;
50      private volatile Handler[] _handlers;
51  
52      /* ------------------------------------------------------------ */
53      public HandlerCollection()
54      {
55          _mutableWhenRunning=false;
56      }
57  
58      /* ------------------------------------------------------------ */
59      public HandlerCollection(boolean mutableWhenRunning)
60      {
61          _mutableWhenRunning=mutableWhenRunning;
62      }
63  
64      /* ------------------------------------------------------------ */
65      /**
66       * @return Returns the handlers.
67       */
68      @Override
69      @ManagedAttribute(value="Wrapped handlers", readonly=true)
70      public Handler[] getHandlers()
71      {
72          return _handlers;
73      }
74  
75      /* ------------------------------------------------------------ */
76      /**
77       * @param handlers The handlers to set.
78       */
79      public void setHandlers(Handler[] handlers)
80      {
81          if (!_mutableWhenRunning && isStarted())
82              throw new IllegalStateException(STARTED);
83  
84          if (handlers!=null)
85              for (Handler handler:handlers)
86                  if (handler.getServer()!=getServer())
87                      handler.setServer(getServer());
88          
89          updateBeans(_handlers, handlers);
90          _handlers = handlers;
91      }
92  
93      /* ------------------------------------------------------------ */
94      /**
95       * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse)
96       */
97      @Override
98      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
99          throws IOException, ServletException
100     {
101         if (_handlers!=null && isStarted())
102         {
103             MultiException mex=null;
104 
105             for (int i=0;i<_handlers.length;i++)
106             {
107                 try
108                 {
109                     _handlers[i].handle(target,baseRequest, request, response);
110                 }
111                 catch(IOException e)
112                 {
113                     throw e;
114                 }
115                 catch(RuntimeException e)
116                 {
117                     throw e;
118                 }
119                 catch(Exception e)
120                 {
121                     if (mex==null)
122                         mex=new MultiException();
123                     mex.add(e);
124                 }
125             }
126             if (mex!=null)
127             {
128                 if (mex.size()==1)
129                     throw new ServletException(mex.getThrowable(0));
130                 else
131                     throw new ServletException(mex);
132             }
133 
134         }
135     }
136 
137     /* ------------------------------------------------------------ */
138     @Override
139     public void setServer(Server server)
140     {
141         super.setServer(server);
142         Handler[] handlers=getHandlers();
143         if (handlers!=null)
144             for (Handler h : handlers)
145                 h.setServer(server);
146     }
147 
148     /* ------------------------------------------------------------ */
149     /* Add a handler.
150      * This implementation adds the passed handler to the end of the existing collection of handlers.
151      * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler)
152      */
153     public void addHandler(Handler handler)
154     {
155         setHandlers(ArrayUtil.addToArray(getHandlers(), handler, Handler.class));
156     }
157 
158     /* ------------------------------------------------------------ */
159     public void removeHandler(Handler handler)
160     {
161         Handler[] handlers = getHandlers();
162 
163         if (handlers!=null && handlers.length>0 )
164             setHandlers(ArrayUtil.removeFromArray(handlers, handler));
165     }
166 
167     /* ------------------------------------------------------------ */
168     @Override
169     protected void expandChildren(List<Handler> list, Class<?> byClass)
170     {
171         if (getHandlers()!=null)
172             for (Handler h:getHandlers())
173                 expandHandler(h, list, byClass);
174     }
175 
176     /* ------------------------------------------------------------ */
177     @Override
178     public void destroy()
179     {
180         if (!isStopped())
181             throw new IllegalStateException("!STOPPED");
182         Handler[] children=getChildHandlers();
183         setHandlers(null);
184         for (Handler child: children)
185             child.destroy();
186         super.destroy();
187     }
188 }