View Javadoc
1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.kns.util;
17  
18  import java.util.Collection;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.kuali.rice.kns.util.properties.PropertyTree;
23  
24  /**
25   * This class implements the Map interface for a Properties instance. Exports all properties from the given Properties instance as
26   * constants, usable from jstl. Implements the Map interface (by delegating everything to the PropertyTree, which really implements
27   * the Map methods directly) so that jstl can translate ${Constants.a} into a call to ConfigConstants.get( "a" ).
28   * <p>
29   * The contents of this Map cannot be changed once it has been initialized. Any calls to any of the Map methods made before the
30   * propertyTree has been initialized (i.e. before setProperties has been called) will throw an IllegalStateException.
31   * <p>
32   * Jstl converts ${Constants.a.b.c} into get("a").get("b").get("c"), so the properties are stored in a PropertyTree, which converts
33   * the initial set( "a.b.c", "value" ) into construction of the necessary tree structure to support get("a").get("b").get("c").
34   * <p>
35   * Implicitly relies on the assumption that the JSP will be calling toString() on the result of the final <code>get</code>, since
36   * <code>get</code> can only return one type, and that type must be the complex one so that further dereferencing will be
37   * possible.
38   * 
39   * 
40   */
41  
42  public abstract class JstlPropertyHolder implements Map {
43      private PropertyTree propertyTree;
44  
45      /**
46       * Default constructor
47       */
48      public JstlPropertyHolder() {
49          propertyTree = null;
50      }
51  
52      protected void setProperties(Map<String,String> properties) {
53          propertyTree = new PropertyTree();
54          propertyTree.setProperties(properties);
55      }
56  
57      /**
58       * Copies in the given propertyTree rather than building its own. Reasonably dangerous, since that tree might presumably be
59       * modified, violating the readonlyness of this datastructure.
60       * 
61       * @param properties
62       */
63      protected void setPropertyTree(PropertyTree tree) {
64          propertyTree = tree;
65      }
66  
67  
68      // delegated methods
69      /**
70       * @see org.kuali.rice.kns.util.properties.PropertyTree#get(java.lang.Object)
71       */
72      public Object get(Object key) {
73          if (propertyTree == null) {
74              throw new IllegalStateException("propertyTree has not been initialized");
75          }
76          return this.propertyTree.get(key);
77      }
78  
79      /**
80       * @see org.kuali.rice.kns.util.properties.PropertyTree#size()
81       */
82      public int size() {
83          if (propertyTree == null) {
84              throw new IllegalStateException("propertyTree has not been initialized");
85          }
86          return this.propertyTree.size();
87      }
88  
89      /**
90       * @see org.kuali.rice.kns.util.properties.PropertyTree#clear()
91       */
92      public void clear() {
93          if (propertyTree == null) {
94              throw new IllegalStateException("propertyTree has not been initialized");
95          }
96          this.propertyTree.clear();
97      }
98  
99      /**
100      * @see org.kuali.rice.kns.util.properties.PropertyTree#isEmpty()
101      */
102     public boolean isEmpty() {
103         if (propertyTree == null) {
104             throw new IllegalStateException("propertyTree has not been initialized");
105         }
106         return this.propertyTree.isEmpty();
107     }
108 
109     /**
110      * @see org.kuali.rice.kns.util.properties.PropertyTree#containsKey(java.lang.Object)
111      */
112     public boolean containsKey(Object key) {
113         if (propertyTree == null) {
114             throw new IllegalStateException("propertyTree has not been initialized");
115         }
116         return this.propertyTree.containsKey(key);
117     }
118 
119     /**
120      * @see org.kuali.rice.kns.util.properties.PropertyTree#containsValue(java.lang.Object)
121      */
122     public boolean containsValue(Object value) {
123         if (propertyTree == null) {
124             throw new IllegalStateException("propertyTree has not been initialized");
125         }
126         return this.propertyTree.containsValue(value);
127     }
128 
129     /**
130      * @see org.kuali.rice.kns.util.properties.PropertyTree#values()
131      */
132     public Collection values() {
133         if (propertyTree == null) {
134             throw new IllegalStateException("propertyTree has not been initialized");
135         }
136         return this.propertyTree.values();
137     }
138 
139     /**
140      * @see org.kuali.rice.kns.util.properties.PropertyTree#putAll(java.util.Map)
141      */
142     public void putAll(Map m) {
143         if (propertyTree == null) {
144             throw new IllegalStateException("propertyTree has not been initialized");
145         }
146         this.propertyTree.putAll(m);
147     }
148 
149     /**
150      * @see org.kuali.rice.kns.util.properties.PropertyTree#entrySet()
151      */
152     public Set entrySet() {
153         if (propertyTree == null) {
154             throw new IllegalStateException("propertyTree has not been initialized");
155         }
156         return this.propertyTree.entrySet();
157     }
158 
159     /**
160      * @see org.kuali.rice.kns.util.properties.PropertyTree#keySet()
161      */
162     public Set keySet() {
163         if (propertyTree == null) {
164             throw new IllegalStateException("propertyTree has not been initialized");
165         }
166         return this.propertyTree.keySet();
167     }
168 
169     /**
170      * @see org.kuali.rice.kns.util.properties.PropertyTree#remove(java.lang.Object)
171      */
172     public Object remove(Object key) {
173         if (propertyTree == null) {
174             throw new IllegalStateException("propertyTree has not been initialized");
175         }
176         return this.propertyTree.remove(key);
177     }
178 
179     /**
180      * @see org.kuali.rice.kns.util.properties.PropertyTree#put(java.lang.Object, java.lang.Object)
181      */
182     public Object put(Object key, Object value) {
183         if (propertyTree == null) {
184             throw new IllegalStateException("propertyTree has not been initialized");
185         }
186         return this.propertyTree.put(key, value);
187     }
188 }