Coverage Report - org.kuali.rice.core.config.SimpleNodeSettingsStore
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleNodeSettingsStore
0%
0/62
0%
0/18
2.778
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 17  
 package org.kuali.rice.core.config;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.FileOutputStream;
 22  
 import java.io.IOException;
 23  
 import java.util.Collections;
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 import java.util.Properties;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.apache.log4j.Logger;
 30  
 import org.springframework.beans.factory.InitializingBean;
 31  
 
 32  
 /**
 33  
  * A simple node settings store that backs the settings with a properties file
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class SimpleNodeSettingsStore implements NodeSettings, InitializingBean {
 37  
 
 38  0
     private static final Logger LOG = Logger.getLogger(SimpleNodeSettingsStore.class);
 39  
 
 40  
     private boolean enabled;
 41  
     private String propertiesPath;
 42  
     private Properties properties;
 43  
 
 44  
     public void afterPropertiesSet() throws Exception {
 45  0
         this.enabled = false;
 46  0
             if (StringUtils.isEmpty(this.propertiesPath)) {
 47  0
                 this.propertiesPath = ConfigContext.getCurrentContextConfig().getProperty(Config.NODE_PROPERTIES_PATH);
 48  
             }
 49  
             // if it's still empty, then node settings are not available
 50  0
             if (StringUtils.isEmpty(this.propertiesPath)) {
 51  0
                     LOG.warn("No node-level settings are available, the NodeSettingsStore will be disabled.");
 52  0
                     this.properties = new Properties();
 53  
             } else {
 54  0
                 this.properties = load();
 55  
             }
 56  0
     }
 57  
 
 58  
     protected Properties load() throws IOException {
 59  0
             Properties p = new Properties();
 60  0
             File file = new File(this.propertiesPath);
 61  
             try {
 62  0
                     if (!file.exists()) {
 63  0
                             LOG.warn("Properties path '" + this.propertiesPath + "' does not exist, attempting to create it.");
 64  0
                             if (!file.getParentFile().exists()) {
 65  0
                                     file.getParentFile().mkdirs();
 66  
                             }
 67  0
                             file.createNewFile();
 68  
                     }
 69  0
             FileInputStream fis = new FileInputStream(this.propertiesPath);
 70  
             try {
 71  0
                 p.load(fis);
 72  0
                 this.enabled = true;
 73  0
             } finally {
 74  0
                 fis.close();
 75  0
             }
 76  0
         } catch (IOException e) {
 77  0
             LOG.warn("Properties path '" + this.propertiesPath + "' does not exist despite efforts to create it at: " + file.getAbsolutePath(), e);
 78  0
         }
 79  0
         return p;
 80  
     }
 81  
 
 82  
     public void setPropertiesPath(String path) {
 83  0
         this.propertiesPath = path;
 84  0
     }
 85  
 
 86  
     protected synchronized void store(Properties p) {
 87  
         try {
 88  0
                 FileOutputStream fos = new FileOutputStream(this.propertiesPath);
 89  
                 try {
 90  0
                         p.store(fos, null);
 91  0
                 } finally {
 92  0
                         fos.close();
 93  0
                 }
 94  0
         } catch (IOException e) {
 95  0
                 throw new ConfigurationException("Failed to persist node-specific settings.", e);
 96  0
         }
 97  0
     }
 98  
 
 99  
     public synchronized String getSetting(String key) {
 100  0
             if (!isEnabled()) {
 101  0
                     LOG.warn("Node settings are not enabled, getSetting('"+key+"') is returning null.");
 102  0
                     return null;
 103  
             }
 104  0
         return this.properties.getProperty(key);
 105  
     }
 106  
 
 107  
     public synchronized void setSetting(String key, String value) {
 108  0
             if (!isEnabled()) {
 109  0
                     LOG.warn("Node settings are not enabled, setSetting('"+key+"', '"+value+"') will have no effect.");
 110  0
                     return;
 111  
             }
 112  0
             this.properties.put(key, value);
 113  0
         store(this.properties);
 114  0
     }
 115  
 
 116  
     public synchronized String removeSetting(String key) {
 117  0
             if (!isEnabled()) {
 118  0
                     LOG.warn("Node settings are not enabled, removeSetting('"+key+"') will have no effect.");
 119  0
                     return null;
 120  
             }
 121  0
         String property = (String)this.properties.remove(key);
 122  0
         store(this.properties);
 123  0
         return property;
 124  
     }
 125  
 
 126  
     public synchronized Map<String, String> getSettings() {
 127  0
             if (!isEnabled()) {
 128  0
                     LOG.warn("Node settings are not enabled, returning empty map for getSettings().");
 129  0
                     return Collections.emptyMap();
 130  
             }
 131  0
             Map<String, String> settings = new HashMap<String, String>();
 132  0
             for (Object key : this.properties.keySet()) {
 133  0
                     settings.put((String)key, (String)this.properties.get(key));
 134  
             }
 135  0
         return Collections.unmodifiableMap(settings);
 136  
     }
 137  
 
 138  
     public synchronized boolean isEnabled() {
 139  0
             return this.enabled;
 140  
     }
 141  
 
 142  
 }