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