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.
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;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.eclipse.jetty.server.Handler;
30 import org.eclipse.jetty.server.Request;
31 import org.eclipse.jetty.server.Server;
32 import org.eclipse.jetty.util.ArrayUtil;
33 import org.eclipse.jetty.util.MultiException;
34 import org.eclipse.jetty.util.annotation.ManagedAttribute;
35 import org.eclipse.jetty.util.annotation.ManagedObject;
37 /* ------------------------------------------------------------ */
38 /** A collection of handlers.
40 * The default implementations calls all handlers in list order,
41 * regardless of the response status or exceptions. Derived implementation
42 * may alter the order or the conditions of calling the contained
47 @ManagedObject("Handler of multiple handlers")
48 public class HandlerCollection extends AbstractHandlerContainer
50 private final boolean _mutableWhenRunning;
51 private volatile Handler[] _handlers;
53 /* ------------------------------------------------------------ */
54 public HandlerCollection()
56 _mutableWhenRunning=false;
59 /* ------------------------------------------------------------ */
60 public HandlerCollection(boolean mutableWhenRunning)
62 _mutableWhenRunning=mutableWhenRunning;
65 /* ------------------------------------------------------------ */
67 * @return Returns the handlers.
70 @ManagedAttribute(value="Wrapped handlers", readonly=true)
71 public Handler[] getHandlers()
76 /* ------------------------------------------------------------ */
78 * @param handlers The handlers to set.
80 public void setHandlers(Handler[] handlers)
82 if (!_mutableWhenRunning && isStarted())
83 throw new IllegalStateException(STARTED);
86 for (Handler handler:handlers)
87 if (handler.getServer()!=getServer())
88 handler.setServer(getServer());
89 Handler[] old=_handlers;;
91 updateBeans(old, handlers);
94 /* ------------------------------------------------------------ */
96 * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse)
99 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
100 throws IOException, ServletException
102 if (_handlers!=null && isStarted())
104 MultiException mex=null;
106 for (int i=0;i<_handlers.length;i++)
110 _handlers[i].handle(target,baseRequest, request, response);
116 catch(RuntimeException e)
123 mex=new MultiException();
130 throw new ServletException(mex.getThrowable(0));
132 throw new ServletException(mex);
138 /* ------------------------------------------------------------ */
140 public void setServer(Server server)
142 super.setServer(server);
143 Handler[] handlers=getHandlers();
145 for (Handler h : handlers)
149 /* ------------------------------------------------------------ */
151 * This implementation adds the passed handler to the end of the existing collection of handlers.
152 * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler)
154 public void addHandler(Handler handler)
156 setHandlers(ArrayUtil.addToArray(getHandlers(), handler, Handler.class));
159 /* ------------------------------------------------------------ */
160 public void removeHandler(Handler handler)
162 Handler[] handlers = getHandlers();
164 if (handlers!=null && handlers.length>0 )
165 setHandlers(ArrayUtil.removeFromArray(handlers, handler));
168 /* ------------------------------------------------------------ */
170 protected void expandChildren(List<Handler> list, Class<?> byClass)
172 if (getHandlers()!=null)
173 for (Handler h:getHandlers())
174 expandHandler(h, list, byClass);
177 /* ------------------------------------------------------------ */
179 public void destroy()
182 throw new IllegalStateException("!STOPPED");
183 Handler[] children=getChildHandlers();
185 for (Handler child: children)
190 /* ------------------------------------------------------------ */
192 public String toString()
194 Handler[] handlers=getHandlers();
195 return super.toString()+(handlers==null?"[]":Arrays.asList(getHandlers()).toString());