Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/handler/HandlerCollection.java @ 1056:7d872cc72ec2
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 08 Nov 2016 01:19:36 -0700 (2016-11-08) |
| parents | 32d4b569567c |
| children |
| rev | line source |
|---|---|
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server.handler; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.util.concurrent.CountDownLatch; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import javax.servlet.ServletException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import javax.servlet.http.HttpServletResponse; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import org.eclipse.jetty.server.Handler; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import org.eclipse.jetty.server.Request; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import org.eclipse.jetty.server.Server; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.util.LazyList; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.util.MultiException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 /* ------------------------------------------------------------ */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 /** A collection of handlers. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 * The default implementations calls all handlers in list order, |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 * regardless of the response status or exceptions. Derived implementation |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 * may alter the order or the conditions of calling the contained |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 * handlers. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 * @org.apache.xbean.XBean |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 public class HandlerCollection extends AbstractHandlerContainer |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 { |
| 848 | 46 private final boolean _mutableWhenRunning; |
| 47 private volatile Handler[] _handlers; | |
| 48 private boolean _parallelStart=false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
| 848 | 50 /* ------------------------------------------------------------ */ |
| 51 public HandlerCollection() | |
| 52 { | |
| 53 _mutableWhenRunning=false; | |
| 54 } | |
| 55 | |
| 56 /* ------------------------------------------------------------ */ | |
| 57 public HandlerCollection(boolean mutableWhenRunning) | |
| 58 { | |
| 59 _mutableWhenRunning=mutableWhenRunning; | |
| 60 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
| 848 | 62 /* ------------------------------------------------------------ */ |
| 63 /** | |
| 64 * @return Returns the handlers. | |
| 65 */ | |
| 66 public Handler[] getHandlers() | |
| 67 { | |
| 68 return _handlers; | |
| 69 } | |
| 70 | |
| 71 /* ------------------------------------------------------------ */ | |
| 72 /** | |
| 73 * | |
| 74 * @param handlers The handlers to set. | |
| 75 */ | |
| 76 public void setHandlers(Handler[] handlers) | |
| 77 { | |
| 78 if (!_mutableWhenRunning && isStarted()) | |
| 79 throw new IllegalStateException(STARTED); | |
| 80 | |
| 81 Handler [] old_handlers = _handlers==null?null:_handlers.clone(); | |
| 82 _handlers = handlers; | |
| 83 | |
| 84 Server server = getServer(); | |
| 85 MultiException mex = new MultiException(); | |
| 86 for (int i=0;handlers!=null && i<handlers.length;i++) | |
| 87 { | |
| 88 if (handlers[i].getServer()!=server) | |
| 89 handlers[i].setServer(server); | |
| 90 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
91 |
| 848 | 92 // stop old handlers |
| 93 for (int i=0;old_handlers!=null && i<old_handlers.length;i++) | |
| 94 { | |
| 95 if (old_handlers[i]!=null) | |
| 96 { | |
| 97 try | |
| 98 { | |
| 99 if (old_handlers[i].isStarted()) | |
| 100 old_handlers[i].stop(); | |
| 101 } | |
| 102 catch (Throwable e) | |
| 103 { | |
| 104 mex.add(e); | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 mex.ifExceptionThrowRuntime(); | |
| 110 } | |
| 111 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
| 848 | 113 |
| 114 /* ------------------------------------------------------------ */ | |
| 115 /** Get the parrallelStart. | |
| 116 * @return true if the contained handlers are started in parallel. | |
| 117 */ | |
| 118 public boolean isParallelStart() | |
| 119 { | |
| 120 return _parallelStart; | |
| 121 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
122 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
123 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
124 |
| 848 | 125 /* ------------------------------------------------------------ */ |
| 126 /** Set the parallelStart. | |
| 127 * @param parallelStart If true, contained handlers are started in parallel. | |
| 128 */ | |
| 129 public void setParallelStart(boolean parallelStart) | |
| 130 { | |
| 131 this._parallelStart = parallelStart; | |
| 132 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
| 1000 | 134 @Override |
| 135 public void handle(String target, Request request, HttpServletResponse response) | |
| 848 | 136 throws IOException, ServletException |
| 137 { | |
| 138 if (_handlers!=null && isStarted()) | |
| 139 { | |
| 140 MultiException mex=null; | |
| 141 | |
| 142 for (int i=0;i<_handlers.length;i++) | |
| 143 { | |
| 144 try | |
| 145 { | |
| 1000 | 146 _handlers[i].handle(target, request, response); |
| 848 | 147 } |
| 148 catch(IOException e) | |
| 149 { | |
| 150 throw e; | |
| 151 } | |
| 152 catch(RuntimeException e) | |
| 153 { | |
| 154 throw e; | |
| 155 } | |
| 156 catch(Exception e) | |
| 157 { | |
| 158 if (mex==null) | |
| 159 mex=new MultiException(); | |
| 160 mex.add(e); | |
| 161 } | |
| 162 } | |
| 163 if (mex!=null) | |
| 164 { | |
| 165 if (mex.size()==1) | |
| 166 throw new ServletException(mex.getThrowable(0)); | |
| 167 else | |
| 168 throw new ServletException(mex); | |
| 169 } | |
| 170 | |
| 171 } | |
| 172 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
173 |
| 848 | 174 /* ------------------------------------------------------------ */ |
| 175 /* | |
| 176 * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart() | |
| 177 */ | |
| 178 @Override | |
| 179 protected void doStart() throws Exception | |
| 180 { | |
| 181 final MultiException mex=new MultiException(); | |
| 182 if (_handlers!=null) | |
| 183 { | |
| 184 if (_parallelStart) | |
| 185 { | |
| 186 final CountDownLatch latch = new CountDownLatch(_handlers.length); | |
| 187 final ClassLoader loader = Thread.currentThread().getContextClassLoader(); | |
| 188 for (int i=0;i<_handlers.length;i++) | |
| 189 { | |
| 190 final int h=i; | |
|
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
191 getServer().threadPool.execute( |
| 848 | 192 new Runnable() |
| 193 { | |
| 194 public void run() | |
| 195 { | |
| 196 ClassLoader orig = Thread.currentThread().getContextClassLoader(); | |
| 197 try | |
| 198 { | |
| 199 Thread.currentThread().setContextClassLoader(loader); | |
| 200 _handlers[h].start(); | |
| 201 } | |
| 202 catch(Throwable e) | |
| 203 { | |
| 204 mex.add(e); | |
| 205 } | |
| 206 finally | |
| 207 { | |
| 208 Thread.currentThread().setContextClassLoader(orig); | |
| 209 latch.countDown(); | |
| 210 } | |
| 211 } | |
| 212 } | |
| 213 ); | |
| 214 } | |
| 215 latch.await(); | |
| 216 } | |
| 217 else | |
| 218 { | |
| 219 for (int i=0;i<_handlers.length;i++) | |
| 220 { | |
| 221 try{_handlers[i].start();} | |
| 222 catch(Throwable e){mex.add(e);} | |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 super.doStart(); | |
| 227 mex.ifExceptionThrow(); | |
| 228 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
229 |
| 848 | 230 /* ------------------------------------------------------------ */ |
| 231 /* | |
| 232 * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop() | |
| 233 */ | |
| 234 @Override | |
| 235 protected void doStop() throws Exception | |
| 236 { | |
| 237 MultiException mex=new MultiException(); | |
| 238 try { super.doStop(); } catch(Throwable e){mex.add(e);} | |
| 239 if (_handlers!=null) | |
| 240 { | |
| 241 for (int i=_handlers.length;i-->0;) | |
| 242 try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} | |
| 243 } | |
| 244 mex.ifExceptionThrow(); | |
| 245 } | |
| 246 | |
| 247 /* ------------------------------------------------------------ */ | |
| 248 @Override | |
| 249 public void setServer(Server server) | |
| 250 { | |
| 251 if (isStarted()) | |
| 252 throw new IllegalStateException(STARTED); | |
| 253 | |
| 254 Server old_server=getServer(); | |
| 255 | |
| 256 super.setServer(server); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
257 |
| 848 | 258 Handler[] h=getHandlers(); |
| 259 for (int i=0;h!=null && i<h.length;i++) | |
| 260 h[i].setServer(server); | |
| 261 | |
| 262 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 |
| 848 | 264 /* ------------------------------------------------------------ */ |
| 265 /* Add a handler. | |
| 266 * This implementation adds the passed handler to the end of the existing collection of handlers. | |
| 267 * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler) | |
| 268 */ | |
| 269 public void addHandler(Handler handler) | |
| 270 { | |
| 271 setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class)); | |
| 272 } | |
| 273 | |
| 274 /* ------------------------------------------------------------ */ | |
| 275 public void removeHandler(Handler handler) | |
| 276 { | |
| 277 Handler[] handlers = getHandlers(); | |
| 278 | |
| 279 if (handlers!=null && handlers.length>0 ) | |
| 280 setHandlers((Handler[])LazyList.removeFromArray(handlers, handler)); | |
| 281 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
282 |
| 848 | 283 /* ------------------------------------------------------------ */ |
| 284 @Override | |
| 285 protected Object expandChildren(Object list, Class byClass) | |
| 286 { | |
| 287 Handler[] handlers = getHandlers(); | |
| 288 for (int i=0;handlers!=null && i<handlers.length;i++) | |
| 289 list=expandHandler(handlers[i], list, byClass); | |
| 290 return list; | |
| 291 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 |
| 848 | 293 /* ------------------------------------------------------------ */ |
| 294 @Override | |
| 295 public void destroy() | |
| 296 { | |
| 297 if (!isStopped()) | |
| 298 throw new IllegalStateException("!STOPPED"); | |
| 299 Handler[] children=getChildHandlers(); | |
| 300 setHandlers(null); | |
| 301 for (Handler child: children) | |
| 302 child.destroy(); | |
| 303 super.destroy(); | |
| 304 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
305 } |
