View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Holder
42   * 
43   * Specialization of AbstractHolder for servlet-related classes that 
44   * have init-params etc
45   * @param <T> the type of holder
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  
57  
58      /* ---------------------------------------------------------------- */
59      protected Holder(Source source)
60      {
61          super(source);
62          switch(_source)
63          {
64              case JAVAX_API:
65              case DESCRIPTOR:
66              case ANNOTATION:
67                  _asyncSupported=false;
68                  break;
69              default:
70                  _asyncSupported=true;
71          }
72      }
73  
74    
75  
76      /* ------------------------------------------------------------ */
77      @ManagedAttribute(value="Display Name", readonly=true)
78      public String getDisplayName()
79      {
80          return _displayName;
81      }
82  
83      /* ---------------------------------------------------------------- */
84      public String getInitParameter(String param)
85      {
86          if (_initParams==null)
87              return null;
88          return (String)_initParams.get(param);
89      }
90  
91      /* ------------------------------------------------------------ */
92      public Enumeration<String> getInitParameterNames()
93      {
94          if (_initParams==null)
95              return Collections.enumeration(Collections.EMPTY_LIST);
96          return Collections.enumeration(_initParams.keySet());
97      }
98  
99      /* ---------------------------------------------------------------- */
100     @ManagedAttribute(value="Initial Parameters", readonly=true)
101     public Map<String,String> getInitParameters()
102     {
103         return _initParams;
104     }
105 
106     /* ------------------------------------------------------------ */
107     @ManagedAttribute(value="Name", readonly=true)
108     public String getName()
109     {
110         return _name;
111     }
112 
113   
114     /* ------------------------------------------------------------ */
115     public void destroyInstance(Object instance)
116     throws Exception
117     {
118     }
119     /* ------------------------------------------------------------ */
120     /**
121      * @param className The className to set.
122      */
123     public void setClassName(String className)
124     {
125         super.setClassName(className);
126         if (_name==null)
127             _name=className+"-"+Integer.toHexString(this.hashCode());
128     }
129 
130     /* ------------------------------------------------------------ */
131     /**
132      * @param held The class to hold
133      */
134     public void setHeldClass(Class<? extends T> held)
135     {
136         super.setHeldClass(held);
137         if (held!=null)
138         {
139             if (_name==null)
140                 _name=held.getName()+"-"+Integer.toHexString(this.hashCode());
141         }
142     }
143 
144     /* ------------------------------------------------------------ */
145     public void setDisplayName(String name)
146     {
147         _displayName=name;
148     }
149 
150     /* ------------------------------------------------------------ */
151     public void setInitParameter(String param,String value)
152     {
153         _initParams.put(param,value);
154     }
155 
156     /* ---------------------------------------------------------------- */
157     public void setInitParameters(Map<String,String> map)
158     {
159         _initParams.clear();
160         _initParams.putAll(map);
161     }
162 
163     /* ------------------------------------------------------------ */
164     /**
165      * The name is a primary key for the held object.
166      * Ensure that the name is set BEFORE adding a Holder
167      * (eg ServletHolder or FilterHolder) to a ServletHandler.
168      * @param name The name to set.
169      */
170     public void setName(String name)
171     {
172         _name = name;
173     }
174 
175 
176     /* ------------------------------------------------------------ */
177     public void setAsyncSupported(boolean suspendable)
178     {
179         _asyncSupported=suspendable;
180     }
181 
182     /* ------------------------------------------------------------ */
183     public boolean isAsyncSupported()
184     {
185         return _asyncSupported;
186     }
187 
188 
189     /* ------------------------------------------------------------ */
190     @Override
191     public void dump(Appendable out, String indent) throws IOException
192     {
193         super.dump(out,indent);
194         ContainerLifeCycle.dump(out,indent,_initParams.entrySet());
195     }
196 
197     /* ------------------------------------------------------------ */
198     @Override
199     public String dump()
200     {
201         return super.dump();
202     }
203 
204     /* ------------------------------------------------------------ */
205     @Override
206     public String toString()
207     {
208         return String.format("%s@%x==%s",_name,hashCode(),_className);
209     }
210     
211     /* ------------------------------------------------------------ */
212     /* ------------------------------------------------------------ */
213     /* ------------------------------------------------------------ */
214     protected class HolderConfig
215     {
216 
217         /* -------------------------------------------------------- */
218         public ServletContext getServletContext()
219         {
220             return _servletHandler.getServletContext();
221         }
222 
223         /* -------------------------------------------------------- */
224         public String getInitParameter(String param)
225         {
226             return Holder.this.getInitParameter(param);
227         }
228 
229         /* -------------------------------------------------------- */
230         public Enumeration<String> getInitParameterNames()
231         {
232             return Holder.this.getInitParameterNames();
233         }
234     }
235 
236     /* -------------------------------------------------------- */
237     /* -------------------------------------------------------- */
238     /* -------------------------------------------------------- */
239     protected class HolderRegistration implements Registration.Dynamic
240     {
241         public void setAsyncSupported(boolean isAsyncSupported)
242         {
243             illegalStateIfContextStarted();
244             Holder.this.setAsyncSupported(isAsyncSupported);
245         }
246 
247         public void setDescription(String description)
248         {
249             if (LOG.isDebugEnabled())
250                 LOG.debug(this+" is "+description);
251         }
252 
253         public String getClassName()
254         {
255             return Holder.this.getClassName();
256         }
257 
258         public String getInitParameter(String name)
259         {
260             return Holder.this.getInitParameter(name);
261         }
262 
263         public Map<String, String> getInitParameters()
264         {
265             return Holder.this.getInitParameters();
266         }
267 
268         public String getName()
269         {
270             return Holder.this.getName();
271         }
272 
273         public boolean setInitParameter(String name, String value)
274         {
275             illegalStateIfContextStarted();
276             if (name == null) {
277                 throw new IllegalArgumentException("init parameter name required");
278             }
279             if (value == null) {
280                 throw new IllegalArgumentException("non-null value required for init parameter " + name);
281             }
282             if (Holder.this.getInitParameter(name)!=null)
283                 return false;
284             Holder.this.setInitParameter(name,value);
285             return true;
286         }
287 
288         public Set<String> setInitParameters(Map<String, String> initParameters)
289         {
290             illegalStateIfContextStarted();
291             Set<String> clash=null;
292             for (Map.Entry<String, String> entry : initParameters.entrySet())
293             {
294                 if (entry.getKey() == null) {
295                     throw new IllegalArgumentException("init parameter name required");
296                 }
297                 if (entry.getValue() == null) {
298                     throw new IllegalArgumentException("non-null value required for init parameter " + entry.getKey());
299                 }
300                 if (Holder.this.getInitParameter(entry.getKey())!=null)
301                 {
302                     if (clash==null)
303                         clash=new HashSet<String>();
304                     clash.add(entry.getKey());
305                 }
306             }
307             if (clash!=null)
308                 return clash;
309             Holder.this.getInitParameters().putAll(initParameters);
310             return Collections.emptySet();
311         }
312     }
313 }
314 
315 
316 
317 
318