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:Main.java

public static void main(String[] argv) throws Exception {
    boolean exists = Preferences.userRoot().nodeExists("/yourValue"); // false

    Preferences.userRoot().node("/yourValue");

    exists = Preferences.userRoot().nodeExists("/yourValue"); // true

    Preferences prefs = Preferences.userRoot().node("/yourValue");
    prefs.removeNode();//from   w w  w. j  a va2 s.  co  m

    // exists = prefs.nodeExists("/yourValue");

    exists = prefs.nodeExists(""); // false
}

From source file:PrefsUtil.java

/**
 * Clear all the node//from   w  w  w .jav a 2 s  . c  o  m
 */
public static void clear(Preferences preferences, String key) {
    try {
        if (preferences.nodeExists(key)) {
            preferences.node(key).clear();
        }
    } catch (BackingStoreException bse) {
        bse.printStackTrace();
    }
}

From source file:PrefsUtil.java

/**
 * Remove the node//from  w w  w  .  j a  v  a 2  s.co  m
 */
public static void remove(Preferences preferences, String key) {
    try {
        if (preferences.nodeExists(key)) {
            preferences.node(key).removeNode();
        }
    } catch (BackingStoreException bse) {
        bse.printStackTrace();
    }
}

From source file:ec.nbdemetra.ui.Installer.java

public static void loadConfig(Collection<?> list, Preferences root) {
    Parsers.Parser<Config> parser = Config.xmlParser();
    for (IConfigurable o : Iterables.filter(list, IConfigurable.class)) {
        Config current = o.getConfig();/*from  w  w  w  .j  a  v a 2  s. c  o m*/
        try {
            if (root.nodeExists(current.getDomain())) {
                Preferences domain = root.node(current.getDomain());
                Optional<Config> config = InstallerStep.tryGet(domain, current.getName(), parser);
                if (config.isPresent()) {
                    o.setConfig(config.get());
                }
            }
        } catch (BackingStoreException ex) {
            // do nothing?
        }
    }
}

From source file:org.keyboardplaying.xtt.configuration.PreferencesHelper.java

/** Creates a new instance. */
public PreferencesHelper() {
    Preferences prefRoot = Preferences.userRoot();
    try {// w w  w .j av a2 s .  c  o m
        initialized = prefRoot.nodeExists(NODE);
    } catch (BackingStoreException e) {
        LOG.error("The preferences store could not be loaded. Preferences will not be saved.", e);
        initialized = false;
    }
    preferences = prefRoot.node(NODE);
}

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

/**
 * Load configuration with the given name
 * //w  w  w.  j  av a 2 s.  c o 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 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 w w  w . j a  v a2  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 WMS configuration"); //$NON-NLS-1$
        return false;
    }
}

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

/**
 * Save the configuration//from   w w  w .  j a v  a 2 s . com
 * 
 * @param overwrite if old settings/servers with the same name shall be
 *            overridden
 */
