Example usage for java.util.prefs Preferences clear

List of usage examples for java.util.prefs Preferences clear

Introduction

In this page you can find the example usage for java.util.prefs Preferences clear.

Prototype

public abstract void clear() throws BackingStoreException;

Source Link

Document

Removes all of the preferences (key-value associations) in this preference node.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Get the user preference node for java.lang
    Preferences prefs = Preferences.userNodeForPackage(String.class);

    // Remove a preference in the node
    final String PREF_NAME = "name_of_preference";
    prefs.remove(PREF_NAME);/*  w w  w. j a v  a 2 s  . c om*/

    // Remove all preferences in the node

    prefs.clear();

}

From source file:kindleclippings.quizlet.QuizletSync.java

public static void clearPrefs() throws BackingStoreException {
    Preferences prefs = Preferences.userNodeForPackage(QuizletSync.class);
    prefs.clear();
    prefs.flush();/*  w ww.  j  a  va2  s. com*/
}

From source file:edu.umd.cs.findbugs.gui2.GUISaveState.java

static void clear() {
    Preferences p = Preferences.userNodeForPackage(GUISaveState.class);
    try {//  w  ww .ja v  a 2 s .  c  o  m
        p.clear();
    } catch (BackingStoreException e) {
        Debug.println(e);
    }
    instance = new GUISaveState();
}

From source file:org.pdfsam.ui.PreferencesRecentWorkspacesService.java

@PreDestroy
public void flush() {
    Preferences prefs = Preferences.userRoot().node(WORKSPACES_PATH);
    LOG.trace("Flushing recently used workspaces");
    try {//w  w  w.j a va 2  s . c o  m
        prefs.clear();
        for (Entry<String, String> entry : cache.entrySet()) {
            prefs.put(entry.getValue(), entry.getKey());
        }
        prefs.flush();
    } catch (BackingStoreException e) {
        LOG.error("Error storing recently used workspace", e);
    }
}

From source file:org.apache.cayenne.pref.UpgradeCayennePreference.java

