Skip to content

Commit 717fc53

Browse files
author
lcaouen
committed
check if property files are in classes folders and exclude keys from the check
1 parent 54083d7 commit 717fc53

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

core/framework/src/main/java/org/phoebus/framework/preferences/PropertyPreferenceWriter.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.OutputStream;
1717
import java.io.OutputStreamWriter;
1818
import java.io.Writer;
19+
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.nio.file.Paths;
2122
import java.util.Enumeration;
@@ -28,6 +29,7 @@
2829
import java.util.prefs.Preferences;
2930
import java.util.regex.Matcher;
3031
import java.util.regex.Pattern;
32+
import java.util.stream.Stream;
3133

3234
/** Write preferences in property file format
3335
* @author Kay Kasemir
@@ -58,6 +60,7 @@ public static void save(final OutputStream stream) throws Exception
5860
out.append("# the.package.name/key=value<br/>\n");
5961
out.append("<div style='color: red; font-weight: bold'># key=value in red are incorrect properties</div><br/>\n");
6062
listSettings(getAllPropertyKeys(), out, Preferences.userRoot());
63+
out.append("<br/>\n");
6164
out.append("# End.<br/>\n");
6265
out.flush();
6366
}
@@ -76,7 +79,15 @@ private static void formatSetting(Map<String, String> allKeysWithPackages, final
7679
final String path = node.absolutePath();
7780
String fullKey = path.substring(1).replace('/', '.') + '/' + key;
7881
String keyFound = allKeysWithPackages.get(fullKey);
79-
boolean bNotFound = keyFound == null ? true : false;
82+
boolean bNotFound = keyFound == null;
83+
84+
// ignore keys that can be used but not from preferences.properties
85+
if (key.toLowerCase().contains("external_app") ||
86+
key.toLowerCase().contains("password") ||
87+
key.toLowerCase().contains("username")) {
88+
bNotFound = false;
89+
}
90+
8091
if (bNotFound) out.append("<div style='color: red; font-weight: bold'>");
8192
out.append(escapeHtml(fullKey))
8293
.append('=')
@@ -95,9 +106,8 @@ private static Map<String, String> getAllPropertyKeys() throws Exception
95106
if (jars.length == 1) jars = getAllJarFromManifest(jars[0]);
96107

97108
for (String jarEntry : jars) {
98-
File file = new File(jarEntry);
99-
100109
if (jarEntry.endsWith(".jar")) {
110+
File file = new File(jarEntry);
101111
try (JarFile jarFile = new JarFile(file)) {
102112
Enumeration<JarEntry> entries = jarFile.entries();
103113

@@ -115,7 +125,24 @@ private static Map<String, String> getAllPropertyKeys() throws Exception
115125
}
116126
} catch (IOException e) {
117127
System.err.println("Error opening JAR : " + jarEntry);
118-
e.printStackTrace();
128+
}
129+
}
130+
else if (jarEntry.endsWith("classes")) {
131+
Path startPath = Paths.get(jarEntry);
132+
String filePattern = "preferences.properties";
133+
134+
System.out.println(startPath);
135+
try (Stream<Path> paths = Files.walk(startPath)) {
136+
paths.filter(path -> path.toString().endsWith(filePattern))
137+
.forEach(path -> {
138+
try (InputStream inputStream = Files.newInputStream(path)) {
139+
parsePropertiesWithPackage(inputStream, path.getFileName().toString(), allKeysWithPackages);
140+
} catch (IOException e) {
141+
System.err.println("Error opening properties : " + path);
142+
}
143+
});
144+
} catch (IOException e) {
145+
System.err.println("Error listing files in : " + startPath);
119146
}
120147
}
121148
}
@@ -148,7 +175,6 @@ private static String[] getAllJarFromManifest(String jarPath) {
148175
}
149176
} catch (IOException e) {
150177
System.err.println("Error when reading the jar : " + jarPath);
151-
e.printStackTrace();
152178
}
153179

154180
return jars;

0 commit comments

Comments
 (0)