public void save(boolean overwrite) {
    Preferences preferences = getPreferences();

    try {
        String name = getName();

        if (!overwrite) {
            int i = 1;
            // find unique name
            while (preferences.nodeExists(name)) {
                name = getName() + "_" + i; //$NON-NLS-1$
                i++;
            }
        }

        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:com.adito.jdbc.DBUpgrader.java

/**
 * Check the database schema and perform any upgrades.
 * //from w w  w  .  j  a  v a2 s.  c o m
 * @throws Exception on any error
 */
public void upgrade() throws Exception {
    Properties versions = null;
    if (versionsFile == null) {
        /* If required, convert from the old preferences node to the new
         * file (version 0.2.5)
         */
        versionsFile = new File(ContextHolder.getContext().getDBDirectory(), "versions.log");
        Preferences p = ContextHolder.getContext().getPreferences().node("dbupgrader");
        if (p.nodeExists("currentDataVersion")) {
            log.warn("Migrating database versions from preferences to properties file in "
                    + ContextHolder.getContext().getDBDirectory().getAbsolutePath() + ".");
            versions = new Properties();
            p = p.node("currentDataVersion");
            String[] c = p.keys();
            for (int i = 0; i < c.length; i++) {
                versions.put(c[i], p.get(c[i], ""));
            }
            FileOutputStream fos = new FileOutputStream(versionsFile);
            try {
                versions.store(fos, "Database versions");
            } finally {
                Util.closeStream(fos);
            }
            p.removeNode();
        }
    }

    // Load the database versions
    if (versions == null) {
        versions = new Properties();
        if (versionsFile.exists()) {
            FileInputStream fin = new FileInputStream(versionsFile);
            try {
                versions.load(fin);
            } finally {
                Util.closeStream(fin);
            }
        }
    }

    try {
        String dbCheckName = useDbNameForVersionCheck ? engine.getDatabase() : engine.getAlias();

        if ((!engine.isDatabaseExists() || removed.containsKey(engine.getDatabase()))
                && !removeProcessed.containsKey(dbCheckName)) {
            versions.remove(dbCheckName);
            removeProcessed.put(dbCheckName, Boolean.TRUE);
            if (log.isInfoEnabled())
                log.info("Database for " + dbCheckName + " (" + engine.getDatabase()
                        + ") has been removed, assuming this is a re-install.");
            removed.put(engine.getDatabase(), Boolean.TRUE);
        }

        // Check for any SQL scripts to run to bring the databases up to
        // date
        VersionInfo.Version currentDataVersion = new VersionInfo.Version(
                versions.getProperty(dbCheckName, "0.0.0"));
        if (log.isInfoEnabled()) {
            log.info("New logical database version for " + engine.getAlias() + " is " + newDbVersion);
            log.info("Current logical database version for " + engine.getAlias() + " is " + currentDataVersion);
            //
            log.info("Upgrade script directory is " + upgradeDir.getAbsolutePath());
        }
        List upgrades = getSortedUpgrades(upgradeDir);
        File oldLog = new File(upgradeDir, "upgrade.log");
        if (!dbDir.exists()) {
            if (!dbDir.mkdirs()) {
                throw new Exception("Failed to create database directory " + dbDir.getAbsolutePath());
            }
        }
        File logFile = new File(dbDir, "upgrade.log");
        if (oldLog.exists()) {
            if (log.isInfoEnabled())
                log.info(
                        "Moving upgrade.log to new location (as of version 0.1.5 it resides in the db directory.");
            if (!oldLog.renameTo(logFile)) {
                throw new Exception("Failed to move upgrade log file from " + oldLog.getAbsolutePath() + " to "
                        + logFile.getAbsolutePath());
            }
        }
        HashMap completedUpgrades = new HashMap();
        if (!logFile.exists()) {
            OutputStream out = null;
            try {
                out = new FileOutputStream(logFile);
                PrintWriter writer = new PrintWriter(out, true);
                writer.println("# This file contains a list of database upgrades");
                writer.println("# that have completed correctly.");
            } finally {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            }
        } else {
            InputStream in = null;
            try {
                in = new FileInputStream(logFile);
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (!line.equals("") && !line.startsWith("#")) {
                        completedUpgrades.put(line, line);
                    }
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(logFile, true);
            PrintWriter writer = new PrintWriter(out, true);
            Class.forName("org.hsqldb.jdbcDriver"); // shouldnt be needed,
            // but
            // just in
            // case
            for (Iterator i = upgrades.iterator(); i.hasNext();) {
                DBUpgradeOp upgrade = (DBUpgradeOp) i.next();
                boolean runBefore = completedUpgrades.containsKey(upgrade.getFile().getName());
                if (log.isInfoEnabled())
                    log.info("Checking if upgrade " + upgrade.getFile() + " [" + upgrade.getVersion()
                            + "] needs to be run. Run before = " + runBefore + ". Current data version = "
                            + currentDataVersion + ", upgrade version = " + upgrade.getVersion());
                if ((!runBefore || (currentDataVersion.getMajor() == 0 && currentDataVersion.getMinor() == 0
                        && currentDataVersion.getBuild() == 0))
                        && upgrade.getVersion().compareTo(currentDataVersion) >= 0
                        && upgrade.getVersion().compareTo(newDbVersion) < 0) {
                    if (log.isInfoEnabled())
                        log.info("Running script " + upgrade.getName() + " [" + upgrade.getVersion()
                                + "] on database " + engine.getDatabase());

                    // Get a JDBC connection
                    JDBCConnectionImpl conx = engine.aquireConnection();
                    try {
                        runSQLScript(conx, upgrade.getFile());
                        completedUpgrades.put(upgrade.getFile().getName(), upgrade.getFile().getName());
                        writer.println(upgrade.getFile().getName());
                    } finally {
                        engine.releaseConnection(conx);
                    }
                }
            }
            versions.put(dbCheckName, newDbVersion.toString());

            if (log.isInfoEnabled())
                log.info("Logical database " + engine.getAlias() + " (" + engine.getDatabase()
                        + ") is now at version " + newDbVersion);
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    } finally {
        FileOutputStream fos = new FileOutputStream(versionsFile);
        try {
            versions.store(fos, "Database versions");
        } finally {
            Util.closeStream(fos);
        }
    }
}

From source file:org.multicore_association.measure.mem.generate.MemCodeGen.java

/**
 * Check of the section presence.//from w  w  w.j  a v a2 s. c o  m
 * @param prefs read configulation data
 * @param section section name
 * @return Flag of presense
 */
private boolean checkPrefSection(Preferences prefs, String section) {
    boolean ret = false;
    try {
        ret = prefs.nodeExists(section);
    } catch (Exception e) {
        ret = false;
    }
    return ret;
}