001/*
002 * Copyright 2005-2008 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.util;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.Set;
021
022import org.kuali.rice.kns.util.properties.PropertyTree;
023
024/**
025 * This class implements the Map interface for a Properties instance. Exports all properties from the given Properties instance as
026 * constants, usable from jstl. Implements the Map interface (by delegating everything to the PropertyTree, which really implements
027 * the Map methods directly) so that jstl can translate ${Constants.a} into a call to ConfigConstants.get( "a" ).
028 * <p>
029 * The contents of this Map cannot be changed once it has been initialized. Any calls to any of the Map methods made before the
030 * propertyTree has been initialized (i.e. before setProperties has been called) will throw an IllegalStateException.
031 * <p>
032 * Jstl converts ${Constants.a.b.c} into get("a").get("b").get("c"), so the properties are stored in a PropertyTree, which converts
033 * the initial set( "a.b.c", "value" ) into construction of the necessary tree structure to support get("a").get("b").get("c").
034 * <p>
035 * Implicitly relies on the assumption that the JSP will be calling toString() on the result of the final <code>get</code>, since
036 * <code>get</code> can only return one type, and that type must be the complex one so that further dereferencing will be
037 * possible.
038 * 
039 * 
040 */
041
042public abstract class JstlPropertyHolder implements Map {
043    private PropertyTree propertyTree;
044
045    /**
046     * Default constructor
047     */
048    public JstlPropertyHolder() {
049        propertyTree = null;
050    }
051
052    protected void setProperties(Map<String,String> properties) {
053        propertyTree = new PropertyTree();
054        propertyTree.setProperties(properties);
055    }
056
057    /**
058     * Copies in the given propertyTree rather than building its own. Reasonably dangerous, since that tree might presumably be
059     * modified, violating the readonlyness of this datastructure.
060     * 
061     * @param properties
062     */
063    protected void setPropertyTree(PropertyTree tree) {
064        propertyTree = tree;
065    }
066
067
068    // delegated methods
069    /**
070     * @see org.kuali.rice.kns.util.properties.PropertyTree#get(java.lang.Object)
071     */
072    public Object get(Object key) {
073        if (propertyTree == null) {
074            throw new IllegalStateException("propertyTree has not been initialized");
075        }
076        return this.propertyTree.get(key);
077    }
078
079    /**
080     * @see org.kuali.rice.kns.util.properties.PropertyTree#size()
081     */
082    public int size() {
083        if (propertyTree == null) {
084            throw new IllegalStateException("propertyTree has not been initialized");
085        }
086        return this.propertyTree.size();
087    }
088
089    /**
090     * @see org.kuali.rice.kns.util.properties.PropertyTree#clear()
091     */
092    public void clear() {
093        if (propertyTree == null) {
094            throw new IllegalStateException("propertyTree has not been initialized");
095        }
096        this.propertyTree.clear();
097    }
098
099    /**
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}