1919import java .nio .file .Files ;
2020import java .nio .file .Path ;
2121import java .nio .file .Paths ;
22- import java .util .Enumeration ;
23- import java .util .HashMap ;
24- import java .util .Map ;
25- import java .util .Properties ;
22+ import java .util .*;
2623import java .util .jar .JarEntry ;
2724import java .util .jar .JarFile ;
2825import java .util .jar .Manifest ;
26+ import java .util .logging .Level ;
27+ import java .util .logging .Logger ;
2928import java .util .prefs .Preferences ;
3029import java .util .regex .Matcher ;
3130import java .util .regex .Pattern ;
31+ import java .util .stream .Collectors ;
3232import java .util .stream .Stream ;
3333
3434/** Write preferences in property file format
3737@ SuppressWarnings ("nls" )
3838public class PropertyPreferenceWriter
3939{
40+ public static final Logger logger = Logger .getLogger (PropertyPreferenceWriter .class .getName ());
41+ public static Set <String > excludedKeys = new HashSet <>();
42+ public static Set <String > excludedPackages = new HashSet <>();
43+
4044 /** Save preferences in property file format
4145 *
4246 * <p>Properties have the name "package/setting",
@@ -50,16 +54,27 @@ public class PropertyPreferenceWriter
5054 */
5155 public static void save (final OutputStream stream ) throws Exception
5256 {
57+ Map <String , String > allKeysWithPackages = getAllPropertyKeys ();
58+ Preferences prefs = Preferences .userRoot ().node ("org/phoebus/ui" );
59+
60+ String value = prefs .get ("excluded_keys_from_settings_check" , "" );
61+ if (value .isEmpty ()) value = allKeysWithPackages .get ("org.phoebus.ui/excluded_keys_from_settings_check" );
62+ if (!value .isEmpty ()) excludedKeys = Arrays .stream (value .split ("," )).map (String ::trim ).collect (Collectors .toSet ());
63+
64+ value = prefs .get ("excluded_packages_from_settings_check" , "" );
65+ if (value .isEmpty ()) value = allKeysWithPackages .get ("org.phoebus.ui/excluded_packages_from_settings_check" );
66+ if (!value .isEmpty ()) excludedPackages = Arrays .stream (value .split ("," )).map (String ::trim ).collect (Collectors .toSet ());
67+
5368 try
5469 (
55- final OutputStreamWriter out = new OutputStreamWriter (stream );
70+ final OutputStreamWriter out = new OutputStreamWriter (stream )
5671 )
5772 {
5873 out .append ("# Preference settings<br/>\n " );
5974 out .append ("# Format:<br/>\n " );
6075 out .append ("# the.package.name/key=value<br/>\n " );
6176 out .append ("<div style='color: red; font-weight: bold'># key=value in red are incorrect properties</div><br/>\n " );
62- listSettings (getAllPropertyKeys () , out , Preferences .userRoot ());
77+ listSettings (allKeysWithPackages , out , Preferences .userRoot ());
6378 out .append ("<br/>\n " );
6479 out .append ("# End.<br/>\n " );
6580 out .flush ();
@@ -81,12 +96,10 @@ private static void formatSetting(Map<String, String> allKeysWithPackages, final
8196 String keyFound = allKeysWithPackages .get (fullKey );
8297 boolean bNotFound = keyFound == null ;
8398
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- }
99+ // exclude keys that must not be checked
100+ boolean containsExcludedKeys = excludedKeys .stream ().anyMatch (key ::contains );
101+ boolean containsExcludedPackages = excludedPackages .stream ().anyMatch (fullKey ::startsWith );
102+ if (containsExcludedKeys || containsExcludedPackages ) bNotFound = false ;
90103
91104 if (bNotFound ) out .append ("<div style='color: red; font-weight: bold'>" );
92105 out .append (escapeHtml (fullKey ))
@@ -96,12 +109,12 @@ private static void formatSetting(Map<String, String> allKeysWithPackages, final
96109 if (bNotFound ) out .append ("</div>" );
97110 }
98111
99- private static Map <String , String > getAllPropertyKeys () throws Exception
112+ private static Map <String , String > getAllPropertyKeys ()
100113 {
101114 Map <String , String > allKeysWithPackages = new HashMap <>();
102115
103116 String classpath = System .getProperty ("java.class.path" );
104- String [] jars = classpath .split (System . getProperty ( "path.separator" ) );
117+ String [] jars = classpath .split (File . pathSeparator );
105118
106119 if (jars .length == 1 ) jars = getAllJarFromManifest (jars [0 ]);
107120
@@ -123,26 +136,25 @@ private static Map<String, String> getAllPropertyKeys() throws Exception
123136 );
124137 }
125138 }
126- } catch (IOException e ) {
127- System . err . println ( "Error opening JAR : " + jarEntry );
139+ } catch (IOException ex ) {
140+ logger . log ( Level . WARNING , "Error opening JAR : " + jarEntry , ex );
128141 }
129142 }
130143 else if (jarEntry .endsWith ("classes" )) {
131144 Path startPath = Paths .get (jarEntry );
132145 String filePattern = "preferences.properties" ;
133146
134- System .out .println (startPath );
135147 try (Stream <Path > paths = Files .walk (startPath )) {
136148 paths .filter (path -> path .toString ().endsWith (filePattern ))
137149 .forEach (path -> {
138150 try (InputStream inputStream = Files .newInputStream (path )) {
139151 parsePropertiesWithPackage (inputStream , path .getFileName ().toString (), allKeysWithPackages );
140- } catch (IOException e ) {
141- System . err . println ( "Error opening properties : " + path );
152+ } catch (IOException ex ) {
153+ logger . log ( Level . WARNING , "Error opening properties file : " + path , ex );
142154 }
143155 });
144- } catch (IOException e ) {
145- System . err . println ( "Error listing files in : " + startPath );
156+ } catch (IOException ex ) {
157+ logger . log ( Level . WARNING , "Error listing files in : " + startPath , ex );
146158 }
147159 }
148160 }
@@ -168,13 +180,13 @@ private static String[] getAllJarFromManifest(String jarPath) {
168180 jars [iJar ] = fullPath .toString ();
169181 }
170182 } else {
171- System . err . println ( "No Class-Path found in MANIFEST.MF." );
183+ logger . log ( Level . WARNING , "No Class-Path found in MANIFEST.MF " + jarPath );
172184 }
173185 } else {
174- System . err . println ( "MANIFEST.MF not found in the JAR." );
186+ logger . log ( Level . WARNING , "MANIFEST.MF not found in the JAR " + jarPath );
175187 }
176- } catch (IOException e ) {
177- System . err . println ( "Error when reading the jar : " + jarPath );
188+ } catch (IOException ex ) {
189+ logger . log ( Level . WARNING , "Error when reading the jar : " + jarPath , ex );
178190 }
179191
180192 return jars ;
@@ -192,7 +204,7 @@ private static void parsePropertiesWithPackage(InputStream inputStream, String f
192204 line = line .trim ();
193205 if (line .startsWith ("#" ) && line .contains ("Package" )) {
194206 // Find package name
195- Pattern pattern = Pattern .compile ("#\\ s*Package\\ s+([^ \\ s] +)" );
207+ Pattern pattern = Pattern .compile ("#\\ s*Package\\ s+(\\ S +)" );
196208 Matcher matcher = pattern .matcher (line );
197209 if (matcher .find ()) {
198210 packageName = matcher .group (1 );
@@ -202,7 +214,7 @@ private static void parsePropertiesWithPackage(InputStream inputStream, String f
202214 }
203215 }
204216
205- if (content .length () > 0 ) {
217+ if (! content .isEmpty () ) {
206218 props .load (new ByteArrayInputStream (content .toString ().getBytes ()));
207219 }
208220
@@ -211,9 +223,8 @@ private static void parsePropertiesWithPackage(InputStream inputStream, String f
211223 String prefixedKey = (packageName != null ) ? packageName + "/" + key : key ;
212224 allKeysWithPackages .put (prefixedKey , props .getProperty (key ));
213225 }
214- } catch (IOException e ) {
215- System .err .println ("Error when reading file " + fileName );
216- e .printStackTrace ();
226+ } catch (IOException ex ) {
227+ logger .log (Level .WARNING , "Error when reading file " + fileName , ex );
217228 }
218229 }
219230
0 commit comments