View Javadoc

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