Example usage for java.util.prefs Preferences nodeExists

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

Introduction

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

Prototype

public abstract boolean nodeExists(String pathName) throws BackingStoreException;

Source Link

Document

Returns true if the named preference node exists in the same tree as this node.

Usage

From source file:org.pentaho.reporting.ui.datasources.jdbc.connection.JdbcConnectionDefinitionManager.java

/**
 * package-local visibility for testing purposes
 *///from  w  w w .j  a va  2s  .  c  om
JdbcConnectionDefinitionManager(final Preferences externalPreferences, final String node) {
    userPreferences = externalPreferences;
    // Load the list of JNDI Sources
    try {
        final String[] childNodeNames = userPreferences.childrenNames();
        for (int i = 0; i < childNodeNames.length; i++) {
            final String name = childNodeNames[i];
            final Preferences p = userPreferences.node(name);
            final String type = p.get("type", null);
            if (type == null) {
                p.removeNode();
            } else if (type.equals("local")) {
                final Properties props = new Properties();
                if (p.nodeExists("properties")) {
                    final Preferences preferences = p.node("properties");
                    final String[] strings = preferences.keys();
                    for (int j = 0; j < strings.length; j++) {
                        final String string = strings[j];
                        final String value = preferences.get(string, null);
                        if (value != null) {
                            props.setProperty(string, value);
                        } else {
                            props.remove(string);
                        }
                    }
                }

                final DriverConnectionDefinition driverConnection = new DriverConnectionDefinition(name,
                        p.get(DRIVER_KEY, null), p.get(URL_KEY, null), p.get(USERNAME_KEY, null),
                        p.get(PASSWORD_KEY, null), p.get(HOSTNAME_KEY, null), p.get(DATABASE_NAME_KEY, null),
                        p.get(DATABASE_TYPE_KEY, null), p.get(PORT_KEY, null), props);

                connectionDefinitions.put(name, driverConnection);
            } else if (type.equals("jndi")) {
                final JndiConnectionDefinition connectionDefinition = new JndiConnectionDefinition(name,
                        p.get(JNDI_LOCATION, null), p.get(DATABASE_TYPE_KEY, null), p.get(USERNAME_KEY, null),
                        p.get(PASSWORD_KEY, null));
                connectionDefinitions.put(name, connectionDefinition);
            } else {
                p.removeNode();
            }
        }
    } catch (BackingStoreException e) {
        // The system preferences system is not working - log this as a message and use defaults
        log.warn("Could not access the user prefererences while loading the "
                + "JNDI connection information - using default JNDI connection entries", e);
    } catch (final Exception e) {
        log.warn("Configuration information was invalid.", e);
    }

    // If the connectionDefinitions is empty, add any default entries
    if (connectionDefinitions.isEmpty() && DATASOURCE_PREFERENCES_NODE.equals(node)) {
        if (userPreferences.getBoolean("sample-data-created", false) == true) {
            // only create the sample connections once, if we work on a totally fresh config.
            return;
        }
        updateSourceList(SAMPLE_DATA_JNDI_SOURCE);
        updateSourceList(SAMPLE_DATA_DRIVER_SOURCE);
        updateSourceList(SAMPLE_DATA_MEMORY_SOURCE);
        updateSourceList(LOCAL_SAMPLE_DATA_DRIVER_SOURCE);
        updateSourceList(MYSQL_SAMPLE_DATA_DRIVER_SOURCE);
        userPreferences.putBoolean("sample-data-created", true);
        try {
            userPreferences.flush();
        } catch (BackingStoreException e) {
            // ignored ..
        }
    }
}

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 preferences the Preferences to resolve against
 * @return the value for the placeholder, or <code>null</code> if none found
 */// w  w  w .j a v  a2s.  c  om
protected String getPreferenceValue(final String path, final String key, final Preferences preferences) {
    if (path != null) {
        // Do not create the node if it does not exist...
        try {
            if (preferences.nodeExists(path)) {
                return preferences.node(path).get(key, null);
            }
            return null;
        } catch (final BackingStoreException e) {
            throw new RuntimeException("Cannot access specified node path [" + path + "]", e);
        }
    }
    return preferences.get(key, null);
}

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  w w w. ja va2  s.c o  m*/
}