1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.web;
18
19 import java.util.Iterator;
20 import java.util.List;
21 import javax.servlet.Servlet;
22 import javax.servlet.ServletContext;
23
24 import org.apache.commons.collections.iterators.EnumerationIterator;
25 import org.apache.commons.configuration.AbstractConfiguration;
26 import org.apache.commons.configuration.PropertyConverter;
27
28 /***
29 * A configuration wrapper to read the initialization parameters of a servlet
30 * context. This configuration is read only, adding or removing a property will
31 * throw an UnsupportedOperationException.
32 *
33 * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
34 * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
35 * @since 1.1
36 */
37 public class ServletContextConfiguration extends AbstractConfiguration
38 {
39 protected ServletContext context;
40
41 /***
42 * Create a ServletContextConfiguration using the context of
43 * the specified servlet.
44 *
45 * @param servlet the servlet
46 */
47 public ServletContextConfiguration(Servlet servlet)
48 {
49 this.context = servlet.getServletConfig().getServletContext();
50 }
51
52 /***
53 * Create a ServletContextConfiguration using the servlet context
54 * initialization parameters.
55 *
56 * @param context the servlet context
57 */
58 public ServletContextConfiguration(ServletContext context)
59 {
60 this.context = context;
61 }
62
63 public Object getProperty(String key)
64 {
65 Object value = context.getInitParameter(key);
66 List list = PropertyConverter.split((String) value, getDelimiter());
67
68 return list.size() > 1 ? list : value;
69 }
70
71 /***
72 * <p><strong>This operation is not supported and will throw an
73 * UnsupportedOperationException.</strong></p>
74 *
75 * @throws UnsupportedOperationException
76 */
77 protected void addPropertyDirect(String key, Object obj)
78 {
79 throw new UnsupportedOperationException("Read only configuration");
80 }
81
82 public boolean isEmpty()
83 {
84 return !getKeys().hasNext();
85 }
86
87 public boolean containsKey(String key)
88 {
89 return getProperty(key) != null;
90 }
91
92 /***
93 * <p><strong>This operation is not supported and will throw an
94 * UnsupportedOperationException.</strong></p>
95 *
96 * @throws UnsupportedOperationException
97 */
98 public void clearProperty(String key)
99 {
100 throw new UnsupportedOperationException("Read only configuration");
101 }
102
103 public Iterator getKeys()
104 {
105 return new EnumerationIterator(context.getInitParameterNames());
106 }
107 }