1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server.handler;
15
16 import java.io.IOException;
17
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.server.Handler;
23 import org.eclipse.jetty.server.Request;
24 import org.eclipse.jetty.server.Server;
25 import org.eclipse.jetty.util.LazyList;
26 import org.eclipse.jetty.util.MultiException;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class HandlerCollection extends AbstractHandlerContainer
40 {
41 private final boolean _mutableWhenRunning;
42 private volatile Handler[] _handlers;
43
44
45 public HandlerCollection()
46 {
47 _mutableWhenRunning=false;
48 }
49
50
51 public HandlerCollection(boolean mutableWhenRunning)
52 {
53 _mutableWhenRunning=mutableWhenRunning;
54 }
55
56
57
58
59
60 public Handler[] getHandlers()
61 {
62 return _handlers;
63 }
64
65
66
67
68
69
70 public void setHandlers(Handler[] handlers)
71 {
72 if (!_mutableWhenRunning && isStarted())
73 throw new IllegalStateException(STARTED);
74
75 Handler [] old_handlers = _handlers==null?null:_handlers.clone();
76
77 if (getServer()!=null)
78 getServer().getContainer().update(this, old_handlers, handlers, "handler");
79
80 Server server = getServer();
81 MultiException mex = new MultiException();
82 for (int i=0;handlers!=null && i<handlers.length;i++)
83 {
84 if (handlers[i].getServer()!=server)
85 handlers[i].setServer(server);
86 }
87
88
89 _handlers = handlers;
90
91
92 for (int i=0;old_handlers!=null && i<old_handlers.length;i++)
93 {
94 if (old_handlers[i]!=null)
95 {
96 try
97 {
98 if (old_handlers[i].isStarted())
99 old_handlers[i].stop();
100 }
101 catch (Throwable e)
102 {
103 mex.add(e);
104 }
105 }
106 }
107
108 mex.ifExceptionThrowRuntime();
109 }
110
111
112
113
114
115 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
116 throws IOException, ServletException
117 {
118 if (_handlers!=null && isStarted())
119 {
120 MultiException mex=null;
121
122 for (int i=0;i<_handlers.length;i++)
123 {
124 try
125 {
126 _handlers[i].handle(target,baseRequest, request, response);
127 }
128 catch(IOException e)
129 {
130 throw e;
131 }
132 catch(RuntimeException e)
133 {
134 throw e;
135 }
136 catch(Exception e)
137 {
138 if (mex==null)
139 mex=new MultiException();
140 mex.add(e);
141 }
142 }
143 if (mex!=null)
144 {
145 if (mex.size()==1)
146 throw new ServletException(mex.getThrowable(0));
147 else
148 throw new ServletException(mex);
149 }
150
151 }
152 }
153
154
155
156
157
158 @Override
159 protected void doStart() throws Exception
160 {
161 MultiException mex=new MultiException();
162 if (_handlers!=null)
163 {
164 for (int i=0;i<_handlers.length;i++)
165 try{_handlers[i].start();}catch(Throwable e){mex.add(e);}
166 }
167 super.doStart();
168 mex.ifExceptionThrow();
169 }
170
171
172
173
174
175 @Override
176 protected void doStop() throws Exception
177 {
178 MultiException mex=new MultiException();
179 try { super.doStop(); } catch(Throwable e){mex.add(e);}
180 if (_handlers!=null)
181 {
182 for (int i=_handlers.length;i-->0;)
183 try{_handlers[i].stop();}catch(Throwable e){mex.add(e);}
184 }
185 mex.ifExceptionThrow();
186 }
187
188
189 @Override
190 public void setServer(Server server)
191 {
192 if (isStarted())
193 throw new IllegalStateException(STARTED);
194
195 Server old_server=getServer();
196
197 super.setServer(server);
198
199 Handler[] h=getHandlers();
200 for (int i=0;h!=null && i<h.length;i++)
201 h[i].setServer(server);
202
203 if (server!=null && server!=old_server)
204 server.getContainer().update(this, null,_handlers, "handler");
205
206 }
207
208
209
210
211
212
213 public void addHandler(Handler handler)
214 {
215 setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class));
216 }
217
218
219 public void removeHandler(Handler handler)
220 {
221 Handler[] handlers = getHandlers();
222
223 if (handlers!=null && handlers.length>0 )
224 setHandlers((Handler[])LazyList.removeFromArray(handlers, handler));
225 }
226
227
228 @Override
229 protected Object expandChildren(Object list, Class byClass)
230 {
231 Handler[] handlers = getHandlers();
232 for (int i=0;handlers!=null && i<handlers.length;i++)
233 list=expandHandler(handlers[i], list, byClass);
234 return list;
235 }
236
237
238 }