comparison src/org/eclipse/jetty/server/handler/HandlerCollection.java @ 848:22a4e93ed20e

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