2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
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;
29 import javax.servlet.Registration;
30 import javax.servlet.ServletContext;
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;
39 /* --------------------------------------------------------------------- */
43 * Specialization of AbstractHolder for servlet-related classes that
44 * have init-params etc
47 @ManagedObject("Holder - a container for servlets and the like")
48 public class Holder<T> extends BaseHolder<T>
50 private static final Logger LOG = Log.getLogger(Holder.class);
52 protected final Map<String,String> _initParams=new HashMap<String,String>(3);
53 protected String _displayName;
54 protected boolean _asyncSupported;
55 protected String _name;
58 /* ---------------------------------------------------------------- */
59 protected Holder(Source source)
67 _asyncSupported=false;
76 /* ------------------------------------------------------------ */
77 @ManagedAttribute(value="Display Name", readonly=true)
78 public String getDisplayName()
83 /* ---------------------------------------------------------------- */
84 public String getInitParameter(String param)
86 if (_initParams==null)
88 return (String)_initParams.get(param);
91 /* ------------------------------------------------------------ */
92 public Enumeration getInitParameterNames()
94 if (_initParams==null)
95 return Collections.enumeration(Collections.EMPTY_LIST);
96 return Collections.enumeration(_initParams.keySet());
99 /* ---------------------------------------------------------------- */
100 @ManagedAttribute(value="Initial Parameters", readonly=true)
101 public Map<String,String> getInitParameters()
106 /* ------------------------------------------------------------ */
107 @ManagedAttribute(value="Name", readonly=true)
108 public String getName()
114 /* ------------------------------------------------------------ */
115 public void destroyInstance(Object instance)
119 /* ------------------------------------------------------------ */
121 * @param className The className to set.
123 public void setClassName(String className)
125 super.setClassName(className);
127 _name=className+"-"+Integer.toHexString(this.hashCode());
130 /* ------------------------------------------------------------ */
132 * @param held The class to hold
134 public void setHeldClass(Class<? extends T> held)
136 super.setHeldClass(held);
140 _name=held.getName()+"-"+Integer.toHexString(this.hashCode());
144 /* ------------------------------------------------------------ */
145 public void setDisplayName(String name)
150 /* ------------------------------------------------------------ */
151 public void setInitParameter(String param,String value)
153 _initParams.put(param,value);
156 /* ---------------------------------------------------------------- */
157 public void setInitParameters(Map<String,String> map)
160 _initParams.putAll(map);
163 /* ------------------------------------------------------------ */
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.
170 public void setName(String name)
176 /* ------------------------------------------------------------ */
177 public void setAsyncSupported(boolean suspendable)
179 _asyncSupported=suspendable;
182 /* ------------------------------------------------------------ */
183 public boolean isAsyncSupported()
185 return _asyncSupported;
189 /* ------------------------------------------------------------ */
191 public void dump(Appendable out, String indent) throws IOException
193 super.dump(out,indent);
194 ContainerLifeCycle.dump(out,indent,_initParams.entrySet());
197 /* ------------------------------------------------------------ */
204 /* ------------------------------------------------------------ */
206 public String toString()
208 return String.format("%s@%x==%s",_name,hashCode(),_className);
211 /* ------------------------------------------------------------ */
212 /* ------------------------------------------------------------ */
213 /* ------------------------------------------------------------ */
214 protected class HolderConfig
217 /* -------------------------------------------------------- */
218 public ServletContext getServletContext()
220 return _servletHandler.getServletContext();
223 /* -------------------------------------------------------- */
224 public String getInitParameter(String param)
226 return Holder.this.getInitParameter(param);
229 /* -------------------------------------------------------- */
230 public Enumeration getInitParameterNames()
232 return Holder.this.getInitParameterNames();
236 /* -------------------------------------------------------- */
237 /* -------------------------------------------------------- */
238 /* -------------------------------------------------------- */
239 protected class HolderRegistration implements Registration.Dynamic
241 public void setAsyncSupported(boolean isAsyncSupported)
243 illegalStateIfContextStarted();
244 Holder.this.setAsyncSupported(isAsyncSupported);
247 public void setDescription(String description)
249 if (LOG.isDebugEnabled())
250 LOG.debug(this+" is "+description);
253 public String getClassName()
255 return Holder.this.getClassName();
258 public String getInitParameter(String name)
260 return Holder.this.getInitParameter(name);
263 public Map<String, String> getInitParameters()
265 return Holder.this.getInitParameters();
268 public String getName()
270 return Holder.this.getName();
273 public boolean setInitParameter(String name, String value)
275 illegalStateIfContextStarted();
277 throw new IllegalArgumentException("init parameter name required");
280 throw new IllegalArgumentException("non-null value required for init parameter " + name);
282 if (Holder.this.getInitParameter(name)!=null)
284 Holder.this.setInitParameter(name,value);
288 public Set<String> setInitParameters(Map<String, String> initParameters)
290 illegalStateIfContextStarted();
291 Set<String> clash=null;
292 for (Map.Entry<String, String> entry : initParameters.entrySet())
294 if (entry.getKey() == null) {
295 throw new IllegalArgumentException("init parameter name required");
297 if (entry.getValue() == null) {
298 throw new IllegalArgumentException("non-null value required for init parameter " + entry.getKey());
300 if (Holder.this.getInitParameter(entry.getKey())!=null)
303 clash=new HashSet<String>();
304 clash.add(entry.getKey());
309 Holder.this.getInitParameters().putAll(initParameters);
310 return Collections.emptySet();