Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/handler/HandlerCollection.java @ 953:7db4a488fc82
simplify SelectorManager
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Wed, 12 Oct 2016 22:16:36 -0600 |
| parents | b9aa175d9a29 |
| children | 32d4b569567c |
| 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.HttpServletRequest; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import javax.servlet.http.HttpServletResponse; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import org.eclipse.jetty.server.Handler; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import org.eclipse.jetty.server.Request; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.server.Server; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.util.LazyList; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.util.MultiException; |
|
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 /* ------------------------------------------------------------ */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 /** A collection of handlers. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 * The default implementations calls all handlers in list order, |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 * regardless of the response status or exceptions. Derived implementation |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 * 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
|
40 * handlers. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 * @org.apache.xbean.XBean |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 public class HandlerCollection extends AbstractHandlerContainer |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 { |
| 848 | 47 private final boolean _mutableWhenRunning; |
| 48 private volatile Handler[] _handlers; | |
| 49 private boolean _parallelStart=false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
| 848 | 51 /* ------------------------------------------------------------ */ |
| 52 public HandlerCollection() | |
| 53 { | |
| 54 _mutableWhenRunning=false; | |
| 55 } | |
| 56 | |
| 57 /* ------------------------------------------------------------ */ | |
| 58 public HandlerCollection(boolean mutableWhenRunning) | |
| 59 { | |
| 60 _mutableWhenRunning=mutableWhenRunning; | |
| 61 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 |
| 848 | 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 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 |
| 848 | 93 // stop old handlers |
| 94 for (int i=0;old_handlers!=null && i<old_handlers.length;i++) | |
| 95 { | |
| 96 if (old_handlers[i]!=null) | |
| 97 { | |
| 98 try | |
| 99 { | |
| 100 if (old_handlers[i].isStarted()) | |
| 101 old_handlers[i].stop(); | |
| 102 } | |
| 103 catch (Throwable e) | |
| 104 { | |
| 105 mex.add(e); | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 mex.ifExceptionThrowRuntime(); | |
| 111 } | |
| 112 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
113 |
| 848 | 114 |
| 115 /* ------------------------------------------------------------ */ | |
| 116 /** Get the parrallelStart. | |
| 117 * @return true if the contained handlers are started in parallel. | |
| 118 */ | |
| 119 public boolean isParallelStart() | |
| 120 { | |
| 121 return _parallelStart; | |
| 122 } | |
|
802
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 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
125 |
| 848 | 126 /* ------------------------------------------------------------ */ |
| 127 /** Set the parallelStart. | |
| 128 * @param parallelStart If true, contained handlers are started in parallel. | |
| 129 */ | |
| 130 public void setParallelStart(boolean parallelStart) | |
| 131 { | |
| 132 this._parallelStart = parallelStart; | |
| 133 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
134 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
135 |
| 848 | 136 /* ------------------------------------------------------------ */ |
| 137 /** | |
| 138 * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse) | |
| 139 */ | |
| 140 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |
| 141 throws IOException, ServletException | |
| 142 { | |
| 143 if (_handlers!=null && isStarted()) | |
| 144 { | |
| 145 MultiException mex=null; | |
| 146 | |
| 147 for (int i=0;i<_handlers.length;i++) | |
| 148 { | |
| 149 try | |
| 150 { | |
| 151 _handlers[i].handle(target,baseRequest, request, response); | |
| 152 } | |
| 153 catch(IOException e) | |
| 154 { | |
| 155 throw e; | |
| 156 } | |
| 157 catch(RuntimeException e) | |
| 158 { | |
| 159 throw e; | |
| 160 } | |
| 161 catch(Exception e) | |
| 162 { | |
| 163 if (mex==null) | |
| 164 mex=new MultiException(); | |
| 165 mex.add(e); | |
| 166 } | |
| 167 } | |
| 168 if (mex!=null) | |
| 169 { | |
| 170 if (mex.size()==1) | |
| 171 throw new ServletException(mex.getThrowable(0)); | |
| 172 else | |
| 173 throw new ServletException(mex); | |
| 174 } | |
| 175 | |
| 176 } | |
| 177 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 |
| 848 | 179 /* ------------------------------------------------------------ */ |
| 180 /* | |
| 181 * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart() | |
| 182 */ | |
| 183 @Override | |
| 184 protected void doStart() throws Exception | |
| 185 { | |
| 186 final MultiException mex=new MultiException(); | |
| 187 if (_handlers!=null) | |
| 188 { | |
| 189 if (_parallelStart) | |
| 190 { | |
| 191 final CountDownLatch latch = new CountDownLatch(_handlers.length); | |
| 192 final ClassLoader loader = Thread.currentThread().getContextClassLoader(); | |
| 193 for (int i=0;i<_handlers.length;i++) | |
| 194 { | |
| 195 final int h=i; | |
|
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
196 getServer().threadPool.execute( |
| 848 | 197 new Runnable() |
| 198 { | |
| 199 public void run() | |
| 200 { | |
| 201 ClassLoader orig = Thread.currentThread().getContextClassLoader(); | |
| 202 try | |
| 203 { | |
| 204 Thread.currentThread().setContextClassLoader(loader); | |
| 205 _handlers[h].start(); | |
| 206 } | |
| 207 catch(Throwable e) | |
| 208 { | |
| 209 mex.add(e); | |
| 210 } | |
| 211 finally | |
| 212 { | |
| 213 Thread.currentThread().setContextClassLoader(orig); | |
| 214 latch.countDown(); | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 ); | |
| 219 } | |
| 220 latch.await(); | |
| 221 } | |
| 222 else | |
| 223 { | |
| 224 for (int i=0;i<_handlers.length;i++) | |
| 225 { | |
| 226 try{_handlers[i].start();} | |
| 227 catch(Throwable e){mex.add(e);} | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 super.doStart(); | |
| 232 mex.ifExceptionThrow(); | |
| 233 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
234 |
| 848 | 235 /* ------------------------------------------------------------ */ |
| 236 /* | |
| 237 * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop() | |
| 238 */ | |
| 239 @Override | |
| 240 protected void doStop() throws Exception | |
| 241 { | |
| 242 MultiException mex=new MultiException(); | |
| 243 try { super.doStop(); } catch(Throwable e){mex.add(e);} | |
| 244 if (_handlers!=null) | |
| 245 { | |
| 246 for (int i=_handlers.length;i-->0;) | |
| 247 try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} | |
| 248 } | |
| 249 mex.ifExceptionThrow(); | |
| 250 } | |
| 251 | |
| 252 /* ------------------------------------------------------------ */ | |
| 253 @Override | |
| 254 public void setServer(Server server) | |
| 255 { | |
| 256 if (isStarted()) | |
| 257 throw new IllegalStateException(STARTED); | |
| 258 | |
| 259 Server old_server=getServer(); | |
| 260 | |
| 261 super.setServer(server); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
262 |
| 848 | 263 Handler[] h=getHandlers(); |
| 264 for (int i=0;h!=null && i<h.length;i++) | |
| 265 h[i].setServer(server); | |
| 266 | |
| 267 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
268 |
| 848 | 269 /* ------------------------------------------------------------ */ |
| 270 /* Add a handler. | |
| 271 * This implementation adds the passed handler to the end of the existing collection of handlers. | |
| 272 * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler) | |
| 273 */ | |
| 274 public void addHandler(Handler handler) | |
| 275 { | |
| 276 setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class)); | |
| 277 } | |
| 278 | |
| 279 /* ------------------------------------------------------------ */ | |
| 280 public void removeHandler(Handler handler) | |
| 281 { | |
| 282 Handler[] handlers = getHandlers(); | |
| 283 | |
| 284 if (handlers!=null && handlers.length>0 ) | |
| 285 setHandlers((Handler[])LazyList.removeFromArray(handlers, handler)); | |
| 286 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
287 |
| 848 | 288 /* ------------------------------------------------------------ */ |
| 289 @Override | |
| 290 protected Object expandChildren(Object list, Class byClass) | |
| 291 { | |
| 292 Handler[] handlers = getHandlers(); | |
| 293 for (int i=0;handlers!=null && i<handlers.length;i++) | |
| 294 list=expandHandler(handlers[i], list, byClass); | |
| 295 return list; | |
| 296 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 |
| 848 | 298 /* ------------------------------------------------------------ */ |
| 299 @Override | |
| 300 public void destroy() | |
| 301 { | |
| 302 if (!isStopped()) | |
| 303 throw new IllegalStateException("!STOPPED"); | |
| 304 Handler[] children=getChildHandlers(); | |
| 305 setHandlers(null); | |
| 306 for (Handler child: children) | |
| 307 child.destroy(); | |
| 308 super.destroy(); | |
| 309 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
310 } |
