Coverage Report - org.apache.torque.util.BeanPropertiesLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertiesLoader
0%
0/82
0%
0/18
2.1
 
 1  
 package org.apache.torque.util;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.InputStreamReader;
 6  
 import java.io.Reader;
 7  
 import java.util.Map;
 8  
 import java.util.Properties;
 9  
 import java.util.Set;
 10  
 
 11  
 import org.apache.commons.beanutils.BeanUtils;
 12  
 import org.apache.commons.lang.StringUtils;
 13  
 import org.apache.commons.logging.Log;
 14  
 import org.apache.commons.logging.LogFactory;
 15  
 import org.kuali.core.db.torque.PropertyHandlingException;
 16  
 import org.kuali.core.db.torque.Utils;
 17  
 import org.springframework.core.io.DefaultResourceLoader;
 18  
 import org.springframework.core.io.Resource;
 19  
 import org.springframework.core.io.ResourceLoader;
 20  
 
 21  
 public class BeanPropertiesLoader {
 22  0
         private static final Log log = LogFactory.getLog(BeanPropertiesLoader.class);
 23  
 
 24  0
         Utils utils = new Utils();
 25  
         String location;
 26  
         String encoding;
 27  
         Object bean;
 28  0
         boolean overrideExistingPropertyValues = true;
 29  0
         boolean overrideSystemProperties = false;
 30  
         String description;
 31  
 
 32  
         public BeanPropertiesLoader() {
 33  0
                 this(null, null, null, null);
 34  0
         }
 35  
 
 36  
         public BeanPropertiesLoader(Object bean, String location, String encoding, String description) {
 37  0
                 super();
 38  0
                 this.bean = bean;
 39  0
                 this.location = location;
 40  0
                 this.encoding = encoding;
 41  0
                 this.description = description;
 42  0
         }
 43  
 
 44  
         public boolean isPropertiesExist() {
 45  0
                 return utils.isFileOrResource(location);
 46  
         }
 47  
 
 48  
         protected boolean isSkip(Map<String, Object> description, String key) {
 49  0
                 Object value = description.get(key);
 50  0
                 if (value != null && !isOverrideExistingPropertyValues()) {
 51  
                         // The property is already set, don't override it unless they have asked us to
 52  0
                         log.debug("Skipping property " + key + " it is already set to " + value);
 53  0
                         return true;
 54  
                 }
 55  0
                 Set<String> beanProperties = description.keySet();
 56  0
                 if (!beanProperties.contains(key)) {
 57  
                         // This is not a property of the bean
 58  0
                         log.debug("Skipping property " + key + " as it is not a property of this bean");
 59  0
                         return true;
 60  
                 }
 61  0
                 return false;
 62  
         }
 63  
 
 64  
         @SuppressWarnings("unchecked")
 65  
         public void loadToBean() throws PropertyHandlingException {
 66  0
                 if (!utils.isFileOrResource(location)) {
 67  0
                         log.info("------------------------------------------------------------------------");
 68  0
                         log.warn("No properties file located at " + location);
 69  0
                         log.info("------------------------------------------------------------------------");
 70  0
                         return;
 71  
                 } else {
 72  0
                         log.info("------------------------------------------------------------------------");
 73  0
                         log.info("Loading " + getDescription() + " properties from " + location);
 74  0
                         log.info("------------------------------------------------------------------------");
 75  
                 }
 76  
                 try {
 77  0
                         Properties properties = getProperties();
 78  0
                         if (!overrideSystemProperties) {
 79  0
                                 properties.putAll(System.getProperties());
 80  
                         }
 81  0
                         Set<String> keys = properties.stringPropertyNames();
 82  0
                         Map<String, Object> description = BeanUtils.describe(bean);
 83  0
                         for (String key : keys) {
 84  0
                                 if (isSkip(description, key)) {
 85  0
                                         continue;
 86  
                                 }
 87  
                                 // Extract the value and set it on the bean
 88  0
                                 String newValue = properties.getProperty(key);
 89  0
                                 log.info("Setting " + key + "=" + getLogValue(key, newValue));
 90  0
                                 BeanUtils.copyProperty(bean, key, newValue);
 91  0
                         }
 92  0
                 } catch (Exception e) {
 93  0
                         throw new PropertyHandlingException(e);
 94  0
                 }
 95  0
         }
 96  
 
 97  
         /**
 98  
          * Don't display password'ish type properties
 99  
          */
 100  
         protected String getLogValue(String key, String value) {
 101  0
                 int pos = key.toLowerCase().indexOf("password");
 102  0
                 if (pos == -1) {
 103  0
                         return value;
 104  
                 } else {
 105  0
                         return StringUtils.repeat("*", value.length());
 106  
                 }
 107  
         }
 108  
 
 109  
         /**
 110  
          * Load the properties file into a Properties object
 111  
          */
 112  
         public Properties getProperties() throws PropertyHandlingException {
 113  
                 try {
 114  0
                         Reader reader = getReader();
 115  0
                         Properties properties = new Properties();
 116  0
                         properties.load(reader);
 117  0
                         return properties;
 118  0
                 } catch (Exception e) {
 119  0
                         throw new PropertyHandlingException(e);
 120  
                 }
 121  
         }
 122  
 
 123  
         /**
 124  
          * Return a Reader for reading in the properties file. First check the file system to see if the file exists. If
 125  
          * not, return a Reader using Spring Resource loading
 126  
          */
 127  
         protected Reader getReader() throws PropertyHandlingException {
 128  
                 try {
 129  0
                         File file = new File(location);
 130  0
                         if (file.exists()) {
 131  0
                                 return new InputStreamReader(new FileInputStream(file), getEncoding());
 132  
                         }
 133  0
                         ResourceLoader loader = new DefaultResourceLoader();
 134  0
                         Resource resource = loader.getResource(location);
 135  0
                         return new InputStreamReader(resource.getInputStream(), getEncoding());
 136  0
                 } catch (Exception e) {
 137  0
                         throw new PropertyHandlingException(e);
 138  
                 }
 139  
         }
 140  
 
 141  
         public String getLocation() {
 142  0
                 return location;
 143  
         }
 144  
 
 145  
         public void setLocation(String location) {
 146  0
                 this.location = location;
 147  0
         }
 148  
 
 149  
         public String getEncoding() {
 150  0
                 return encoding;
 151  
         }
 152  
 
 153  
         public void setEncoding(String encoding) {
 154  0
                 this.encoding = encoding;
 155  0
         }
 156  
 
 157  
         public Object getBean() {
 158  0
                 return bean;
 159  
         }
 160  
 
 161  
         public void setBean(Object bean) {
 162  0
                 this.bean = bean;
 163  0
         }
 164  
 
 165  
         public boolean isOverrideExistingPropertyValues() {
 166  0
                 return overrideExistingPropertyValues;
 167  
         }
 168  
 
 169  
         public void setOverrideExistingPropertyValues(boolean override) {
 170  0
                 this.overrideExistingPropertyValues = override;
 171  0
         }
 172  
 
 173  
         public String getDescription() {
 174  0
                 return description;
 175  
         }
 176  
 
 177  
         public void setDescription(String description) {
 178  0
                 this.description = description;
 179  0
         }
 180  
 
 181  
         public boolean isOverrideSystemProperties() {
 182  0
                 return overrideSystemProperties;
 183  
         }
 184  
 
 185  
         public void setOverrideSystemProperties(boolean overrideSystemProperties) {
 186  0
                 this.overrideSystemProperties = overrideSystemProperties;
 187  0
         }
 188  
 
 189  
 }