View Javadoc

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  	public BeanPropertiesLoader() {
33  		this(null, null, null, null);
34  	}
35  
36  	public BeanPropertiesLoader(Object bean, String location, String encoding, String description) {
37  		super();
38  		this.bean = bean;
39  		this.location = location;
40  		this.encoding = encoding;
41  		this.description = description;
42  	}
43  
44  	public boolean isPropertiesExist() {
45  		return utils.isFileOrResource(location);
46  	}
47  
48  	protected boolean isSkip(Map<String, Object> description, String key) {
49  		Object value = description.get(key);
50  		if (value != null && !isOverrideExistingPropertyValues()) {
51  			// The property is already set, don't override it unless they have asked us to
52  			log.debug("Skipping property " + key + " it is already set to " + value);
53  			return true;
54  		}
55  		Set<String> beanProperties = description.keySet();
56  		if (!beanProperties.contains(key)) {
57  			// This is not a property of the bean
58  			log.debug("Skipping property " + key + " as it is not a property of this bean");
59  			return true;
60  		}
61  		return false;
62  	}
63  
64  	@SuppressWarnings("unchecked")
65  	public void loadToBean() throws PropertyHandlingException {
66  		if (!utils.isFileOrResource(location)) {
67  			log.info("------------------------------------------------------------------------");
68  			log.warn("No properties file located at " + location);
69  			log.info("------------------------------------------------------------------------");
70  			return;
71  		} else {
72  			log.info("------------------------------------------------------------------------");
73  			log.info("Loading " + getDescription() + " properties from " + location);
74  			log.info("------------------------------------------------------------------------");
75  		}
76  		try {
77  			Properties properties = getProperties();
78  			if (!overrideSystemProperties) {
79  				properties.putAll(System.getProperties());
80  			}
81  			Set<String> keys = properties.stringPropertyNames();
82  			Map<String, Object> description = BeanUtils.describe(bean);
83  			for (String key : keys) {
84  				if (isSkip(description, key)) {
85  					continue;
86  				}
87  				// Extract the value and set it on the bean
88  				String newValue = properties.getProperty(key);
89  				log.info("Setting " + key + "=" + getLogValue(key, newValue));
90  				BeanUtils.copyProperty(bean, key, newValue);
91  			}
92  		} catch (Exception e) {
93  			throw new PropertyHandlingException(e);
94  		}
95  	}
96  
97  	/**
98  	 * Don't display password'ish type properties
99  	 */
100 	protected String getLogValue(String key, String value) {
101 		int pos = key.toLowerCase().indexOf("password");
102 		if (pos == -1) {
103 			return value;
104 		} else {
105 			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 			Reader reader = getReader();
115 			Properties properties = new Properties();
116 			properties.load(reader);
117 			return properties;
118 		} catch (Exception e) {
119 			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 			File file = new File(location);
130 			if (file.exists()) {
131 				return new InputStreamReader(new FileInputStream(file), getEncoding());
132 			}
133 			ResourceLoader loader = new DefaultResourceLoader();
134 			Resource resource = loader.getResource(location);
135 			return new InputStreamReader(resource.getInputStream(), getEncoding());
136 		} catch (Exception e) {
137 			throw new PropertyHandlingException(e);
138 		}
139 	}
140 
141 	public String getLocation() {
142 		return location;
143 	}
144 
145 	public void setLocation(String location) {
146 		this.location = location;
147 	}
148 
149 	public String getEncoding() {
150 		return encoding;
151 	}
152 
153 	public void setEncoding(String encoding) {
154 		this.encoding = encoding;
155 	}
156 
157 	public Object getBean() {
158 		return bean;
159 	}
160 
161 	public void setBean(Object bean) {
162 		this.bean = bean;
163 	}
164 
165 	public boolean isOverrideExistingPropertyValues() {
166 		return overrideExistingPropertyValues;
167 	}
168 
169 	public void setOverrideExistingPropertyValues(boolean override) {
170 		this.overrideExistingPropertyValues = override;
171 	}
172 
173 	public String getDescription() {
174 		return description;
175 	}
176 
177 	public void setDescription(String description) {
178 		this.description = description;
179 	}
180 
181 	public boolean isOverrideSystemProperties() {
182 		return overrideSystemProperties;
183 	}
184 
185 	public void setOverrideSystemProperties(boolean overrideSystemProperties) {
186 		this.overrideSystemProperties = overrideSystemProperties;
187 	}
188 
189 }