| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 101 (101) |
Complexity: 31 |
Complexity Density: 0.46 |
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 32 |
0
|
public BeanPropertiesLoader() {... |
| 33 |
0
|
this(null, null, null, null); |
| 34 |
|
} |
| 35 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 36 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 44 |
0
|
public boolean isPropertiesExist() {... |
| 45 |
0
|
return utils.isFileOrResource(location); |
| 46 |
|
} |
| 47 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 48 |
0
|
protected boolean isSkip(Map<String, Object> description, String key) {... |
| 49 |
0
|
Object value = description.get(key); |
| 50 |
0
|
if (value != null && !isOverrideExistingPropertyValues()) { |
| 51 |
|
|
| 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 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 5 |
Complexity Density: 0.24 |
|
| 64 |
0
|
@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 |
|
|
| 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 |
|
|
| 99 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 100 |
0
|
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 |
|
|
| 111 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 112 |
0
|
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 |
|
|
| 125 |
|
|
| 126 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 127 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
0
|
public String getLocation() {... |
| 142 |
0
|
return location; |
| 143 |
|
} |
| 144 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 145 |
0
|
public void setLocation(String location) {... |
| 146 |
0
|
this.location = location; |
| 147 |
|
} |
| 148 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 149 |
0
|
public String getEncoding() {... |
| 150 |
0
|
return encoding; |
| 151 |
|
} |
| 152 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 153 |
0
|
public void setEncoding(String encoding) {... |
| 154 |
0
|
this.encoding = encoding; |
| 155 |
|
} |
| 156 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 157 |
0
|
public Object getBean() {... |
| 158 |
0
|
return bean; |
| 159 |
|
} |
| 160 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 161 |
0
|
public void setBean(Object bean) {... |
| 162 |
0
|
this.bean = bean; |
| 163 |
|
} |
| 164 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 165 |
0
|
public boolean isOverrideExistingPropertyValues() {... |
| 166 |
0
|
return overrideExistingPropertyValues; |
| 167 |
|
} |
| 168 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 169 |
0
|
public void setOverrideExistingPropertyValues(boolean override) {... |
| 170 |
0
|
this.overrideExistingPropertyValues = override; |
| 171 |
|
} |
| 172 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 173 |
0
|
public String getDescription() {... |
| 174 |
0
|
return description; |
| 175 |
|
} |
| 176 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 177 |
0
|
public void setDescription(String description) {... |
| 178 |
0
|
this.description = description; |
| 179 |
|
} |
| 180 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 181 |
0
|
public boolean isOverrideSystemProperties() {... |
| 182 |
0
|
return overrideSystemProperties; |
| 183 |
|
} |
| 184 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 185 |
0
|
public void setOverrideSystemProperties(boolean overrideSystemProperties) {... |
| 186 |
0
|
this.overrideSystemProperties = overrideSystemProperties; |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
} |