001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.core.util.properties;
017:
018: import java.util.Iterator;
019: import java.util.Properties;
020:
021: import org.kuali.core.exceptions.DuplicateKeyException;
022: import org.kuali.core.exceptions.PropertiesException;
023:
024: /**
025: * This class is a Property container. It is able to load properties from various property-sources.
026: *
027: *
028: */
029: public class PropertyHolder {
030: Properties heldProperties;
031:
032: /**
033: * Default constructor.
034: */
035: public PropertyHolder() {
036: this .heldProperties = new Properties();
037: }
038:
039: /**
040: * @return true if this container currently has no properties
041: */
042: public boolean isEmpty() {
043: return this .heldProperties.isEmpty();
044: }
045:
046: /**
047: * @param key
048: * @return true if a property with the given key exists in this container
049: * @throws IllegalArgumentException if the given key is null
050: */
051: public boolean containsKey(String key) {
052: validateKey(key);
053:
054: return this .heldProperties.containsKey(key);
055: }
056:
057: /**
058: * @param key
059: * @return the current value of the property with the given key, or null if no property exists with that key
060: * @throws IllegalArgumentException if the given key is null
061: */
062: public String getProperty(String key) {
063: validateKey(key);
064:
065: return this .heldProperties.getProperty(key);
066: }
067:
068: /**
069: * Associates the given value with the given key
070: *
071: * @param key
072: * @param value
073: * @throws IllegalArgumentException if the given key is null
074: * @throws IllegalArgumentException if the given value is null
075: * @throws DuplicateKeyException if a property with the given key already exists
076: */
077: public void setProperty(String key, String value) {
078: validateKey(key);
079: validateValue(value);
080:
081: if (containsKey(key)) {
082: throw new DuplicateKeyException("duplicate key '" + key
083: + "'");
084: }
085: this .heldProperties.setProperty(key, value);
086: }
087:
088: /**
089: * Removes the property with the given key from this container
090: *
091: * @param key
092: * @throws IllegalArgumentException if the given key is null
093: */
094: public void clearProperty(String key) {
095: validateKey(key);
096:
097: this .heldProperties.remove(key);
098: }
099:
100: /**
101: * Copies all name,value pairs from the given PropertySource instance into this container.
102: *
103: * @param source
104: * @throws IllegalStateException if the source is invalid (improperly initialized)
105: * @throws DuplicateKeyException the first time a given property has the same key as an existing property
106: * @throws PropertiesException if unable to load properties from the given source
107: */
108: public void loadProperties(PropertySource source) {
109: if (source == null) {
110: throw new IllegalArgumentException("invalid (null) source");
111: }
112:
113: Properties newProperties = source.loadProperties();
114:
115: for (Iterator i = newProperties.keySet().iterator(); i
116: .hasNext();) {
117: String key = (String) i.next();
118: setProperty(key, newProperties.getProperty(key));
119: }
120: }
121:
122: /**
123: * Removes all properties from this container.
124: */
125: public void clearProperties() {
126: this .heldProperties.clear();
127: }
128:
129: /**
130: * @return iterator over the keys of all properties in this container
131: */
132: public Iterator getKeys() {
133: return this .heldProperties.keySet().iterator();
134: }
135:
136: /**
137: * @param key
138: * @throws IllegalArgumentException if the given key is null
139: */
140: private void validateKey(String key) {
141: if (key == null) {
142: throw new IllegalArgumentException("invalid (null) key");
143: }
144: }
145:
146: /**
147: * @param value
148: * @throws IllegalArgumentException if the given value is null
149: */
150: private void validateValue(String value) {
151: if (value == null) {
152: throw new IllegalArgumentException("invalid (null) value");
153: }
154: }
155:
156: public Properties getHeldProperties() {
157: return heldProperties;
158: }
159:
160: public void setHeldProperties(Properties heldProperties) {
161: this.heldProperties = heldProperties;
162: }
163: }
|