| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.util; |
| 17 | |
|
| 18 | |
import java.io.FileInputStream; |
| 19 | |
import java.io.IOException; |
| 20 | |
import java.io.InputStream; |
| 21 | |
import java.net.URL; |
| 22 | |
import java.util.HashSet; |
| 23 | |
import java.util.Properties; |
| 24 | |
import java.util.Set; |
| 25 | |
|
| 26 | |
import org.springframework.beans.factory.FactoryBean; |
| 27 | |
import org.springframework.core.io.ClassPathResource; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 1 | public class PropertiesFilterFactoryBean implements FactoryBean { |
| 52 | |
|
| 53 | |
private static final String CLASSPATH_PREFIX = "classpath:"; |
| 54 | |
|
| 55 | |
private static final String FILEURL_PREFIX = "file:"; |
| 56 | |
|
| 57 | |
private String propertyFile; |
| 58 | |
private String prefix; |
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public Object getObject() throws Exception { |
| 62 | |
|
| 63 | |
InputStream stream; |
| 64 | |
|
| 65 | 1 | if (propertyFile.startsWith(CLASSPATH_PREFIX)) { |
| 66 | 1 | ClassPathResource resource = new ClassPathResource(propertyFile |
| 67 | |
.substring(CLASSPATH_PREFIX.length())); |
| 68 | 1 | stream = resource.getInputStream(); |
| 69 | 1 | } else if(propertyFile.toLowerCase().startsWith(FILEURL_PREFIX)){ |
| 70 | 0 | URL url = new URL(propertyFile); |
| 71 | 0 | stream = url.openConnection().getInputStream(); |
| 72 | 0 | } else { |
| 73 | 0 | stream = new FileInputStream(propertyFile); |
| 74 | |
} |
| 75 | |
|
| 76 | 1 | Properties inProperties = new Properties(); |
| 77 | |
try { |
| 78 | 1 | inProperties.load(stream); |
| 79 | |
} finally { |
| 80 | 1 | if (stream != null) { |
| 81 | |
try { |
| 82 | 1 | stream.close(); |
| 83 | 0 | } catch (IOException e) { |
| 84 | 1 | } |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | 1 | Properties outProperties = new Properties(); |
| 89 | 1 | int prefixLength = prefix.length(); |
| 90 | 1 | for (String key : inProperties.stringPropertyNames()) { |
| 91 | 8 | if (key.startsWith(prefix)) { |
| 92 | 4 | String stringValue = resolvePlaceholder(key, inProperties); |
| 93 | 4 | String resolvedProperty = this.parseStringValue(stringValue, inProperties, new HashSet<String>()); |
| 94 | 4 | outProperties.setProperty(key.substring(prefixLength), |
| 95 | |
resolvedProperty); |
| 96 | 8 | } |
| 97 | |
} |
| 98 | |
|
| 99 | 1 | return outProperties; |
| 100 | |
} |
| 101 | |
|
| 102 | |
@Override |
| 103 | |
public Class<?> getObjectType() { |
| 104 | 0 | return Properties.class; |
| 105 | |
} |
| 106 | |
|
| 107 | |
@Override |
| 108 | |
public boolean isSingleton() { |
| 109 | 0 | return false; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public String getPropertyFile() { |
| 113 | 0 | return propertyFile; |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public void setPropertyFile(String propertyFile) { |
| 122 | 1 | this.propertyFile = propertyFile; |
| 123 | 1 | } |
| 124 | |
|
| 125 | |
public String getPrefix() { |
| 126 | 0 | return prefix; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public void setPrefix(String prefix) { |
| 134 | 1 | this.prefix = prefix + "."; |
| 135 | 1 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
protected String parseStringValue(String strVal, Properties props, |
| 147 | |
Set<String> visitedPlaceholders) { |
| 148 | |
|
| 149 | 13 | if(strVal!=null&&strVal.indexOf("${")>-1){ |
| 150 | |
|
| 151 | 9 | String begin = strVal.substring(0, strVal.indexOf("${")); |
| 152 | 9 | String resolveString = strVal.substring(strVal.indexOf("${")+2,strVal.indexOf("}")); |
| 153 | 9 | String end = strVal.substring(strVal.indexOf("}")+1); |
| 154 | 9 | if(!visitedPlaceholders.add(resolveString)){ |
| 155 | 0 | return ""; |
| 156 | |
} |
| 157 | 9 | String propVal = resolvePlaceholder(resolveString, props); |
| 158 | 9 | String parsedString = parseStringValue(begin+propVal+end, props, |
| 159 | |
visitedPlaceholders); |
| 160 | 9 | visitedPlaceholders.add(resolveString); |
| 161 | 9 | return parsedString; |
| 162 | |
} |
| 163 | 4 | return strVal; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
protected String resolvePlaceholder(String key, Properties props) { |
| 179 | |
try { |
| 180 | 13 | String value = props.getProperty(key); |
| 181 | 13 | if (value == null) { |
| 182 | 3 | value = System.getProperty(key); |
| 183 | |
} |
| 184 | 13 | if (value == null) { |
| 185 | 3 | value = System.getenv(key); |
| 186 | |
} |
| 187 | 13 | return value; |
| 188 | 0 | } catch (Throwable ex) { |
| 189 | 0 | return null; |
| 190 | |
} |
| 191 | |
} |
| 192 | |
|
| 193 | |
} |