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