forked from uPortal-Project/uPortal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findProperties.groovy
executable file
·219 lines (202 loc) · 8.48 KB
/
findProperties.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env groovy
/**
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
def cl = new CliBuilder(usage: 'groovy findProperties.groovy [-s] [-h] [-d]')
cl.h(longOpt:'help', 'Show usage information and quit')
cl.s(longOpt:'source', 'Show property source')
cl.d(longOpt:'debug', 'Show debugging information')
def opt = cl.parse(args)
if (opt.h) {
cl.usage();
return;
}
def activePlaceholders = new TreeMap(); //Track active placeholders from portal.properties
def examplePlaceholders = new TreeMap(); //Track example placeholders from portal.properties
def foundPlaceholders = new TreeMap(); //Track placeholders used in code
//search .properties files for existing placeholders
searchFilePlaceholders(
new File( 'uportal-war/src/main/resources/properties' ),
~/.*\.properties/,
~/^([^#= ]+)(=?)(.*)$/,
activePlaceholders);
searchFilePlaceholders(
new File( 'uportal-war/src/main/resources/properties' ),
~/.*\.properties/,
~/^#([^#= ]+)(=)(.*)$/,
examplePlaceholders);
//Search through spring context files for property placeholders
searchPlaceholders(
new File( 'uportal-war/src/main/resources/properties/contexts' ),
~/.*\.xml/,
~/\$\{([^}:]*)(:?)(.*)}/,
foundPlaceholders);
//Serach through Java files for property placholders in @Value annotations
searchPlaceholders(
new File( 'uportal-war/src/main/java' ),
~/.*\.java/,
~/@Value *\( *"\$\{([^}:]*)(:?)(.*)}" *\)/,
foundPlaceholders);
if (opt.d) {
println ""
println "Active Properties Found in portal.properties"
activePlaceholders.each {
println "\t" + it.key + "=" + it.value;
}
println ""
println "Example Properties Found in portal.properties"
examplePlaceholders.each {
println "\t" + it.key + "=" + it.value;
}
println ""
println "Properties Found in XML and Java files"
foundPlaceholders.each {
println "\t" + it.key + "=" + it.value;
}
println ""
}
def existingPlaceholders = new TreeMap();
existingPlaceholders.putAll(examplePlaceholders);
existingPlaceholders.putAll(activePlaceholders);
def fatalPlaceholders = new TreeMap(); //Placeholders that are missing from properties file and have no default value
def missingPlaceholders = new TreeMap(); //Placeholders that are missing from properties file
def incorrectPlaceholders = new TreeMap(); //Placeholders that exist in the properties file but have differing default values
def shouldntBeActivePlaceholders = new TreeMap(); //Placeholders that exist and are not commented out but are set to their default value
//Process the search results into the missing, incorrect and not-used sets
foundPlaceholders.each {
existingValue = existingPlaceholders.remove(it.key);
activeValue = activePlaceholders.get(it.key);
if (!it.value.hasValue && activeValue == null) {
//No default value for the placeholder and no active property configured, this is a fatal problem
fatalPlaceholders.put(it.key, it.value);
}
else if (existingValue == null) {
//Placeholder has a default but no matching entry in portal.properties, record it as missing
missingPlaceholders.put(it.key, it.value);
}
else if (it.value.hasValue) {
if (existingValue.value != it.value.value) {
//Placeholder has default value that doesn't match the value in portal.properties, record it as incorrect
it.value.oldValue = existingValue.value
incorrectPlaceholders.put(it.key, it.value);
}
else if (activeValue != null && activeValue.value == it.value.value) {
//Placeholder has a default value which matches the active (uncommented) value in portal.properties
shouldntBeActivePlaceholders.put(it.key, it.value);
}
}
}
println ""
if (!existingPlaceholders.isEmpty()) {
println "Properties in portal.properties that are not referenced"
existingPlaceholders.each {
println "\t" + it.key + "=" + it.value.value;
if (opt.s) {
println "\t\t" + it.value.source;
}
}
println ""
}
if (!missingPlaceholders.isEmpty()) {
println "Properties that are missing from portal.properties"
missingPlaceholders.each {
println "\t" + it.key + "=" + it.value.value
if (opt.s) {
println "\t\t" + it.value.source;
}
}
println ""
}
if (!shouldntBeActivePlaceholders.isEmpty()) {
println "Properties that are not commented out where the default value matches the property value"
shouldntBeActivePlaceholders.each {
println "\t" + it.key + "=" + it.value.value;
if (opt.s) {
println "\t\t" + it.value.source;
}
}
println ""
}
if (!incorrectPlaceholders.isEmpty()) {
println "Properties with incorrect default values in portal.properties (placeholder value)!=(portal.properties value)"
incorrectPlaceholders.each {
println "\t" + it.key + "=(" + it.value.value + ")!=(" + it.value.oldValue + ")";
if (opt.s) {
println "\t\t" + it.value.source;
}
}
println ""
}
if (!fatalPlaceholders.isEmpty()) {
println "FATAL: Placeholders with no default value and no active property"
fatalPlaceholders.each {
println "\t" + it.key + "=" + it.value.value
println "\t\t" + it.value.source;
}
println ""
}
/**
* Recursively search a directory for matching files, extracting placeholder data and storing
* it into the placeholders map
*/
def searchPlaceholders(baseDir, xmlPattern, placeholderPattern, placeholders) {
searchFilePlaceholders(baseDir, xmlPattern, placeholderPattern, placeholders);
baseDir.eachDirRecurse() { dir ->
searchFilePlaceholders(dir, xmlPattern, placeholderPattern, placeholders);
}
}
/**
* Search a directory for matching files, extracting placeholder data and storing it into
* the placeholders map
*/
def searchFilePlaceholders(dir, fileMatcher, placeholderPattern, placeholders) {
dir.eachFileMatch(fileMatcher) { file ->
file.eachLine { ln ->
lnMatcher = ( ln =~ placeholderPattern );
while ( lnMatcher.find() ) {
def newPlaceholder = [
source: [ file ],
key: lnMatcher[0][1].trim(),
hasValue: lnMatcher[0][2].trim().size() > 0,
value: lnMatcher[0][3]
];
existingPlaceholder = placeholders.get(newPlaceholder.key);
if (existingPlaceholder != null) {
if (existingPlaceholder.value == newPlaceholder.value) {
//Multiple placeholders found with the same value, just add to the source array
existingPlaceholder.source.add(file);
}
else if (newPlaceholder.hasValue && !existingPlaceholder.hasValue) {
//Existing placeholder didn't have a value, new one does, use the valued version
placeholders.put(newPlaceholder.key, newPlaceholder);
}
else if (existingPlaceholder.hasValue && existingPlaceholder.value != newPlaceholder.value) {
System.err << "WARNING: Replacing value for property: " + newPlaceholder.key + "\n" +
"\told value: " + existingPlaceholder.value + " from " + existingPlaceholder.source + "\n" +
"\tnew value: " + newPlaceholder.value + " from " + newPlaceholder.source + "\n";
placeholders.put(newPlaceholder.key, newPlaceholder);
}
}
else {
//Nothing exists, add the placeholder
placeholders.put(newPlaceholder.key, newPlaceholder);
}
}
}
}
}