Example usage for java.util.prefs Preferences node

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

Introduction

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

Prototype

public abstract Preferences node(String pathName);

Source Link

Document

Returns the named preference node in the same tree as this node, creating it and any of its ancestors if they do not already exist.

Usage

From source file:org.settings4j.connector.PreferencesConnector.java

/**
 * Resolve the given path and key against the given Preferences.
 *
 * @param path the preferences path (placeholder part before '/')
 * @param key the preferences key (placeholder part after '/')
 * @param value the Value to store./*ww  w.ja v a2  s . c  o m*/
 * @param preferences the Preferences to resolve against
 */
protected void setPreferenceValue(final String path, final String key, final String value,
        final Preferences preferences) {
    if (path != null) {
        preferences.node(path).put(key, value);
    } else {
        preferences.put(key, value);
    }
    try {
        preferences.flush();
    } catch (final BackingStoreException e) {
        throw new RuntimeException("Cannot access specified node path [" + path + "]", e);
    }
}

From source file:org.javaswift.cloudie.login.CredentialsStore.java

/**
 * saves the given credentials./*www  .ja  v a  2s  . co m*/
 * @param cr the credentials.
 */
public void save(Credentials cr) {
    try {
        Preferences prefs = Preferences.userNodeForPackage(CredentialsStore.class);
        saveCredentials(prefs.node(cr.toString()), cr);
        prefs.flush();
    } catch (BackingStoreException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.fuin.utils4j.PropertiesFilePreferencesTest.java

/**
 * @testng.test//from w w  w . j  av  a2 s .co  m
 */
public final void testChildConstruction() throws BackingStoreException {
    final File dir = new File(baseDir, "user");
    final File subDir = new File(dir, "abc");
    final File subDir2 = new File(subDir, "def");
    final File file = new File(subDir, PropertiesFilePreferences.FILENAME);
    final File file2 = new File(subDir2, PropertiesFilePreferences.FILENAME);

    final PropertiesFilePreferences root = new PropertiesFilePreferences(dir);

    // Create child node
    final Preferences pref = root.node("abc");
    pref.put("one", "A");
    pref.put("two", "B");
    pref.put("three", "C");

    final Preferences pref2 = pref.node("def");
    pref2.put("four", "4");

    // Flush root to save data to disk
    root.flush();

    Assert.assertTrue(file.exists());
    Assert.assertTrue(file2.exists());
    TestHelper.assertPropertiesEqual(file, ((PropertiesFilePreferences) pref).toProperties());
    TestHelper.assertPropertiesEqual(file2, ((PropertiesFilePreferences) pref2).toProperties());
}

From source file:org.settings4j.helper.spring.Settings4jPlaceholderConfigurerTest.java

private void removeUnitTestNode(final Preferences userRoot) throws BackingStoreException {
    if (userRoot.nodeExists(PREF_UNITTEST_NODE)) {
        userRoot.node(PREF_UNITTEST_NODE).removeNode();
    }/*from   www  .j  a v a 2s . c  o  m*/
}

From source file:org.pdfsam.module.PreferencesUsageDataStore.java

public List<ModuleUsage> getUsages() {
    Preferences prefs = Preferences.userRoot().node(USAGE_PATH);
    List<ModuleUsage> retList = new ArrayList<>();
    try {/*from  w  w  w  . j av  a 2  s.  c om*/
        List<String> jsons = Arrays.stream(prefs.childrenNames()).parallel().map(name -> prefs.node(name))
                .map(node -> node.get(MODULE_USAGE_KEY, "")).filter(json -> isNotBlank(json)).collect(toList());
        for (String json : jsons) {
            retList.add(JSON.std.beanFrom(ModuleUsage.class, json));
        }
    } catch (BackingStoreException | IOException e) {
        LOG.error("Unable to get modules usage statistics", e);
    }
    return retList;
}

From source file:org.javaswift.cloudie.login.CredentialsStore.java

/**
 * lists the available credentials for this user.
 * @return the credentials./*from  w ww .  j  a v  a  2s  . c om*/
 */
public List<Credentials> getAvailableCredentials() {
    List<Credentials> results = new ArrayList<Credentials>();
    try {
        Preferences prefs = Preferences.userNodeForPackage(CredentialsStore.class);
        for (String node : prefs.childrenNames()) {
            Preferences cred = prefs.node(node);
            try {
                results.add(toCredentials(cred));
            } catch (IllegalArgumentException ex) {
                LoggerFactory.getLogger(getClass()).warn("Bad preferences node - skipping");
            }
        }
    } catch (BackingStoreException ex) {
        throw new RuntimeException(ex);
    }
    return results;
}

From source file:de.fhg.igd.mapviewer.server.tiles.CustomTileMapServerConfiguration.java

/**
 * Save the configuration//from  w ww.  j a v a  2  s  .c om
 * 
 */
public void save() {
    Preferences preferences = getPreferences();

    try {
        String name = getName();
        Preferences node = preferences.node(name);
        setName(name);

        saveProperties(node);

        node.flush();
    } catch (BackingStoreException e) {
        log.error("Error saving map server preferences", e); //$NON-NLS-1$
    }
}

From source file:org.apache.cayenne.modeler.Application.java

public Preferences getMainPreferenceForProject() {

    DataChannelDescriptor descriptor = (DataChannelDescriptor) getFrameController().getProjectController()
            .getProject().getRootNode();

    // if new project
    if (descriptor.getConfigurationSource() == null) {
        return getPreferencesNode(getProject().getClass(), getNewProjectTemporaryName());
    }//from  ww w.  j a va2  s  .c  om

    String path = CayennePreference
            .filePathToPrefereceNodePath(descriptor.getConfigurationSource().getURL().getPath());
    Preferences pref = getPreferencesNode(getProject().getClass(), "");
    return pref.node(pref.absolutePath() + path);
}

From source file:de.fhg.igd.mapviewer.server.tiles.CustomTileMapServerConfiguration.java

/**
 * Load configuration with the given name
 * //from w w  w.  j a va2  s . com
 * @param name the configuration name
 * 
 * @return if loading the configuration succeeded
 */
public boolean load(String name) {
    Preferences preferences = getPreferences();

    try {
        if (preferences.nodeExists(name)) {
            Preferences node = preferences.node(name);

            setName(name);

            loadProperties(node);

            return true;
        } else {
            log.warn("No configuration named " + name + " found"); //$NON-NLS-1$ //$NON-NLS-2$
            return false;
        }
    } catch (BackingStoreException e) {
        log.error("Error loading CustomTile configuration"); //$NON-NLS-1$
        return false;
    }
}

From source file:de.fhg.igd.mapviewer.server.wms.WMSConfiguration.java

/**
 * Load a WMS configuration with the given name
 * //from   ww w.j ava  2  s .  co m
 * @param name the configuration name
 * 
 * @return if loading the configuration succeeded
 */
public boolean load(String name) {
    Preferences preferences = getPreferences();

    try {
        if (preferences.nodeExists(name)) {
            Preferences node = preferences.node(name);

            setName(name);

            loadProperties(node);

            return true;
        } else {
            log.warn("No configuration named " + name + " found"); //$NON-NLS-1$ //$NON-NLS-2$
            return false;
        }
    } catch (BackingStoreException e) {
        log.error("Error loading WMS configuration"); //$NON-NLS-1$
        return false;
    }
}