public void upgrade() {
    try {// ww  w.  j a va2 s  .co m

        if (!Preferences.userRoot().nodeExists(CAYENNE_PREFERENCES_PATH)) {

            File prefsFile = new File(preferencesDirectory(), PREFERENCES_NAME_OLD);
            if (prefsFile.exists()) {
                ExtendedProperties ep = new ExtendedProperties();
                try {
                    ep.load(new FileInputStream(prefsFile));

                    Preferences prefEditor = Preferences.userRoot().node(CAYENNE_PREFERENCES_PATH).node(EDITOR);

                    prefEditor.putBoolean(ModelerPreferences.EDITOR_LOGFILE_ENABLED,
                            ep.getBoolean(EDITOR_LOGFILE_ENABLED_OLD));
                    prefEditor.put(ModelerPreferences.EDITOR_LOGFILE, ep.getString(EDITOR_LOGFILE_OLD));

                    Preferences frefLastProjFiles = prefEditor.node(LAST_PROJ_FILES);

                    Vector arr = ep.getVector(LAST_PROJ_FILES_OLD);

                    while (arr.size() > ModelerPreferences.LAST_PROJ_FILES_SIZE) {
                        arr.remove(arr.size() - 1);
                    }

                    frefLastProjFiles.clear();
                    int size = arr.size();

                    for (int i = 0; i < size; i++) {
                        frefLastProjFiles.put(String.valueOf(i), arr.get(i).toString());
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (BackingStoreException e) {
        // do nothing
    }
}

From source file:org.apache.cayenne.modeler.preferences.UpgradeCayennePreference.java

public void upgrade() {
    try {/*from  w w w  . j a  va 2 s.  com*/

        if (!Preferences.userRoot().nodeExists(CAYENNE_PREFERENCES_PATH)) {

            File prefsFile = new File(preferencesDirectory(), PREFERENCES_NAME_OLD);
            if (prefsFile.exists()) {
                ExtendedProperties ep = new ExtendedProperties();
                try {
                    ep.load(new FileInputStream(prefsFile));

                    Preferences prefEditor = Preferences.userRoot().node(CAYENNE_PREFERENCES_PATH).node(EDITOR);

                    prefEditor.putBoolean(ModelerPreferences.EDITOR_LOGFILE_ENABLED,
                            ep.getBoolean(EDITOR_LOGFILE_ENABLED_OLD));
                    prefEditor.put(ModelerPreferences.EDITOR_LOGFILE, ep.getString(EDITOR_LOGFILE_OLD));

                    Preferences frefLastProjFiles = prefEditor.node(LAST_PROJ_FILES);

                    Vector arr = ep.getVector(LAST_PROJ_FILES_OLD);

                    while (arr.size() > ModelerPreferences.LAST_PROJ_FILES_SIZE) {
                        arr.remove(arr.size() - 1);
                    }

                    frefLastProjFiles.clear();
                    int size = arr.size();

                    for (int i = 0; i < size; i++) {
                        frefLastProjFiles.put(String.valueOf(i), arr.get(i).toString());
                    }
                } catch (FileNotFoundException e) {
                    LOGGER.error(e);
                } catch (IOException e) {
                    LOGGER.error(e);
                }
            }
        }
    } catch (BackingStoreException e) {
        // do nothing
    }
}

From source file:com.vaadin.integration.maven.wscdn.CvalChecker.java

private CvalInfo getCachedLicenseInfo(String productName) {
    Preferences p = Preferences.userNodeForPackage(CvalInfo.class);
    String json = p.get(productName, "");
    if (!json.isEmpty()) {
        try {//from   w  ww .  j  av a 2s  . co m
            CvalInfo info = parseJson(json);
            if (info != null) {
                return info;
            }
        } catch (RuntimeException e) {
            try {
                p.clear();
            } catch (BackingStoreException ex) {
                Logger.getLogger(CvalChecker.class.getName()).log(Level.SEVERE, null, ex);
            }
            throw e;
        }
    }
    return null;
}

From source file:org.rhq.enterprise.server.agent.EmbeddedAgentBootstrapService.java

/**
 * Loads the {@link #getConfigurationFile() configuration file}. The file location will first be checked for
 * existence on the file system and then as a URL. If it cannot be found, it will be assumed the file location
 * specifies the file as found in the current class loader and the file will be searched there. An exception is
 * thrown if the file cannot be found anywhere.
 *
 * @return the configuration that was loaded
 *
 * @throws IOException                       if failed to load the configuration file
 * @throws InvalidPreferencesFormatException if the configuration file had an invalid format
 * @throws BackingStoreException             if failed to access the preferences persistence store
 * @throws Exception                         on other failures
 *///from ww  w  .j a va 2s.  c om
private AgentConfiguration loadConfigurationFile() throws Exception {
    String file_name = getConfigurationFile();
    String preferences_node_name = getPreferencesNodeName();
    InputStream config_file_input_stream = null;

    // first see if the file was specified as a path on the local file system
    try {
        File config_file = new File(file_name);

        if (config_file.exists()) {
            config_file_input_stream = new FileInputStream(config_file);
        }
    } catch (Exception e) {
        // isn't really an error - this just isn't a file on the local file system
    }

    // see if the file was specified as a URL
    if (config_file_input_stream == null) {
        try {
            URL config_file = new URL(file_name);

            config_file_input_stream = config_file.openStream();
        } catch (Exception e) {
            // isn't really an error - this just isn't a URL
        }
    }

    // if neither a file path or URL, assume the config file can be found in the classloader
    if (config_file_input_stream == null) {
        config_file_input_stream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(file_name);
    }

    if (config_file_input_stream == null) {
        throw new IOException("Bad config file: " + file_name);
    }

    // We need to clear out any previous configuration in case the current config file doesn't specify a preference
    // that already exists in the preferences node.  In this case, the configuration file wants to fall back on the
    // default value and if we don't clear the preferences, we aren't guaranteed the value stored in the backing
    // store is the default value.
    // But first we need to backup these original preferences in case the config file fails to load -
    // we'll restore the original values in that case.

    Preferences preferences_node = getPreferencesNode();
    ByteArrayOutputStream backup = new ByteArrayOutputStream();
    preferences_node.exportSubtree(backup);
    preferences_node.clear();

    // now load in the preferences
    try {
        ByteArrayOutputStream raw_config_file = new ByteArrayOutputStream();
        StreamUtil.copy(config_file_input_stream, raw_config_file, true);
        String new_config = StringPropertyReplacer.replaceProperties(raw_config_file.toString());
        ByteArrayInputStream new_config_input_stream = new ByteArrayInputStream(new_config.getBytes());
        Preferences.importPreferences(new_config_input_stream);

        if (new AgentConfiguration(preferences_node).getAgentConfigurationVersion() == 0) {
            throw new IllegalArgumentException("Bad node name: " + preferences_node_name);
        }
    } catch (Exception e) {
        // a problem occurred importing the config file; let's restore our original values
        try {
            Preferences.importPreferences(new ByteArrayInputStream(backup.toByteArray()));
        } catch (Exception e1) {
            // its conceivable the same problem occurred here as with the original exception (backing store problem?)
            // let's throw the original exception, not this one
        }

        throw e;
    }

    AgentConfiguration agent_configuration = new AgentConfiguration(preferences_node);

    return agent_configuration;
}

From source file:edu.umass.cs.msocket.proxy.console.ConsoleModule.java

/**
 * Store the current command history/*from w  w  w. j a v  a  2s  . c  o m*/
 */
public void storeHistory() {
    List history = console.getHistory().getHistoryList();
    try {
        Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
        prefs.clear();
        int historySize = history.size();
        int start = Math.max(0, historySize - 100);
        // save up to the last 100th history items only
        // witht the stored index starting at 0
        for (int i = start; i < historySize; i++) {
            prefs.put(String.valueOf(i - start), (String) history.get(i + start));
        }
        prefs.flush();
    } catch (Exception e) {
        // unable to store prefs: do nothing
    }
}

From source file:net.sf.jabref.JabRefPreferences.java

/**
 * Adds the given key pattern to the preferences
 *
 * @param pattern the pattern to store/*from  w  w w  . j av  a  2  s . c  om*/
 */
public void putKeyPattern(GlobalLabelPattern pattern) {
    keyPattern = pattern;

    // Store overridden definitions to Preferences.
    Preferences pre = Preferences.userNodeForPackage(GlobalLabelPattern.class);
    try {
        pre.clear(); // We remove all old entries.
    } catch (BackingStoreException ex) {
        LOGGER.info("BackingStoreException in JabRefPreferences.putKeyPattern", ex);
    }

    Set<String> allKeys = pattern.getAllKeys();
    for (String key : allKeys) {
        if (!pattern.isDefaultValue(key)) {
            // no default value
            // the first entry in the array is the full pattern
            // see net.sf.jabref.logic.labelPattern.LabelPatternUtil.split(String)
            pre.put(key, pattern.getValue(key).get(0));
        }
    }
}