Clover Coverage Report - Impex Parent 1.0.21-SNAPSHOT (Aggregated)
Coverage timestamp: Tue Feb 8 2011 11:33:53 EST
../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
67   189   31   3.35
14   151   0.46   20
20     1.55  
1    
 
  BeanPropertiesLoader       Line # 21 67 0% 31 101 0% 0.0
 
No Tests
 
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    private static final Log log = LogFactory.getLog(BeanPropertiesLoader.class);
23   
24    Utils utils = new Utils();
25    String location;
26    String encoding;
27    Object bean;
28    boolean overrideExistingPropertyValues = true;
29    boolean overrideSystemProperties = false;
30    String description;
31   
 
32  0 toggle public BeanPropertiesLoader() {
33  0 this(null, null, null, null);
34    }
35   
 
36  0 toggle 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    }
43   
 
44  0 toggle public boolean isPropertiesExist() {
45  0 return utils.isFileOrResource(location);
46    }
47   
 
48  0 toggle 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  0 toggle @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  0 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    }
92    } catch (Exception e) {
93  0 throw new PropertyHandlingException(e);
94    }
95    }
96   
97    /**
98    * Don't display password'ish type properties
99    */
 
100  0 toggle 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  0 toggle public Properties getProperties() throws PropertyHandlingException {
113  0 try {
114  0 Reader reader = getReader();
115  0 Properties properties = new Properties();
116  0 properties.load(reader);
117  0 return properties;
118    } 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  0 toggle protected Reader getReader() throws PropertyHandlingException {
128  0 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    } catch (Exception e) {
137  0 throw new PropertyHandlingException(e);
138    }
139    }
140   
 
141  0 toggle public String getLocation() {
142  0 return location;
143    }
144   
 
145  0 toggle public void setLocation(String location) {
146  0 this.location = location;
147    }
148   
 
149  0 toggle public String getEncoding() {
150  0 return encoding;
151    }
152   
 
153  0 toggle public void setEncoding(String encoding) {
154  0 this.encoding = encoding;
155    }
156   
 
157  0 toggle public Object getBean() {
158  0 return bean;
159    }
160   
 
161  0 toggle public void setBean(Object bean) {
162  0 this.bean = bean;
163    }
164   
 
165  0 toggle public boolean isOverrideExistingPropertyValues() {
166  0 return overrideExistingPropertyValues;
167    }
168   
 
169  0 toggle public void setOverrideExistingPropertyValues(boolean override) {
170  0 this.overrideExistingPropertyValues = override;
171    }
172   
 
173  0 toggle public String getDescription() {
174  0 return description;
175    }
176   
 
177  0 toggle public void setDescription(String description) {
178  0 this.description = description;
179    }
180   
 
181  0 toggle public boolean isOverrideSystemProperties() {
182  0 return overrideSystemProperties;
183    }
184   
 
185  0 toggle public void setOverrideSystemProperties(boolean overrideSystemProperties) {
186  0 this.overrideSystemProperties = overrideSystemProperties;
187    }
188   
189    }