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.Registration;
30 import javax.servlet.ServletContext;
31
32 import org.eclipse.jetty.util.annotation.ManagedAttribute;
33 import org.eclipse.jetty.util.annotation.ManagedObject;
34 import org.eclipse.jetty.util.component.ContainerLifeCycle;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
37
38
39
40
41
42
43
44
45
46
47 @ManagedObject("Holder - a container for servlets and the like")
48 public class Holder<T> extends BaseHolder<T>
49 {
50 private static final Logger LOG = Log.getLogger(Holder.class);
51
52 protected final Map<String,String> _initParams=new HashMap<String,String>(3);
53 protected String _displayName;
54 protected boolean _asyncSupported;
55 protected String _name;
56 protected boolean _initialized = false;
57
58
59
60 protected Holder(Source source)
61 {
62 super(source);
63 switch(_source)
64 {
65 case JAVAX_API:
66 case DESCRIPTOR:
67 case ANNOTATION:
68 _asyncSupported=false;
69 break;
70 default:
71 _asyncSupported=true;
72 }
73 }
74
75
76
77
78 @ManagedAttribute(value="Display Name", readonly=true)
79 public String getDisplayName()
80 {
81 return _displayName;
82 }
83
84
85 public String getInitParameter(String param)
86 {
87 if (_initParams==null)
88 return null;
89 return (String)_initParams.get(param);
90 }
91
92
93 public Enumeration<String> getInitParameterNames()
94 {
95 if (_initParams==null)
96 return Collections.enumeration(Collections.EMPTY_LIST);
97 return Collections.enumeration(_initParams.keySet());
98 }
99
100
101 @ManagedAttribute(value="Initial Parameters", readonly=true)
102 public Map<String,String> getInitParameters()
103 {
104 return _initParams;
105 }
106
107
108 @ManagedAttribute(value="Name", readonly=true)
109 public String getName()
110 {
111 return _name;
112 }
113
114
115
116 public void destroyInstance(Object instance)
117 throws Exception
118 {
119 }
120
121
122
123
124 public void setClassName(String className)
125 {
126 super.setClassName(className);
127 if (_name==null)
128 _name=className+"-"+Integer.toHexString(this.hashCode());
129 }
130
131
132
133
134
135 public void setHeldClass(Class<? extends T> held)
136 {
137 super.setHeldClass(held);
138 if (held!=null)
139 {
140 if (_name==null)
141 _name=held.getName()+"-"+Integer.toHexString(this.hashCode());
142 }
143 }
144
145
146 public void setDisplayName(String name)
147 {
148 _displayName=name;
149 }
150
151
152 public void setInitParameter(String param,String value)
153 {
154 _initParams.put(param,value);
155 }
156
157
158 public void setInitParameters(Map<String,String> map)
159 {
160 _initParams.clear();
161 _initParams.putAll(map);
162 }
163
164
165
166
167
168
169
170
171 public void setName(String name)
172 {
173 _name = name;
174 }
175
176
177
178 public void setAsyncSupported(boolean suspendable)
179 {
180 _asyncSupported=suspendable;
181 }
182
183
184 public boolean isAsyncSupported()
185 {
186 return _asyncSupported;
187 }
188
189
190
191 @Override
192 public void dump(Appendable out, String indent) throws IOException
193 {
194 super.dump(out,indent);
195 ContainerLifeCycle.dump(out,indent,_initParams.entrySet());
196 }
197
198
199 @Override
200 public String dump()
201 {
202 return super.dump();
203 }
204
205
206 @Override
207 public String toString()
208 {
209 return String.format("%s@%x==%s",_name,hashCode(),_className);
210 }
211
212
213
214
215 protected class HolderConfig
216 {
217
218
219 public ServletContext getServletContext()
220 {
221 return _servletHandler.getServletContext();
222 }
223
224
225 public String getInitParameter(String param)
226 {
227 return Holder.this.getInitParameter(param);
228 }
229
230
231 public Enumeration<String> getInitParameterNames()
232 {
233 return Holder.this.getInitParameterNames();
234 }
235 }
236
237
238
239
240 protected class HolderRegistration implements Registration.Dynamic
241 {
242 public void setAsyncSupported(boolean isAsyncSupported)
243 {
244 illegalStateIfContextStarted();
245 Holder.this.setAsyncSupported(isAsyncSupported);
246 }
247
248 public void setDescription(String description)
249 {
250 if (LOG.isDebugEnabled())
251 LOG.debug(this+" is "+description);
252 }
253
254 public String getClassName()
255 {
256 return Holder.this.getClassName();
257 }
258
259 public String getInitParameter(String name)
260 {
261 return Holder.this.getInitParameter(name);
262 }
263
264 public Map<String, String> getInitParameters()
265 {
266 return Holder.this.getInitParameters();
267 }
268
269 public String getName()
270 {
271 return Holder.this.getName();
272 }
273
274 public boolean setInitParameter(String name, String value)
275 {
276 illegalStateIfContextStarted();
277 if (name == null) {
278 throw new IllegalArgumentException("init parameter name required");
279 }
280 if (value == null) {
281 throw new IllegalArgumentException("non-null value required for init parameter " + name);
282 }
283 if (Holder.this.getInitParameter(name)!=null)
284 return false;
285 Holder.this.setInitParameter(name,value);
286 return true;
287 }
288
289 public Set<String> setInitParameters(Map<String, String> initParameters)
290 {
291 illegalStateIfContextStarted();
292 Set<String> clash=null;
293 for (Map.Entry<String, String> entry : initParameters.entrySet())
294 {
295 if (entry.getKey() == null) {
296 throw new IllegalArgumentException("init parameter name required");
297 }
298 if (entry.getValue() == null) {
299 throw new IllegalArgumentException("non-null value required for init parameter " + entry.getKey());
300 }
301 if (Holder.this.getInitParameter(entry.getKey())!=null)
302 {
303 if (clash==null)
304 clash=new HashSet<String>();
305 clash.add(entry.getKey());
306 }
307 }
308 if (clash!=null)
309 return clash;
310 Holder.this.getInitParameters().putAll(initParameters);
311 return Collections.emptySet();
312 }
313 }
314 }
315
316
317
318
319