Jake Slack | 03928ae | 2014-05-13 18:41:56 -0700 | [diff] [blame] | 1 | // |
| 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. |
| 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.concurrent.CountDownLatch; |
| 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.LazyList; |
| 32 | import org.eclipse.jetty.util.MultiException; |
| 33 | |
| 34 | /* ------------------------------------------------------------ */ |
| 35 | /** A collection of handlers. |
| 36 | * <p> |
| 37 | * The default implementations calls all handlers in list order, |
| 38 | * regardless of the response status or exceptions. Derived implementation |
| 39 | * may alter the order or the conditions of calling the contained |
| 40 | * handlers. |
| 41 | * <p> |
| 42 | * |
| 43 | * @org.apache.xbean.XBean |
| 44 | */ |
| 45 | public class HandlerCollection extends AbstractHandlerContainer |
| 46 | { |
| 47 | private final boolean _mutableWhenRunning; |
| 48 | private volatile Handler[] _handlers; |
| 49 | private boolean _parallelStart=false; |
| 50 | |
| 51 | /* ------------------------------------------------------------ */ |
| 52 | public HandlerCollection() |
| 53 | { |
| 54 | _mutableWhenRunning=false; |
| 55 | } |
| 56 | |
| 57 | /* ------------------------------------------------------------ */ |
| 58 | public HandlerCollection(boolean mutableWhenRunning) |
| 59 | { |
| 60 | _mutableWhenRunning=mutableWhenRunning; |
| 61 | } |
| 62 | |
| 63 | /* ------------------------------------------------------------ */ |
| 64 | /** |
| 65 | * @return Returns the handlers. |
| 66 | */ |
| 67 | public Handler[] getHandlers() |
| 68 | { |
| 69 | return _handlers; |
| 70 | } |
| 71 | |
| 72 | /* ------------------------------------------------------------ */ |
| 73 | /** |
| 74 | * |
| 75 | * @param handlers The handlers to set. |
| 76 | */ |
| 77 | public void setHandlers(Handler[] handlers) |
| 78 | { |
| 79 | if (!_mutableWhenRunning && isStarted()) |
| 80 | throw new IllegalStateException(STARTED); |
| 81 | |
| 82 | Handler [] old_handlers = _handlers==null?null:_handlers.clone(); |
| 83 | _handlers = handlers; |
| 84 | |
| 85 | Server server = getServer(); |
| 86 | MultiException mex = new MultiException(); |
| 87 | for (int i=0;handlers!=null && i<handlers.length;i++) |
| 88 | { |
| 89 | if (handlers[i].getServer()!=server) |
| 90 | handlers[i].setServer(server); |
| 91 | } |
| 92 | |
| 93 | if (getServer()!=null) |
| 94 | getServer().getContainer().update(this, old_handlers, handlers, "handler"); |
| 95 | |
| 96 | // stop old handlers |
| 97 | for (int i=0;old_handlers!=null && i<old_handlers.length;i++) |
| 98 | { |
| 99 | if (old_handlers[i]!=null) |
| 100 | { |
| 101 | try |
| 102 | { |
| 103 | if (old_handlers[i].isStarted()) |
| 104 | old_handlers[i].stop(); |
| 105 | } |
| 106 | catch (Throwable e) |
| 107 | { |
| 108 | mex.add(e); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | mex.ifExceptionThrowRuntime(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | /* ------------------------------------------------------------ */ |
| 119 | /** Get the parrallelStart. |
| 120 | * @return true if the contained handlers are started in parallel. |
| 121 | */ |
| 122 | public boolean isParallelStart() |
| 123 | { |
| 124 | return _parallelStart; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | /* ------------------------------------------------------------ */ |
| 130 | /** Set the parallelStart. |
| 131 | * @param parallelStart If true, contained handlers are started in parallel. |
| 132 | */ |
| 133 | public void setParallelStart(boolean parallelStart) |
| 134 | { |
| 135 | this._parallelStart = parallelStart; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* ------------------------------------------------------------ */ |
| 140 | /** |
| 141 | * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse) |
| 142 | */ |
| 143 | public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) |
| 144 | throws IOException, ServletException |
| 145 | { |
| 146 | if (_handlers!=null && isStarted()) |
| 147 | { |
| 148 | MultiException mex=null; |
| 149 | |
| 150 | for (int i=0;i<_handlers.length;i++) |
| 151 | { |
| 152 | try |
| 153 | { |
| 154 | _handlers[i].handle(target,baseRequest, request, response); |
| 155 | } |
| 156 | catch(IOException e) |
| 157 | { |
| 158 | throw e; |
| 159 | } |
| 160 | catch(RuntimeException e) |
| 161 | { |
| 162 | throw e; |
| 163 | } |
| 164 | catch(Exception e) |
| 165 | { |
| 166 | if (mex==null) |
| 167 | mex=new MultiException(); |
| 168 | mex.add(e); |
| 169 | } |
| 170 | } |
| 171 | if (mex!=null) |
| 172 | { |
| 173 | if (mex.size()==1) |
| 174 | throw new ServletException(mex.getThrowable(0)); |
| 175 | else |
| 176 | throw new ServletException(mex); |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* ------------------------------------------------------------ */ |
| 183 | /* |
| 184 | * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart() |
| 185 | */ |
| 186 | @Override |
| 187 | protected void doStart() throws Exception |
| 188 | { |
| 189 | final MultiException mex=new MultiException(); |
| 190 | if (_handlers!=null) |
| 191 | { |
| 192 | if (_parallelStart) |
| 193 | { |
| 194 | final CountDownLatch latch = new CountDownLatch(_handlers.length); |
| 195 | final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 196 | for (int i=0;i<_handlers.length;i++) |
| 197 | { |
| 198 | final int h=i; |
| 199 | getServer().getThreadPool().dispatch( |
| 200 | new Runnable() |
| 201 | { |
| 202 | public void run() |
| 203 | { |
| 204 | ClassLoader orig = Thread.currentThread().getContextClassLoader(); |
| 205 | try |
| 206 | { |
| 207 | Thread.currentThread().setContextClassLoader(loader); |
| 208 | _handlers[h].start(); |
| 209 | } |
| 210 | catch(Throwable e) |
| 211 | { |
| 212 | mex.add(e); |
| 213 | } |
| 214 | finally |
| 215 | { |
| 216 | Thread.currentThread().setContextClassLoader(orig); |
| 217 | latch.countDown(); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | ); |
| 222 | } |
| 223 | latch.await(); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | for (int i=0;i<_handlers.length;i++) |
| 228 | { |
| 229 | try{_handlers[i].start();} |
| 230 | catch(Throwable e){mex.add(e);} |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | super.doStart(); |
| 235 | mex.ifExceptionThrow(); |
| 236 | } |
| 237 | |
| 238 | /* ------------------------------------------------------------ */ |
| 239 | /* |
| 240 | * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop() |
| 241 | */ |
| 242 | @Override |
| 243 | protected void doStop() throws Exception |
| 244 | { |
| 245 | MultiException mex=new MultiException(); |
| 246 | try { super.doStop(); } catch(Throwable e){mex.add(e);} |
| 247 | if (_handlers!=null) |
| 248 | { |
| 249 | for (int i=_handlers.length;i-->0;) |
| 250 | try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} |
| 251 | } |
| 252 | mex.ifExceptionThrow(); |
| 253 | } |
| 254 | |
| 255 | /* ------------------------------------------------------------ */ |
| 256 | @Override |
| 257 | public void setServer(Server server) |
| 258 | { |
| 259 | if (isStarted()) |
| 260 | throw new IllegalStateException(STARTED); |
| 261 | |
| 262 | Server old_server=getServer(); |
| 263 | |
| 264 | super.setServer(server); |
| 265 | |
| 266 | Handler[] h=getHandlers(); |
| 267 | for (int i=0;h!=null && i<h.length;i++) |
| 268 | h[i].setServer(server); |
| 269 | |
| 270 | if (server!=null && server!=old_server) |
| 271 | server.getContainer().update(this, null,_handlers, "handler"); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | /* ------------------------------------------------------------ */ |
| 276 | /* Add a handler. |
| 277 | * This implementation adds the passed handler to the end of the existing collection of handlers. |
| 278 | * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler) |
| 279 | */ |
| 280 | public void addHandler(Handler handler) |
| 281 | { |
| 282 | setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class)); |
| 283 | } |
| 284 | |
| 285 | /* ------------------------------------------------------------ */ |
| 286 | public void removeHandler(Handler handler) |
| 287 | { |
| 288 | Handler[] handlers = getHandlers(); |
| 289 | |
| 290 | if (handlers!=null && handlers.length>0 ) |
| 291 | setHandlers((Handler[])LazyList.removeFromArray(handlers, handler)); |
| 292 | } |
| 293 | |
| 294 | /* ------------------------------------------------------------ */ |
| 295 | @Override |
| 296 | protected Object expandChildren(Object list, Class byClass) |
| 297 | { |
| 298 | Handler[] handlers = getHandlers(); |
| 299 | for (int i=0;handlers!=null && i<handlers.length;i++) |
| 300 | list=expandHandler(handlers[i], list, byClass); |
| 301 | return list; |
| 302 | } |
| 303 | |
| 304 | /* ------------------------------------------------------------ */ |
| 305 | @Override |
| 306 | public void destroy() |
| 307 | { |
| 308 | if (!isStopped()) |
| 309 | throw new IllegalStateException("!STOPPED"); |
| 310 | Handler[] children=getChildHandlers(); |
| 311 | setHandlers(null); |
| 312 | for (Handler child: children) |
| 313 | child.destroy(); |
| 314 | super.destroy(); |
| 315 | } |
| 316 | } |