1 | |
package org.kuali.spring.util; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Collections; |
5 | |
import java.util.List; |
6 | |
import java.util.Properties; |
7 | |
import java.util.regex.Matcher; |
8 | |
import java.util.regex.Pattern; |
9 | |
|
10 | |
import org.slf4j.Logger; |
11 | |
import org.slf4j.LoggerFactory; |
12 | |
|
13 | |
public class PropertyLogger { |
14 | 26 | final Logger logger = LoggerFactory.getLogger(PropertyLogger.class); |
15 | |
|
16 | |
|
17 | |
public static final String DEFAULT_MASK_EXPRESSION = ".*((?i)password).*"; |
18 | |
public static final String DEFAULT_MASKED_VALUE = "******"; |
19 | |
public static final boolean DEFAULT_IS_MASK_PROPERTY_VALUES = true; |
20 | |
public static final boolean DEFAULT_IS_FLATTEN_PROPERTY_VALUES = false; |
21 | |
public static final boolean DEFAULT_IS_TRIM_PROPERTY_VALUES = false; |
22 | |
public static final boolean DEFAULT_IS_LOG_IN_SORTED_ORDER = true; |
23 | |
|
24 | |
public static final String DEFAULT_REPLACEMENT_STRING = " "; |
25 | |
|
26 | |
public static final String CR = "\r"; |
27 | |
|
28 | |
public static final String LF = "\n"; |
29 | |
|
30 | |
|
31 | 26 | boolean logInSortedOrder = DEFAULT_IS_LOG_IN_SORTED_ORDER; |
32 | |
|
33 | 26 | boolean flattenPropertyValues = DEFAULT_IS_FLATTEN_PROPERTY_VALUES; |
34 | |
|
35 | 26 | String linefeedReplacement = DEFAULT_REPLACEMENT_STRING; |
36 | |
|
37 | 26 | String carriageReturnReplacement = DEFAULT_REPLACEMENT_STRING; |
38 | |
|
39 | 26 | boolean trimPropertyValues = DEFAULT_IS_TRIM_PROPERTY_VALUES; |
40 | |
|
41 | 26 | boolean maskPropertyValues = DEFAULT_IS_MASK_PROPERTY_VALUES; |
42 | |
|
43 | |
String[] maskExpressions; |
44 | |
|
45 | 26 | String maskValue = DEFAULT_MASKED_VALUE; |
46 | |
|
47 | |
Pattern[] patterns; |
48 | |
|
49 | |
public PropertyLogger() { |
50 | 26 | super(); |
51 | 26 | setMaskExpression(DEFAULT_MASK_EXPRESSION); |
52 | 26 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public void setMaskExpression(String maskExpression) { |
60 | 26 | setMaskExpressions(new String[] { maskExpression }); |
61 | 26 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public void setMaskExpressions(String[] maskExpressions) { |
69 | 29 | this.maskExpressions = maskExpressions; |
70 | 29 | this.patterns = new Pattern[maskExpressions.length]; |
71 | 58 | for (int i = 0; i < patterns.length; i++) { |
72 | 29 | patterns[i] = Pattern.compile(maskExpressions[i]); |
73 | |
} |
74 | 29 | } |
75 | |
|
76 | |
public String getLogEntry(String key, String value) { |
77 | 0 | return key + "=" + getLogValue(key, value); |
78 | |
} |
79 | |
|
80 | |
protected boolean isEmpty(String s) { |
81 | 0 | if (s == null) { |
82 | 0 | return true; |
83 | |
} |
84 | 0 | if (s.trim().length() == 0) { |
85 | 0 | return true; |
86 | |
} |
87 | 0 | return false; |
88 | |
} |
89 | |
|
90 | |
public String getLogEntry(Properties properties) { |
91 | 0 | if (properties == null || properties.size() == 0) { |
92 | 0 | return "No properties to log"; |
93 | |
} |
94 | 0 | StringBuilder sb = new StringBuilder(); |
95 | 0 | List<String> keys = new ArrayList<String>(properties.stringPropertyNames()); |
96 | 0 | if (isLogInSortedOrder()) { |
97 | 0 | Collections.sort(keys); |
98 | |
} |
99 | 0 | for (String key : keys) { |
100 | 0 | String value = properties.getProperty(key); |
101 | 0 | sb.append(getLogEntry(key, value) + "\n"); |
102 | 0 | } |
103 | 0 | return sb.toString(); |
104 | |
} |
105 | |
|
106 | |
protected boolean isMatch(Pattern[] patterns, String key) { |
107 | 541 | for (Pattern pattern : patterns) { |
108 | 272 | Matcher matcher = pattern.matcher(key); |
109 | 272 | if (matcher.matches()) { |
110 | 3 | return true; |
111 | |
} |
112 | |
} |
113 | 269 | return false; |
114 | |
} |
115 | |
|
116 | |
public String getLogValue(String key, String value) { |
117 | 272 | if (isFlattenPropertyValues()) { |
118 | 0 | value = value.replace(LF, getLinefeedReplacement()); |
119 | 0 | value = value.replace(CR, getCarriageReturnReplacement()); |
120 | |
} |
121 | 272 | if (isTrimPropertyValues()) { |
122 | 0 | value = value.trim(); |
123 | |
} |
124 | 272 | if (!isMaskPropertyValues()) { |
125 | 0 | return value; |
126 | |
} |
127 | 272 | boolean match = isMatch(getPatterns(), key); |
128 | 272 | if (match) { |
129 | 3 | return getMaskValue(); |
130 | |
} else { |
131 | 269 | return value; |
132 | |
} |
133 | |
} |
134 | |
|
135 | |
public boolean isFlattenPropertyValues() { |
136 | 275 | return flattenPropertyValues; |
137 | |
} |
138 | |
|
139 | |
public void setFlattenPropertyValues(boolean flattenPropertyValues) { |
140 | 3 | this.flattenPropertyValues = flattenPropertyValues; |
141 | 3 | } |
142 | |
|
143 | |
public boolean isMaskPropertyValues() { |
144 | 275 | return maskPropertyValues; |
145 | |
} |
146 | |
|
147 | |
public void setMaskPropertyValues(boolean maskPropertyValues) { |
148 | 3 | this.maskPropertyValues = maskPropertyValues; |
149 | 3 | } |
150 | |
|
151 | |
public String getMaskValue() { |
152 | 6 | return maskValue; |
153 | |
} |
154 | |
|
155 | |
public void setMaskValue(String maskValue) { |
156 | 3 | this.maskValue = maskValue; |
157 | 3 | } |
158 | |
|
159 | |
public String getLinefeedReplacement() { |
160 | 3 | return linefeedReplacement; |
161 | |
} |
162 | |
|
163 | |
public void setLinefeedReplacement(String linefeedReplacement) { |
164 | 3 | this.linefeedReplacement = linefeedReplacement; |
165 | 3 | } |
166 | |
|
167 | |
public String getCarriageReturnReplacement() { |
168 | 3 | return carriageReturnReplacement; |
169 | |
} |
170 | |
|
171 | |
public void setCarriageReturnReplacement(String carriageReturnReplacement) { |
172 | 3 | this.carriageReturnReplacement = carriageReturnReplacement; |
173 | 3 | } |
174 | |
|
175 | |
public boolean isTrimPropertyValues() { |
176 | 275 | return trimPropertyValues; |
177 | |
} |
178 | |
|
179 | |
public void setTrimPropertyValues(boolean trimPropertyValues) { |
180 | 3 | this.trimPropertyValues = trimPropertyValues; |
181 | 3 | } |
182 | |
|
183 | |
public String[] getMaskExpressions() { |
184 | 3 | return maskExpressions; |
185 | |
} |
186 | |
|
187 | |
public Pattern[] getPatterns() { |
188 | 272 | return patterns; |
189 | |
} |
190 | |
|
191 | |
public boolean isLogInSortedOrder() { |
192 | 3 | return logInSortedOrder; |
193 | |
} |
194 | |
|
195 | |
public void setLogInSortedOrder(boolean logInSortedOrder) { |
196 | 3 | this.logInSortedOrder = logInSortedOrder; |
197 | 3 | } |
198 | |
|
199 | |
} |