Coverage Report - org.kuali.rice.krad.service.impl.ConfigurationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationServiceImpl
0%
0/18
0%
0/6
2.083
ConfigurationServiceImpl$FilePropertySource
46%
12/26
50%
3/6
2.083
ConfigurationServiceImpl$PropertyHolder
78%
33/42
56%
9/16
2.083
ConfigurationServiceImpl$PropertySource
N/A
N/A
2.083
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.krad.service.impl;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 24  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 25  
 import org.kuali.rice.core.api.util.Truth;
 26  
 import org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResources;
 27  
 import org.kuali.rice.kns.web.struts.action.KualiPropertyMessageResourcesFactory;
 28  
 import org.kuali.rice.krad.exception.DuplicateKeyException;
 29  
 import org.kuali.rice.krad.exception.PropertiesException;
 30  
 
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.net.URL;
 34  
 import java.util.Collections;
 35  
 import java.util.Iterator;
 36  
 import java.util.Map;
 37  
 import java.util.Properties;
 38  
 
 39  
 public class ConfigurationServiceImpl implements ConfigurationService {
 40  0
     private final PropertyHolder propertyHolder = new PropertyHolder();
 41  
 
 42  
     /**
 43  
      * Harcoding the configFileName, by request.
 44  
      */
 45  0
     public ConfigurationServiceImpl() {
 46  0
         this.propertyHolder.getHeldProperties().putAll(ConfigContext.getCurrentContextConfig().getProperties());
 47  
 
 48  0
         KualiPropertyMessageResourcesFactory propertyMessageFactory = new KualiPropertyMessageResourcesFactory();
 49  
 
 50  
         // create default KualiPropertyMessageResources
 51  0
         KualiPropertyMessageResources messageResources =
 52  
                 (KualiPropertyMessageResources) propertyMessageFactory.createResources("");
 53  
 
 54  
         //Add Kuali Properties to property holder
 55  0
         this.propertyHolder.getHeldProperties().putAll(messageResources.getKualiProperties(null));
 56  0
     }
 57  
 
 58  
     /**
 59  
      * @see org.kuali.rice.core.api.config.property.ConfigurationService#getPropertyValueAsString(java.lang.String)
 60  
      */
 61  
     @Override
 62  
     public String getPropertyValueAsString(String key) {
 63  0
         if (key == null) {
 64  0
             throw new IllegalArgumentException("invalid (null) key");
 65  
         }
 66  
 
 67  0
         return this.propertyHolder.getProperty(key);
 68  
     }
 69  
 
 70  
     /**
 71  
      * @see org.kuali.rice.core.api.config.property.ConfigurationService#getPropertyValueAsBoolean(java.lang.String)
 72  
      */
 73  
     @Override
 74  
     public boolean getPropertyValueAsBoolean(String key) {
 75  0
         if (key == null) {
 76  0
             throw new IllegalArgumentException("invalid (null) key");
 77  
         }
 78  0
         String property = this.propertyHolder.getProperty(key);
 79  0
         Boolean b = Truth.strToBooleanIgnoreCase(property);
 80  0
         if (b == null) {
 81  0
             return false;
 82  
         }
 83  0
         return b;
 84  
     }
 85  
 
 86  
     /**
 87  
      * @see org.kuali.rice.core.api.config.property.ConfigurationService#getAllProperties()
 88  
      */
 89  
     @Override
 90  
     public Map<String, String> getAllProperties() {
 91  0
         return (Map) Collections.unmodifiableMap(propertyHolder.getHeldProperties());
 92  
     }
 93  
 
 94  
     /**
 95  
      * This is an interface for a source for properties
 96  
      *
 97  
      *
 98  
      */
 99  
     static interface PropertySource {
 100  
         /**
 101  
          * @return Properties loaded from this PropertySource
 102  
          * @throws org.kuali.rice.krad.exception.PropertiesException if there's a problem loading the properties
 103  
          */
 104  
         public Properties loadProperties();
 105  
     }
 106  
 
 107  
     /**
 108  
      * This class is a Property container. It is able to load properties from various property-sources.
 109  
      *
 110  
      *
 111  
      */
 112  
     static class PropertyHolder {
 113  1
         private static Logger LOG = Logger.getLogger(PropertyHolder.class);
 114  
 
 115  
         Properties heldProperties;
 116  
 
 117  
         /**
 118  
          * Default constructor.
 119  
          */
 120  24
         public PropertyHolder() {
 121  24
             this.heldProperties = new Properties();
 122  24
         }
 123  
 
 124  
 
 125  
         /**
 126  
          * @return true if this container currently has no properties
 127  
          */
 128  
         public boolean isEmpty() {
 129  8
             return this.heldProperties.isEmpty();
 130  
         }
 131  
 
 132  
         /**
 133  
          * @param key
 134  
          * @return true if a property with the given key exists in this container
 135  
          * @throws IllegalArgumentException if the given key is null
 136  
          */
 137  
         public boolean containsKey(String key) {
 138  56
             validateKey(key);
 139  
 
 140  55
             return this.heldProperties.containsKey(key);
 141  
         }
 142  
 
 143  
         /**
 144  
          * @param key
 145  
          * @return the current value of the property with the given key, or null if no property exists with that key
 146  
          * @throws IllegalArgumentException if the given key is null
 147  
          */
 148  
         public String getProperty(String key) {
 149  11
             validateKey(key);
 150  
 
 151  10
             return this.heldProperties.getProperty(key);
 152  
         }
 153  
 
 154  
 
 155  
         /**
 156  
          * Associates the given value with the given key
 157  
          *
 158  
          * @param key
 159  
          * @param value
 160  
          * @throws IllegalArgumentException if the given key is null
 161  
          * @throws IllegalArgumentException if the given value is null
 162  
          * @throws org.kuali.rice.krad.exception.DuplicateKeyException if a property with the given key already exists
 163  
          */
 164  
         public void setProperty(String key, String value) {
 165  37
         setProperty(null, key, value);
 166  34
         }
 167  
 
 168  
         /**
 169  
          * Associates the given value with the given key
 170  
          *
 171  
          * @param source
 172  
          * @param key
 173  
          * @param value
 174  
          * @throws IllegalArgumentException if the given key is null
 175  
          * @throws IllegalArgumentException if the given value is null
 176  
          * @throws org.kuali.rice.krad.exception.DuplicateKeyException if a property with the given key already exists
 177  
          */
 178  
         public void setProperty(PropertySource source, String key, String value) {
 179  37
             validateKey(key);
 180  36
             validateValue(value);
 181  
 
 182  35
             if (containsKey(key)) {
 183  1
                 if (source != null && source instanceof FilePropertySource && ((FilePropertySource)source).isAllowOverrides()) {
 184  0
                     LOG.info("Duplicate Key: Override is enabled [key=" + key + ", new value=" + value + ", old value=" + this.heldProperties.getProperty(key) + "]");
 185  
                 } else {
 186  1
                     throw new DuplicateKeyException("duplicate key '" + key + "'");
 187  
                 }
 188  
             }
 189  34
             this.heldProperties.setProperty(key, value);
 190  34
         }
 191  
 
 192  
         /**
 193  
          * Removes the property with the given key from this container
 194  
          *
 195  
          * @param key
 196  
          * @throws IllegalArgumentException if the given key is null
 197  
          */
 198  
         public void clearProperty(String key) {
 199  3
             validateKey(key);
 200  
 
 201  2
             this.heldProperties.remove(key);
 202  2
         }
 203  
 
 204  
 
 205  
         /**
 206  
          * Copies all name,value pairs from the given PropertySource instance into this container.
 207  
          *
 208  
          * @param source
 209  
          * @throws IllegalStateException if the source is invalid (improperly initialized)
 210  
          * @throws org.kuali.rice.krad.exception.DuplicateKeyException the first time a given property has the same key as an existing property
 211  
          * @throws org.kuali.rice.krad.exception.PropertiesException if unable to load properties from the given source
 212  
          */
 213  
         public void loadProperties(PropertySource source) {
 214  3
             if (source == null) {
 215  1
                 throw new IllegalArgumentException("invalid (null) source");
 216  
             }
 217  
 
 218  2
             Properties newProperties = source.loadProperties();
 219  
 
 220  0
             for (Iterator i = newProperties.keySet().iterator(); i.hasNext();) {
 221  0
                 String key = (String) i.next();
 222  0
                 setProperty(source, key, newProperties.getProperty(key));
 223  0
             }
 224  0
         }
 225  
 
 226  
         /**
 227  
          * Removes all properties from this container.
 228  
          */
 229  
         public void clearProperties() {
 230  2
             this.heldProperties.clear();
 231  2
         }
 232  
 
 233  
 
 234  
         /**
 235  
          * @return iterator over the keys of all properties in this container
 236  
          */
 237  
         public Iterator getKeys() {
 238  2
             return this.heldProperties.keySet().iterator();
 239  
         }
 240  
 
 241  
 
 242  
         /**
 243  
          * @param key
 244  
          * @throws IllegalArgumentException if the given key is null
 245  
          */
 246  
         private void validateKey(String key) {
 247  107
             if (key == null) {
 248  4
                 throw new IllegalArgumentException("invalid (null) key");
 249  
             }
 250  103
         }
 251  
 
 252  
         /**
 253  
          * @param value
 254  
          * @throws IllegalArgumentException if the given value is null
 255  
          */
 256  
         private void validateValue(String value) {
 257  36
             if (value == null) {
 258  1
                 throw new IllegalArgumentException("invalid (null) value");
 259  
             }
 260  35
         }
 261  
 
 262  
 
 263  
         public Properties getHeldProperties() {
 264  0
             return heldProperties;
 265  
         }
 266  
 
 267  
 
 268  
         public void setHeldProperties(Properties heldProperties) {
 269  0
             this.heldProperties = heldProperties;
 270  0
         }
 271  
     }
 272  
 
 273  
     /**
 274  
      * This class is used to obtain properties from a properites file.
 275  
      *
 276  
      *
 277  
      */
 278  6
     static class FilePropertySource implements PropertySource {
 279  1
         private static Log log = LogFactory.getLog(FilePropertySource.class);
 280  
 
 281  
 
 282  
         private String fileName;
 283  
         private boolean allowOverrides;
 284  
 
 285  
         /**
 286  
          * Set source fileName.
 287  
          *
 288  
          * @param fileName
 289  
          */
 290  
         public void setFileName(String fileName) {
 291  4
             this.fileName = fileName;
 292  4
         }
 293  
 
 294  
         /**
 295  
          * @return source fileName
 296  
          */
 297  
         public String getFileName() {
 298  12
             return this.fileName;
 299  
         }
 300  
 
 301  
         public boolean isAllowOverrides() {
 302  0
             return this.allowOverrides;
 303  
         }
 304  
 
 305  
         public void setAllowOverrides(boolean allowOverrides) {
 306  0
             this.allowOverrides = allowOverrides;
 307  0
         }
 308  
 
 309  
         /**
 310  
          * Attempts to load properties from a properties file which has the current fileName and is located on the classpath.
 311  
          *
 312  
          * @see org.kuali.rice.krad.service.impl.ConfigurationServiceImpl.PropertySource#loadProperties()
 313  
          * @throws IllegalStateException if the fileName is null or empty
 314  
          */
 315  
         public Properties loadProperties() {
 316  6
             if (StringUtils.isBlank(getFileName())) {
 317  3
                 throw new IllegalStateException("invalid (blank) fileName");
 318  
             }
 319  
 
 320  3
             Properties properties = new Properties();
 321  
 
 322  3
             ClassLoader loader = Thread.currentThread().getContextClassLoader();
 323  3
             URL url = loader.getResource(getFileName());
 324  3
             if (url == null) {
 325  3
                 throw new PropertiesException("unable to locate properties file '" + getFileName() + "'");
 326  
             }
 327  
 
 328  0
             InputStream in = null;
 329  
 
 330  
             try {
 331  0
                 in = url.openStream();
 332  0
                 properties.load(in);
 333  
             }
 334  0
             catch (IOException e) {
 335  0
                 throw new PropertiesException("error loading from properties file '" + getFileName() + "'", e);
 336  
             }
 337  
             finally {
 338  0
                 if (in != null) {
 339  
                     try {
 340  0
                         in.close();
 341  
                     }
 342  0
                     catch (IOException e) {
 343  0
                         log.error("caught exception closing InputStream: " + e);
 344  0
                     }
 345  
 
 346  
                 }
 347  
             }
 348  
 
 349  0
             return properties;
 350  
         }
 351  
 
 352  
     }
 353  
 }