Example usage for java.util.prefs Preferences keys

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

Introduction

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

Prototype

public abstract String[] keys() throws BackingStoreException;

Source Link

Document

Returns all of the keys that have an associated value in this preference node.

Usage

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

public Properties getAgentConfiguration() {
    try {//from w w  w  .j av a 2  s .  c om
        Properties properties = new Properties();
        Preferences prefs = getPreferencesNode();

        for (String key : prefs.keys()) {
            properties.setProperty(key, prefs.get(key, "?"));
        }

        return properties;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

private History loadHistory() {
    jline.History jHistory = new jline.History();
    try {//w  w  w  . j a va 2  s  .co m
        Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
        String[] historyKeys = prefs.keys();
        Arrays.sort(historyKeys, 0, historyKeys.length);
        for (int i = 0; i < historyKeys.length; i++) {
            String key = historyKeys[i];
            String value = prefs.get(key, ""); //$NON-NLS-1$
            jHistory.addToHistory(value);
        }
    } catch (Exception e) {
        // unable to load prefs: do nothing
    }
    return jHistory;
}

From source file:com.adito.jdbc.DBUpgrader.java

/**
 * Check the database schema and perform any upgrades.
 * //  ww w  . j  av 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.rhq.server.control.command.AbstractInstall.java

private void clearAgentPreferences() throws Exception {
    log.info("Removing any existing agent preferences from default preference node");

    // remove everything EXCEPT the security token
    Preferences agentPrefs = getAgentPreferences();
    String[] prefKeys = null;/*w  ww. j  a  va 2  s  .  co m*/

    try {
        prefKeys = agentPrefs.keys();
    } catch (Exception e) {
        log.warn("Failed to get agent preferences - cannot clear them: " + e);
    }

    if (prefKeys != null && prefKeys.length > 0) {
        for (String prefKey : prefKeys) {
            if (!prefKey.equals(PREF_RHQ_AGENT_SECURITY_TOKEN)) {
                agentPrefs.remove(prefKey);
            }
        }
        agentPrefs.flush();
        Preferences.userRoot().sync();
    }
}

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

/**
 * Fetches key patterns from preferences.
 * The implementation doesn't cache the results
 *
 * @return LabelPattern containing all keys. Returned LabelPattern has no parent
 *///  w ww. j a  va  2  s.co  m
public GlobalLabelPattern getKeyPattern() {
    keyPattern = new GlobalLabelPattern();
    Preferences pre = Preferences.userNodeForPackage(GlobalLabelPattern.class);
    try {
        String[] keys = pre.keys();
        if (keys.length > 0) {
            for (String key : keys) {
                keyPattern.addLabelPattern(key, pre.get(key, null));
            }
        }
    } catch (BackingStoreException ex) {
        LOGGER.info("BackingStoreException in JabRefPreferences.getKeyPattern", ex);
    }
    return keyPattern;
}

From source file:com.adito.server.DefaultAditoServerFactory.java

void copyNode(Preferences from, Preferences to) throws BackingStoreException {
    String[] keys = from.keys();
    for (String key : keys) {
        to.put(key, from.get(key, ""));
    }/*from  w  w  w.j  a  va  2  s . c  om*/
    String childNodes[] = from.childrenNames();
    for (String childNode : childNodes) {
        Preferences cn = from.node(childNode);
        Preferences tn = to.node(childNode);
        copyNode(cn, tn);
    }
}

From source file:com.adito.server.Main.java

void copyNode(Preferences from, Preferences to) throws BackingStoreException {
    String[] keys = from.keys();
    for (int i = 0; i < keys.length; i++) {
        to.put(keys[i], from.get(keys[i], ""));
    }// www.  j  a v a 2s.  co  m
    String childNodes[] = from.childrenNames();
    for (int i = 0; i < childNodes.length; i++) {
        Preferences cn = from.node(childNodes[i]);
        Preferences tn = to.node(childNodes[i]);
        copyNode(cn, tn);
    }
}