// https://github.com/sonatype/nexus/blob/2f0e154ec565969b4fd8698883ab76a461210f4f/nexus/nexus-test-harness/nexus-it-helper-plugin/src/main/java/org/sonatype/nexus/rt/prefs/FilePreferencesFactory.java package net.sourceforge.tuned.prefs; import java.io.File; import java.util.prefs.Preferences; import java.util.prefs.PreferencesFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * PreferencesFactory implementation that stores the preferences in a user-defined file. To use it, set the system * property java.util.prefs.PreferencesFactory to net.sourceforge.tuned.pref.FilePreferencesFactory *
* The file defaults to [user.home]/.fileprefs, but may be overridden with the system property * net.sourceforge.tuned.pref.FilePreferencesFactory.file. Modified by cstamas, switched to SLF4J logging, and * exposed preferences file property. * * @author David Croft (www.davidc.net) * @version $Id: FilePreferencesFactory.java 282 2009-06-18 17:05:18Z david $ */ public class FilePreferencesFactory implements PreferencesFactory { private static final Logger log = LoggerFactory.getLogger(FilePreferencesFactory.class.getName()); Preferences rootPreferences; public static final String SYSTEM_PROPERTY_FILE = "net.sourceforge.tuned.prefs.file"; public Preferences systemRoot() { return userRoot(); } public Preferences userRoot() { if (rootPreferences == null) { log.debug("Instantiating root preferences"); rootPreferences = new FilePreferences(null, ""); } return rootPreferences; } private static File preferencesFile; public static File getPreferencesFile() { if (preferencesFile == null) { String prefsFile = System.getProperty(SYSTEM_PROPERTY_FILE); if (prefsFile == null || prefsFile.length() == 0) { prefsFile = System.getProperty("user.home") + File.separator + ".fileprefs"; } preferencesFile = new File(prefsFile).getAbsoluteFile(); log.info("Preferences file is {}", preferencesFile); } return preferencesFile; } public static void setPreferencesFile(File file) { preferencesFile = file; } }