1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.servlet;
20
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.servlet.ServletContext;
30 import javax.servlet.UnavailableException;
31
32 import org.eclipse.jetty.server.handler.ContextHandler;
33 import org.eclipse.jetty.servlet.api.Registration;
34 import org.eclipse.jetty.util.Loader;
35 import org.eclipse.jetty.util.component.AbstractLifeCycle;
36 import org.eclipse.jetty.util.component.AggregateLifeCycle;
37 import org.eclipse.jetty.util.component.Dumpable;
38 import org.eclipse.jetty.util.log.Log;
39 import org.eclipse.jetty.util.log.Logger;
40
41
42
43
44
45
46 public class Holder<T> extends AbstractLifeCycle implements Dumpable
47 {
48 private static final Logger LOG = Log.getLogger(Holder.class);
49
50 protected transient Class<? extends T> _class;
51 protected final Map<String,String> _initParams=new HashMap<String,String>(3);
52 protected String _className;
53 protected String _displayName;
54 protected boolean _extInstance;
55 protected boolean _asyncSupported=true;
56
57
58 protected String _name;
59 protected ServletHandler _servletHandler;
60
61
62 protected Holder()
63 {
64 }
65
66
67
68
69
70 public boolean isInstance()
71 {
72 return _extInstance;
73 }
74
75
76 @SuppressWarnings("unchecked")
77 public void doStart()
78 throws Exception
79 {
80
81 if (_class==null && (_className==null || _className.equals("")))
82 throw new UnavailableException("No class for Servlet or Filter", -1);
83
84
85 if (_class==null)
86 {
87 try
88 {
89 _class=Loader.loadClass(Holder.class, _className);
90 if(LOG.isDebugEnabled())
91 LOG.debug("Holding {}",_class);
92 }
93 catch (Exception e)
94 {
95 LOG.warn(e);
96 throw new UnavailableException(e.getMessage(), -1);
97 }
98 }
99 }
100
101
102 @Override
103 public void doStop()
104 throws Exception
105 {
106 if (!_extInstance)
107 _class=null;
108 }
109
110
111 public String getClassName()
112 {
113 return _className;
114 }
115
116
117 public Class<? extends T> getHeldClass()
118 {
119 return _class;
120 }
121
122
123 public String getDisplayName()
124 {
125 return _displayName;
126 }
127
128
129 public String getInitParameter(String param)
130 {
131 if (_initParams==null)
132 return null;
133 return (String)_initParams.get(param);
134 }
135
136
137 public Enumeration getInitParameterNames()
138 {
139 if (_initParams==null)
140 return Collections.enumeration(Collections.EMPTY_LIST);
141 return Collections.enumeration(_initParams.keySet());
142 }
143
144
145 public Map<String,String> getInitParameters()
146 {
147 return _initParams;
148 }
149
150
151 public String getName()
152 {
153 return _name;
154 }
155
156
157
158
159
160 public ServletHandler getServletHandler()
161 {
162 return _servletHandler;
163 }
164
165
166 public void destroyInstance(Object instance)
167 throws Exception
168 {
169 }
170
171
172
173
174
175 public void setClassName(String className)
176 {
177 _className = className;
178 _class=null;
179 }
180
181
182
183
184
185 public void setHeldClass(Class<? extends T> held)
186 {
187 _class=held;
188 if (held!=null)
189 {
190 _className=held.getName();
191 if (_name==null)
192 _name=held.getName()+"-"+this.hashCode();
193 }
194 }
195
196
197 public void setDisplayName(String name)
198 {
199 _displayName=name;
200 }
201
202
203 public void setInitParameter(String param,String value)
204 {
205 _initParams.put(param,value);
206 }
207
208
209 public void setInitParameters(Map<String,String> map)
210 {
211 _initParams.clear();
212 _initParams.putAll(map);
213 }
214
215
216
217
218
219
220
221
222 public void setName(String name)
223 {
224 _name = name;
225 }
226
227
228
229
230
231 public void setServletHandler(ServletHandler servletHandler)
232 {
233 _servletHandler = servletHandler;
234 }
235
236
237 public void setAsyncSupported(boolean suspendable)
238 {
239 _asyncSupported=suspendable;
240 }
241
242
243 public boolean isAsyncSupported()
244 {
245 return _asyncSupported;
246 }
247
248
249 public String toString()
250 {
251 return _name;
252 }
253
254
255 protected void illegalStateIfContextStarted()
256 {
257 if (_servletHandler!=null)
258 {
259 ContextHandler.Context context=(ContextHandler.Context)_servletHandler.getServletContext();
260 if (context!=null && context.getContextHandler().isStarted())
261 throw new IllegalStateException("Started");
262 }
263 }
264
265
266 public void dump(Appendable out, String indent) throws IOException
267 {
268 out.append(_name).append("==").append(_className)
269 .append(" - ").append(AbstractLifeCycle.getState(this)).append("\n");
270 AggregateLifeCycle.dump(out,indent,_initParams.entrySet());
271 }
272
273
274 public String dump()
275 {
276 return AggregateLifeCycle.dump(this);
277 }
278
279
280
281
282 protected class HolderConfig
283 {
284
285
286 public ServletContext getServletContext()
287 {
288 return _servletHandler.getServletContext();
289 }
290
291
292 public String getInitParameter(String param)
293 {
294 return Holder.this.getInitParameter(param);
295 }
296
297
298 public Enumeration getInitParameterNames()
299 {
300 return Holder.this.getInitParameterNames();
301 }
302 }
303
304
305
306
307 protected class HolderRegistration implements Registration.Dynamic
308 {
309 public void setAsyncSupported(boolean isAsyncSupported)
310 {
311 illegalStateIfContextStarted();
312 Holder.this.setAsyncSupported(isAsyncSupported);
313 }
314
315 public void setDescription(String description)
316 {
317 if (LOG.isDebugEnabled())
318 LOG.debug(this+" is "+description);
319 }
320
321 public String getClassName()
322 {
323 return Holder.this.getClassName();
324 }
325
326 public String getInitParameter(String name)
327 {
328 return Holder.this.getInitParameter(name);
329 }
330
331 public Map<String, String> getInitParameters()
332 {
333 return Holder.this.getInitParameters();
334 }
335
336 public String getName()
337 {
338 return Holder.this.getName();
339 }
340
341 public boolean setInitParameter(String name, String value)
342 {
343 illegalStateIfContextStarted();
344 if (Holder.this.getInitParameter(name)!=null)
345 return false;
346 Holder.this.setInitParameter(name,value);
347 return true;
348 }
349
350 public Set<String> setInitParameters(Map<String, String> initParameters)
351 {
352 illegalStateIfContextStarted();
353 Set<String> clash=null;
354 for (String name : initParameters.keySet())
355 {
356 if (Holder.this.getInitParameter(name)!=null)
357 {
358 if (clash==null)
359 clash=new HashSet<String>();
360 clash.add(name);
361 }
362 }
363 if (clash!=null)
364 return clash;
365 Holder.this.setInitParameters(initParameters);
366 return Collections.emptySet();
367 };
368 }
369 }
370
371
372
373
374