%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.configuration.web.ServletContextConfiguration |
|
|
1 | /* |
|
2 | * Copyright 2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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 | 54 | */ |
47 | 54 | public ServletContextConfiguration(Servlet servlet) |
48 | 152 | { |
49 | 98 | this.context = servlet.getServletConfig().getServletContext(); |
50 | 98 | } |
51 | ||
52 | /** |
|
53 | * Create a ServletContextConfiguration using the servlet context |
|
54 | * initialization parameters. |
|
55 | * |
|
56 | * @param context the servlet context |
|
57 | 18 | */ |
58 | 18 | public ServletContextConfiguration(ServletContext context) |
59 | 32 | { |
60 | 14 | this.context = context; |
61 | 14 | } |
62 | ||
63 | 45 | public Object getProperty(String key) |
64 | { |
|
65 | 98 | Object value = context.getInitParameter(key); |
66 | 98 | List list = PropertyConverter.split((String) value, getDelimiter()); |
67 | ||
68 | 98 | 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 | 9 | * |
75 | * @throws UnsupportedOperationException |
|
76 | */ |
|
77 | protected void addPropertyDirect(String key, Object obj) |
|
78 | { |
|
79 | 32 | throw new UnsupportedOperationException("Read only configuration"); |
80 | } |
|
81 | ||
82 | public boolean isEmpty() |
|
83 | { |
|
84 | 46 | return !getKeys().hasNext(); |
85 | } |
|
86 | ||
87 | public boolean containsKey(String key) |
|
88 | { |
|
89 | 28 | return getProperty(key) != null; |
90 | } |
|
91 | ||
92 | /** |
|
93 | * <p><strong>This operation is not supported and will throw an |
|
94 | * UnsupportedOperationException.</strong></p> |
|
95 | 9 | * |
96 | * @throws UnsupportedOperationException |
|
97 | */ |
|
98 | public void clearProperty(String key) |
|
99 | { |
|
100 | 50 | throw new UnsupportedOperationException("Read only configuration"); |
101 | } |
|
102 | ||
103 | public Iterator getKeys() |
|
104 | { |
|
105 | 42 | return new EnumerationIterator(context.getInitParameterNames()); |
106 | } |
|
107 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |