View Javadoc

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