1 // ========================================================================
2 // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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.AttributesMap;
26 import org.eclipse.jetty.util.Loader;
27 import org.eclipse.jetty.util.component.AbstractLifeCycle;
28 import org.eclipse.jetty.util.log.Log;
29
30
31 /* --------------------------------------------------------------------- */
32 /**
33 *
34 */
35 public class Holder extends AbstractLifeCycle
36 {
37 protected transient Class<?> _class;
38 protected String _className;
39 protected String _displayName;
40 protected Map _initParams;
41 protected boolean _extInstance;
42 protected boolean _asyncSupported=true;
43
44 /* ---------------------------------------------------------------- */
45 protected String _name;
46 protected ServletHandler _servletHandler;
47
48 protected Holder()
49 {}
50
51 /* ---------------------------------------------------------------- */
52 protected Holder(Class held)
53 {
54 _class=held;
55 if (held!=null)
56 {
57 _className=held.getName();
58 _name=held.getName()+"-"+this.hashCode();
59 }
60 }
61
62 /* ------------------------------------------------------------ */
63 public void doStart()
64 throws Exception
65 {
66 //if no class already loaded and no classname, make servlet permanently unavailable
67 if (_class==null && (_className==null || _className.equals("")))
68 throw new UnavailableException("No class for Servlet or Filter", -1);
69
70 //try to load class
71 if (_class==null)
72 {
73 try
74 {
75 _class=Loader.loadClass(Holder.class, _className);
76 if(Log.isDebugEnabled())Log.debug("Holding {}",_class);
77 }
78 catch (Exception e)
79 {
80 Log.warn(e);
81 throw new UnavailableException(e.getMessage(), -1);
82 }
83 }
84 }
85
86 /* ------------------------------------------------------------ */
87 public void doStop()
88 throws Exception
89 {
90 if (!_extInstance)
91 _class=null;
92 }
93
94 /* ------------------------------------------------------------ */
95 public String getClassName()
96 {
97 return _className;
98 }
99
100 /* ------------------------------------------------------------ */
101 public Class getHeldClass()
102 {
103 return _class;
104 }
105
106 /* ------------------------------------------------------------ */
107 public String getDisplayName()
108 {
109 return _displayName;
110 }
111
112 /* ---------------------------------------------------------------- */
113 public String getInitParameter(String param)
114 {
115 if (_initParams==null)
116 return null;
117 return (String)_initParams.get(param);
118 }
119
120 /* ------------------------------------------------------------ */
121 public Enumeration getInitParameterNames()
122 {
123 if (_initParams==null)
124 return Collections.enumeration(Collections.EMPTY_LIST);
125 return Collections.enumeration(_initParams.keySet());
126 }
127
128 /* ---------------------------------------------------------------- */
129 public Map getInitParameters()
130 {
131 return _initParams;
132 }
133
134 /* ------------------------------------------------------------ */
135 public String getName()
136 {
137 return _name;
138 }
139
140 /* ------------------------------------------------------------ */
141 /**
142 * @return Returns the servletHandler.
143 */
144 public ServletHandler getServletHandler()
145 {
146 return _servletHandler;
147 }
148
149 /* ------------------------------------------------------------ */
150 public synchronized Object newInstance()
151 throws InstantiationException,
152 IllegalAccessException
153 {
154 if (_class==null)
155 throw new InstantiationException("!"+_className);
156 return _class.newInstance();
157 }
158
159 /* ------------------------------------------------------------ */
160 public void destroyInstance(Object instance)
161 throws Exception
162 {
163 }
164
165 /* ------------------------------------------------------------ */
166 /**
167 * @param className The className to set.
168 */
169 public void setClassName(String className)
170 {
171 _className = className;
172 _class=null;
173 }
174
175 /* ------------------------------------------------------------ */
176 /**
177 * @param className The className to set.
178 */
179 public void setHeldClass(Class held)
180 {
181 _class=held;
182 _className = held!=null?held.getName():null;
183 }
184
185 /* ------------------------------------------------------------ */
186 public void setDisplayName(String name)
187 {
188 _displayName=name;
189 }
190
191 /* ------------------------------------------------------------ */
192 public void setInitParameter(String param,String value)
193 {
194 if (_initParams==null)
195 _initParams=new HashMap(3);
196 _initParams.put(param,value);
197 }
198
199 /* ---------------------------------------------------------------- */
200 public void setInitParameters(Map map)
201 {
202 _initParams=map;
203 }
204
205 /* ------------------------------------------------------------ */
206 /**
207 * The name is a primary key for the held object.
208 * Ensure that the name is set BEFORE adding a Holder
209 * (eg ServletHolder or FilterHolder) to a ServletHandler.
210 * @param name The name to set.
211 */
212 public void setName(String name)
213 {
214 _name = name;
215 }
216
217 /* ------------------------------------------------------------ */
218 /**
219 * @param servletHandler The {@link ServletHandler} that will handle requests dispatched to this servlet.
220 */
221 public void setServletHandler(ServletHandler servletHandler)
222 {
223 _servletHandler = servletHandler;
224 }
225
226 /* ------------------------------------------------------------ */
227 public void setAsyncSupported(boolean suspendable)
228 {
229 _asyncSupported=suspendable;
230 }
231
232 /* ------------------------------------------------------------ */
233 public boolean isAsyncSupported()
234 {
235 return _asyncSupported;
236 }
237
238 /* ------------------------------------------------------------ */
239 public String toString()
240 {
241 return _name;
242 }
243
244 /* ------------------------------------------------------------ */
245 protected void illegalStateIfContextStarted()
246 {
247 if (_servletHandler!=null)
248 {
249 ContextHandler.Context context=(ContextHandler.Context)_servletHandler.getServletContext();
250 if (context!=null && context.getContextHandler().isStarted())
251 throw new IllegalStateException("Started");
252 }
253 }
254
255 /* ------------------------------------------------------------ */
256 /* ------------------------------------------------------------ */
257 /* ------------------------------------------------------------ */
258 protected class HolderConfig
259 {
260
261 /* -------------------------------------------------------- */
262 public ServletContext getServletContext()
263 {
264 return _servletHandler.getServletContext();
265 }
266
267 /* -------------------------------------------------------- */
268 public String getInitParameter(String param)
269 {
270 return Holder.this.getInitParameter(param);
271 }
272
273 /* -------------------------------------------------------- */
274 public Enumeration getInitParameterNames()
275 {
276 return Holder.this.getInitParameterNames();
277 }
278 }
279 }
280
281
282
283
284