| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PropertyHolder |
|
| 1.8571428571428572;1.857 |
| 1 | /* | |
| 2 | * Copyright 2005-2007 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.properties; | |
| 17 | ||
| 18 | import java.util.Iterator; | |
| 19 | import java.util.Properties; | |
| 20 | ||
| 21 | import org.apache.log4j.Logger; | |
| 22 | import org.kuali.rice.krad.exception.DuplicateKeyException; | |
| 23 | import org.kuali.rice.krad.exception.PropertiesException; | |
| 24 | ||
| 25 | /** | |
| 26 | * This class is a Property container. It is able to load properties from various property-sources. | |
| 27 | * | |
| 28 | * | |
| 29 | */ | |
| 30 | public class PropertyHolder { | |
| 31 | 1 | private static Logger LOG = Logger.getLogger(PropertyHolder.class); |
| 32 | ||
| 33 | Properties heldProperties; | |
| 34 | ||
| 35 | /** | |
| 36 | * Default constructor. | |
| 37 | */ | |
| 38 | 24 | public PropertyHolder() { |
| 39 | 24 | this.heldProperties = new Properties(); |
| 40 | 24 | } |
| 41 | ||
| 42 | ||
| 43 | /** | |
| 44 | * @return true if this container currently has no properties | |
| 45 | */ | |
| 46 | public boolean isEmpty() { | |
| 47 | 8 | return this.heldProperties.isEmpty(); |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * @param key | |
| 52 | * @return true if a property with the given key exists in this container | |
| 53 | * @throws IllegalArgumentException if the given key is null | |
| 54 | */ | |
| 55 | public boolean containsKey(String key) { | |
| 56 | 56 | validateKey(key); |
| 57 | ||
| 58 | 55 | return this.heldProperties.containsKey(key); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * @param key | |
| 63 | * @return the current value of the property with the given key, or null if no property exists with that key | |
| 64 | * @throws IllegalArgumentException if the given key is null | |
| 65 | */ | |
| 66 | public String getProperty(String key) { | |
| 67 | 11 | validateKey(key); |
| 68 | ||
| 69 | 10 | return this.heldProperties.getProperty(key); |
| 70 | } | |
| 71 | ||
| 72 | ||
| 73 | /** | |
| 74 | * Associates the given value with the given key | |
| 75 | * | |
| 76 | * @param key | |
| 77 | * @param value | |
| 78 | * @throws IllegalArgumentException if the given key is null | |
| 79 | * @throws IllegalArgumentException if the given value is null | |
| 80 | * @throws DuplicateKeyException if a property with the given key already exists | |
| 81 | */ | |
| 82 | public void setProperty(String key, String value) { | |
| 83 | 37 | setProperty(null, key, value); |
| 84 | 34 | } |
| 85 | ||
| 86 | /** | |
| 87 | * Associates the given value with the given key | |
| 88 | * | |
| 89 | * @param source | |
| 90 | * @param key | |
| 91 | * @param value | |
| 92 | * @throws IllegalArgumentException if the given key is null | |
| 93 | * @throws IllegalArgumentException if the given value is null | |
| 94 | * @throws DuplicateKeyException if a property with the given key already exists | |
| 95 | */ | |
| 96 | public void setProperty(PropertySource source, String key, String value) { | |
| 97 | 37 | validateKey(key); |
| 98 | 36 | validateValue(value); |
| 99 | ||
| 100 | 35 | if (containsKey(key)) { |
| 101 | 1 | if (source != null && source instanceof FilePropertySource && ((FilePropertySource)source).isAllowOverrides()) { |
| 102 | 0 | LOG.info("Duplicate Key: Override is enabled [key=" + key + ", new value=" + value + ", old value=" + this.heldProperties.getProperty(key) + "]"); |
| 103 | } else { | |
| 104 | 1 | throw new DuplicateKeyException("duplicate key '" + key + "'"); |
| 105 | } | |
| 106 | } | |
| 107 | 34 | this.heldProperties.setProperty(key, value); |
| 108 | 34 | } |
| 109 | ||
| 110 | /** | |
| 111 | * Removes the property with the given key from this container | |
| 112 | * | |
| 113 | * @param key | |
| 114 | * @throws IllegalArgumentException if the given key is null | |
| 115 | */ | |
| 116 | public void clearProperty(String key) { | |
| 117 | 3 | validateKey(key); |
| 118 | ||
| 119 | 2 | this.heldProperties.remove(key); |
| 120 | 2 | } |
| 121 | ||
| 122 | ||
| 123 | /** | |
| 124 | * Copies all name,value pairs from the given PropertySource instance into this container. | |
| 125 | * | |
| 126 | * @param source | |
| 127 | * @throws IllegalStateException if the source is invalid (improperly initialized) | |
| 128 | * @throws DuplicateKeyException the first time a given property has the same key as an existing property | |
| 129 | * @throws PropertiesException if unable to load properties from the given source | |
| 130 | */ | |
| 131 | public void loadProperties(PropertySource source) { | |
| 132 | 3 | if (source == null) { |
| 133 | 1 | throw new IllegalArgumentException("invalid (null) source"); |
| 134 | } | |
| 135 | ||
| 136 | 2 | Properties newProperties = source.loadProperties(); |
| 137 | ||
| 138 | 0 | for (Iterator i = newProperties.keySet().iterator(); i.hasNext();) { |
| 139 | 0 | String key = (String) i.next(); |
| 140 | 0 | setProperty(source, key, newProperties.getProperty(key)); |
| 141 | 0 | } |
| 142 | 0 | } |
| 143 | ||
| 144 | /** | |
| 145 | * Removes all properties from this container. | |
| 146 | */ | |
| 147 | public void clearProperties() { | |
| 148 | 2 | this.heldProperties.clear(); |
| 149 | 2 | } |
| 150 | ||
| 151 | ||
| 152 | /** | |
| 153 | * @return iterator over the keys of all properties in this container | |
| 154 | */ | |
| 155 | public Iterator getKeys() { | |
| 156 | 2 | return this.heldProperties.keySet().iterator(); |
| 157 | } | |
| 158 | ||
| 159 | ||
| 160 | /** | |
| 161 | * @param key | |
| 162 | * @throws IllegalArgumentException if the given key is null | |
| 163 | */ | |
| 164 | private void validateKey(String key) { | |
| 165 | 107 | if (key == null) { |
| 166 | 4 | throw new IllegalArgumentException("invalid (null) key"); |
| 167 | } | |
| 168 | 103 | } |
| 169 | ||
| 170 | /** | |
| 171 | * @param value | |
| 172 | * @throws IllegalArgumentException if the given value is null | |
| 173 | */ | |
| 174 | private void validateValue(String value) { | |
| 175 | 36 | if (value == null) { |
| 176 | 1 | throw new IllegalArgumentException("invalid (null) value"); |
| 177 | } | |
| 178 | 35 | } |
| 179 | ||
| 180 | ||
| 181 | public Properties getHeldProperties() { | |
| 182 | 0 | return heldProperties; |
| 183 | } | |
| 184 | ||
| 185 | ||
| 186 | public void setHeldProperties(Properties heldProperties) { | |
| 187 | 0 | this.heldProperties = heldProperties; |
| 188 | 0 | } |
| 189 | } |