1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.servlet;
15
16 import java.util.Collections;
17 import java.util.Enumeration;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.servlet.ServletContext;
22 import javax.servlet.UnavailableException;
23
24 import org.eclipse.jetty.server.handler.ContextHandler;
25 import org.eclipse.jetty.util.Loader;
26 import org.eclipse.jetty.util.component.AbstractLifeCycle;
27 import org.eclipse.jetty.util.log.Log;
28
29
30
31
32
33
34 public class Holder extends AbstractLifeCycle
35 {
36 protected transient Class<?> _class;
37 protected String _className;
38 protected String _displayName;
39 protected Map _initParams;
40 protected boolean _extInstance;
41 protected boolean _asyncSupported=true;
42
43
44 protected String _name;
45 protected ServletHandler _servletHandler;
46
47 protected Holder()
48 {}
49
50
51 protected Holder(Class held)
52 {
53 _class=held;
54 if (held!=null)
55 {
56 _className=held.getName();
57 _name=held.getName()+"-"+this.hashCode();
58 }
59 }
60
61
62 public void doStart()
63 throws Exception
64 {
65
66 if (_class==null && (_className==null || _className.equals("")))
67 throw new UnavailableException("No class for Servlet or Filter", -1);
68
69
70 if (_class==null)
71 {
72 try
73 {
74 _class=Loader.loadClass(Holder.class, _className);
75 if(Log.isDebugEnabled())Log.debug("Holding {}",_class);
76 }
77 catch (Exception e)
78 {
79 Log.warn(e);
80 throw new UnavailableException(e.getMessage(), -1);
81 }
82 }
83 }
84
85
86 public void doStop()
87 throws Exception
88 {
89 if (!_extInstance)
90 _class=null;
91 }
92
93
94 public String getClassName()
95 {
96 return _className;
97 }
98
99
100 public Class getHeldClass()
101 {
102 return _class;
103 }
104
105
106 public String getDisplayName()
107 {
108 return _displayName;
109 }
110
111
112 public String getInitParameter(String param)
113 {
114 if (_initParams==null)
115 return null;
116 return (String)_initParams.get(param);
117 }
118
119
120 public Enumeration getInitParameterNames()
121 {
122 if (_initParams==null)
123 return Collections.enumeration(Collections.EMPTY_LIST);
124 return Collections.enumeration(_initParams.keySet());
125 }
126
127
128 public Map getInitParameters()
129 {
130 return _initParams;
131 }
132
133
134 public String getName()
135 {
136 return _name;
137 }
138
139
140
141
142
143 public ServletHandler getServletHandler()
144 {
145 return _servletHandler;
146 }
147
148
149 public synchronized Object newInstance()
150 throws InstantiationException,
151 IllegalAccessException
152 {
153 if (_class==null)
154 throw new InstantiationException("!"+_className);
155 return _class.newInstance();
156 }
157
158
159 public void destroyInstance(Object instance)
160 throws Exception
161 {
162 }
163
164
165
166
167
168 public void setClassName(String className)
169 {
170 _className = className;
171 _class=null;
172 }
173
174
175
176
177
178 public void setHeldClass(Class held)
179 {
180 _class=held;
181 _className = held!=null?held.getName():null;
182 }
183
184
185 public void setDisplayName(String name)
186 {
187 _displayName=name;
188 }
189
190
191 public void setInitParameter(String param,String value)
192 {
193 if (_initParams==null)
194 _initParams=new HashMap(3);
195 _initParams.put(param,value);
196 }
197
198
199 public void setInitParameters(Map map)
200 {
201 _initParams=map;
202 }
203
204
205
206
207
208
209
210
211 public void setName(String name)
212 {
213 _name = name;
214 }
215
216
217
218
219
220 public void setServletHandler(ServletHandler servletHandler)
221 {
222 _servletHandler = servletHandler;
223 }
224
225
226 public void setAsyncSupported(boolean suspendable)
227 {
228 _asyncSupported=suspendable;
229 }
230
231
232 public boolean isAsyncSupported()
233 {
234 return _asyncSupported;
235 }
236
237
238 public String toString()
239 {
240 return _name;
241 }
242
243
244 protected void illegalStateIfContextStarted()
245 {
246 if (_servletHandler!=null)
247 {
248 ContextHandler.Context context=(ContextHandler.Context)_servletHandler.getServletContext();
249 if (context!=null && context.getContextHandler().isStarted())
250 throw new IllegalStateException("Started");
251 }
252 }
253
254
255
256
257 protected class HolderConfig
258 {
259
260
261 public ServletContext getServletContext()
262 {
263 return _servletHandler.getServletContext();
264 }
265
266
267 public String getInitParameter(String param)
268 {
269 return Holder.this.getInitParameter(param);
270 }
271
272
273 public Enumeration getInitParameterNames()
274 {
275 return Holder.this.getInitParameterNames();
276 }
277 }
278 }
279
280
281
282
283