/** * Wraps a preferences object in this instance. * * @param prefs the configuration preferences * * @throws IllegalArgumentException if props is <code>null</code> */ public ServerConfiguration(Preferences prefs) { if (prefs == null) { throw new IllegalArgumentException("prefs=null"); } m_preferences = new ObfuscatedPreferences(prefs, ServerConfigurationConstants.class); }
@SuppressWarnings("rawtypes") public ObfuscatedPreferences(Preferences actualPreferences, Class classz) { this.actualPreferences = actualPreferences; for (Field field : classz.getFields()) { Restricted restricted = field.getAnnotation(Restricted.class); if (restricted != null) { try { String restrictedProperty = field.get(classz).toString(); restrictedPreferences.add(restrictedProperty); this.get(restrictedProperty, null); } catch (Exception e) { //nothing to do, the field is just not accesible } } } try{ for (String key : actualPreferences.keys()) { if (!restrictedPreferences.contains(key)) { String storedValue = actualPreferences.get(key, null); if (storedValue != null && RestrictedFormat.isRestrictedFormat(storedValue)) { userRestrictedPreferences.add(key); } } } } catch (Exception e) { //nothing to do since this was just an exploration to see if the //user requested any other properties to be restricted } }
@Override public String get(String key, String def) { String value = actualPreferences.get(key, null); if (value == null) { return def; } if (restrictedPreferences.contains(key) || userRestrictedPreferences.contains(key)) { try { if (RestrictedFormat.isRestrictedFormat(value)) { value = RestrictedFormat.retrieveValue(value); return PicketBoxObfuscator.decode(value); } else { throw new Exception("Value not in a restricted format"); } } catch (Exception ex) { this.put(key, value); return value; } } else { return value; } }