Example usage for java.util.prefs Preferences systemRoot

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

Introduction

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

Prototype

public static Preferences systemRoot() 

Source Link

Document

Returns the root preference node for the system.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Get the system root
    Preferences prefs = Preferences.systemRoot();

    // Get the user root
    prefs = Preferences.userRoot();

    // The name of a root is ""
    String name = prefs.name();/*from  www. j  av  a 2s. com*/

    // The parent of a root is null
    Preferences parent = prefs.parent();

    // The absolute path of a root is "/"
    String path = prefs.absolutePath();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // System preference nodes

    // Use a Class
    Preferences prefs = Preferences.systemNodeForPackage(java.lang.String.class);

    // Use an absolute path
    prefs = Preferences.systemRoot().node("/java/lang/String");

    // Use a relative path
    prefs = Preferences.systemRoot().node("/javax/swing");
    prefs = prefs.node("text/html");

    // User preference nodes

    // Use a class
    prefs = Preferences.userNodeForPackage(Main.class);

    // Use an absolute path
    prefs = Preferences.userRoot().node("/com/mycompany");

    // Use a relative path
    prefs = Preferences.userRoot().node("/javax/swing");
    prefs = prefs.node("text/html");
}

From source file:Main.java

public static void main(String[] args) {
    Preferences p = Preferences.userRoot();
    p.put(REALKEY, "bar");

    System.out.println(p);//from  w  w  w. j  a v  a2  s  . co m
    System.out.println(p.get(REALKEY, "key"));

    p = Preferences.systemRoot();
    p.put(REALKEY, "key 2");

    System.out.println(p);
    System.out.println(p.get(REALKEY, "default"));
}

From source file:Main.java

public static void main(String[] args) {
    String PREF_KEY = "org.username";
    // Write Preferences information to HKCU (HKEY_CURRENT_USER),HKCU\Software\JavaSoft\Prefs\
    Preferences userPref = Preferences.userRoot();
    userPref.put(PREF_KEY, "a");

    System.out.println("Preferences = " + userPref.get(PREF_KEY, PREF_KEY + " was not found."));

    // Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),HKLM\Software\JavaSoft\Prefs\
    Preferences systemPref = Preferences.systemRoot();
    systemPref.put(PREF_KEY, "b");

    System.out.println("Preferences = " + systemPref.get(PREF_KEY, PREF_KEY + " was not found."));
}

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

public Integer start(String[] args) {
    startupStarted = System.currentTimeMillis();

    // Inform the wrapper the startup process may take a while
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }/*from   w  ww.  j a  v  a2s  .  co m*/

    // Parse the command line
    Integer returnCode = parseCommandLine(args);
    if (returnCode != null) {
        if (returnCode.intValue() == 999) {
            return null;
        }
        return returnCode;
    }

    // Create the boot progress monitor
    if (gui) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        bootProgressMonitor = new SwingBootProgressMonitor();
    } else {
        bootProgressMonitor = new LogBootProgressMonitor();
    }

    //
    resourceCaches = new HashMap<URL, ResourceCache>();
    contextListeners = new ArrayList<ContextListener>();

    loadSystemProperties();
    initialiseLogging();

    /*
     * Migrate preferences.
     */
    File newPrefDir = new File(ContextHolder.getContext().getConfDirectory(), "prefs");
    PREF = PropertyPreferences.SYSTEM_ROOT;
    try {
        if (!newPrefDir.exists() && Preferences.systemRoot().node("/com").nodeExists("adito")) {
            Preferences from = Preferences.systemRoot().node("/com/adito");
            log.warn("Migrating preferences");
            try {
                copyNode(from.node("core"), PREF.node("core"));
                from.node("core").removeNode();
                copyNode(from.node("plugin"), PREF.node("plugin"));
                from.node("plugin").removeNode();
                copyNode(from.node("extensions"), PREF.node("extensions"));
                from.node("extensions").removeNode();
                copyNode(from.node("dbupgrader"), PREF.node("dbupgrader"));
                from.node("dbupgrader").removeNode();
            } catch (Exception e) {
                log.error("Failed to migrate preferences.", e);
            }
            try {
                from.flush();
            } catch (BackingStoreException bse) {
                log.error("Failed to flush old preferences");
            }
            try {
                PREF.flush();
            } catch (BackingStoreException bse) {
                log.error("Failed to flush new preferences");
            }
            if (log.isInfoEnabled()) {
                log.info("Flushing preferences");
            }

        }
    } catch (BackingStoreException bse) {
        log.error("Failed to migrate preferences.", bse);
    }

    // Inform the wrapper the startup process is going ok
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }

    try {
        clearTemp();
        try {
            hostname = Inet4Address.getLocalHost().getCanonicalHostName();
            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        } catch (Exception ex) {
            // This should be fatal, we now rely on the hostname being
            // available
            throw new Exception("The host name or address on which this service is running could not "
                    + "be determined. Check you network configuration. One possible cause is "
                    + "a misconfigured 'hosts' file (e.g. on UNIX-like systems this would be "
                    + "/etc/hosts, on Windows XP it would be "
                    + "C:\\Windows\\System32\\Drivers\\Etc\\Hosts).");
        }

        PropertyClassManager.getInstance()
                .registerPropertyClass(contextConfiguration = new ContextConfig(getClass().getClassLoader()));

        // Display some information about the system we are running on
        displaySystemInfo();

        // Load the context property definitions
        loadContextProperties();

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        // Configure any HTTP / HTTPS / SOCKS proxy servers
        configureProxyServers();

        PropertyList l = contextConfiguration.retrievePropertyList(new ContextKey("webServer.bindAddress"));
        getBootProgressMonitor().updateMessage("Creating server lock");
        getBootProgressMonitor().updateProgress(6);
        serverLock = new ServerLock((String) l.get(0));
        if (serverLock.isLocked()) {
            if (!isSetupMode()) {
                if (serverLock.isSetup()) {
                    throw new Exception("The installation wizard is currently running. "
                            + "Please shut this down by pointing your browser " + "to http://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The server is already running.");
                }
            } else {
                if (!serverLock.isSetup()) {
                    throw new Exception("The server is currently already running. "
                            + "Please shut this down by pointing your browser " + "to https://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The installation wizard is running..");
                }

            }

        }

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                serverLock.stop();
            }
        });

        //
        registerKeyStores();

        //
        threadGroup = new ThreadGroup("MainThreadGroup");

        if (install) {
            setupMode();

        } else {
            normalMode();
            startHttpServer();
        }
    } catch (Throwable t) {
        startupException = t;
        log.error("Failed to start the server. " + t.getMessage(), t);
        return new Integer(1);
    }

    return null;
}

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

public Integer start(String[] args) {
    startupStarted = System.currentTimeMillis();

    // Inform the wrapper the startup process may take a while
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }// w ww  .j  a  v  a2  s . c o m

    // Parse the command line
    Integer returnCode = parseCommandLine(args);
    if (returnCode != null) {
        if (returnCode.intValue() == 999) {
            return null;
        }
        return returnCode;
    }

    // Create the boot progress monitor
    if (gui) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        bootProgressMonitor = new SwingBootProgressMonitor();
    } else {
        bootProgressMonitor = new LogBootProgressMonitor();
    }

    //
    resourceCaches = new HashMap<URL, ResourceCache>();
    contextListeners = new ArrayList<ContextListener>();

    loadSystemProperties();
    initialiseLogging();

    /*
     * Migrate preferences.
     */
    File newPrefDir = new File(ContextHolder.getContext().getConfDirectory(), "prefs");
    PREF = PropertyPreferences.SYSTEM_ROOT;
    try {
        if (!newPrefDir.exists() && Preferences.systemRoot().node("/com").nodeExists("sslexplorer")) {
            Preferences from = Preferences.systemRoot().node("/com/sslexplorer");
            log.warn("Migrating preferences");
            try {
                copyNode(from.node("core"), PREF.node("core"));
                from.node("core").removeNode();
                copyNode(from.node("plugin"), PREF.node("plugin"));
                from.node("plugin").removeNode();
                copyNode(from.node("extensions"), PREF.node("extensions"));
                from.node("extensions").removeNode();
                copyNode(from.node("dbupgrader"), PREF.node("dbupgrader"));
                from.node("dbupgrader").removeNode();
            } catch (Exception e) {
                log.error("Failed to migrate preferences.", e);
            }
            try {
                from.flush();
            } catch (BackingStoreException bse) {
                log.error("Failed to flush old preferences");
            }
            try {
                PREF.flush();
            } catch (BackingStoreException bse) {
                log.error("Failed to flush new preferences");
            }
            if (log.isInfoEnabled()) {
                log.info("Flushing preferences");
            }

        }
    } catch (BackingStoreException bse) {
        log.error("Failed to migrate preferences.", bse);
    }

    // Inform the wrapper the startup process is going ok
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }

    try {
        clearTemp();
        try {
            hostname = Inet4Address.getLocalHost().getCanonicalHostName();
            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        } catch (Exception ex) {
            // This should be fatal, we now rely on the hostname being
            // available
            throw new Exception("The host name or address on which this service is running could not "
                    + "be determined. Check you network configuration. One possible cause is "
                    + "a misconfigured 'hosts' file (e.g. on UNIX-like systems this would be "
                    + "/etc/hosts, on Windows XP it would be "
                    + "C:\\Windows\\System32\\Drivers\\Etc\\Hosts).");
        }

        PropertyClassManager.getInstance()
                .registerPropertyClass(contextConfiguration = new ContextConfig(getClass().getClassLoader()));

        // Display some information about the system we are running on
        displaySystemInfo();

        // Load the context property definitions
        loadContextProperties();

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        // Configure any HTTP / HTTPS / SOCKS proxy servers
        configureProxyServers();

        PropertyList l = contextConfiguration.retrievePropertyList(new ContextKey("webServer.bindAddress"));
        getBootProgressMonitor().updateMessage("Creating server lock");
        getBootProgressMonitor().updateProgress(6);
        serverLock = new ServerLock((String) l.get(0));
        if (serverLock.isLocked()) {
            if (!isSetupMode()) {
                if (serverLock.isSetup()) {
                    throw new Exception("The installation wizard is currently running. "
                            + "Please shut this down by pointing your browser " + "to http://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The server is already running.");
                }
            } else {
                if (!serverLock.isSetup()) {
                    throw new Exception("The server is currently already running. "
                            + "Please shut this down by pointing your browser " + "to https://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The installation wizard is running..");
                }

            }

        }

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                serverLock.stop();
            }
        });

        //
        registerKeyStores();

        //
        threadGroup = new ThreadGroup("MainThreadGroup");

        if (install) {
            setupMode();

        } else {
            normalMode();
            startHttpServer();
        }
    } catch (Throwable t) {
        startupException = t;
        log.error("Failed to start the server. " + t.getMessage(), t);
        return new Integer(1);
    }

    return null;
}

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

public Integer start(String[] args) {
    startupStarted = System.currentTimeMillis();

    // Inform the wrapper the startup process may take a while
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }/*ww w .jav  a  2  s.c om*/

    // Parse the command line
    Integer returnCode = parseCommandLine(args);
    if (returnCode != null) {
        if (returnCode == 999) {
            return null;
        }
        return returnCode;
    }

    // Create the boot progress monitor
    if (gui) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (UnsupportedLookAndFeelException e) {
        }
        bootProgressMonitor = new SwingBootProgressMonitor();
    } else {
        bootProgressMonitor = new LogBootProgressMonitor();
    }

    //
    resourceCaches = new HashMap<URL, ResourceCache>();
    contextListeners = new ArrayList<ContextListener>();

    loadSystemProperties();
    initialiseLogging();

    /*
     * Migrate preferences.
     */
    File newPrefDir = new File(ContextHolder.getContext().getConfDirectory(), "prefs");
    PREF = PropertyPreferences.SYSTEM_ROOT;
    try {
        if (!newPrefDir.exists() && Preferences.systemRoot().node("/com").nodeExists("adito")) {
            Preferences from = Preferences.systemRoot().node("/com/adito");
            LOG.warn("Migrating preferences");
            try {
                copyNode(from.node("core"), PREF.node("core"));
                from.node("core").removeNode();
                copyNode(from.node("plugin"), PREF.node("plugin"));
                from.node("plugin").removeNode();
                copyNode(from.node("extensions"), PREF.node("extensions"));
                from.node("extensions").removeNode();
                copyNode(from.node("dbupgrader"), PREF.node("dbupgrader"));
                from.node("dbupgrader").removeNode();
            } catch (BackingStoreException e) {
                LOG.error("Failed to migrate preferences.", e);
            }
            try {
                from.flush();
            } catch (BackingStoreException bse) {
                LOG.error("Failed to flush old preferences");
            }
            try {
                PREF.flush();
            } catch (BackingStoreException bse) {
                LOG.error("Failed to flush new preferences");
            }
            if (LOG.isInfoEnabled()) {
                LOG.info("Flushing preferences");
            }

        }
    } catch (BackingStoreException bse) {
        LOG.error("Failed to migrate preferences.", bse);
    }

    // Inform the wrapper the startup process is going ok
    if (useWrapper) {
        WrapperManager.signalStarting(60000);
    }

    try {
        clearTemp();
        try {
            hostname = Inet4Address.getLocalHost().getCanonicalHostName();
            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException ex) {
            // This should be fatal, we now rely on the hostname being
            // available
            throw new Exception("The host name or address on which this service is running could not "
                    + "be determined. Check you network configuration. One possible cause is "
                    + "a misconfigured 'hosts' file (e.g. on UNIX-like systems this would be "
                    + "/etc/hosts, on Windows XP it would be "
                    + "C:\\Windows\\System32\\Drivers\\Etc\\Hosts).");
        }

        PropertyClassManager.getInstance()
                .registerPropertyClass(contextConfiguration = new ContextConfig(getClass().getClassLoader()));

        // Display some information about the system we are running on
        displaySystemInfo();

        // Load the context property definitions
        loadContextProperties();

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        // Configure any HTTP / HTTPS / SOCKS proxy servers
        configureProxyServers();

        PropertyList l = contextConfiguration.retrievePropertyList(new ContextKey("webServer.bindAddress"));
        getBootProgressMonitor().updateMessage("Creating server lock");
        getBootProgressMonitor().updateProgress(6);
        serverLock = new ServerLock(l.get(0));
        if (serverLock.isLocked()) {
            if (!isSetupMode()) {
                if (serverLock.isSetup()) {
                    throw new Exception("The installation wizard is currently running. "
                            + "Please shut this down by pointing your browser " + "to http://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The server is already running.");
                }
            } else {
                if (!serverLock.isSetup()) {
                    throw new Exception("The server is currently already running. "
                            + "Please shut this down by pointing your browser " + "to https://" + getHostname()
                            + ":" + serverLock.getPort()
                            + "/showShutdown.do before attempting to start the server again.");
                } else {
                    throw new Exception("The installation wizard is running..");
                }

            }

        }

        // Inform the wrapper the startup process is going ok
        if (useWrapper) {
            WrapperManager.signalStarting(60000);
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                serverLock.stop();
            }
        });

        //
        registerKeyStores();

        //
        threadGroup = new ThreadGroup("MainThreadGroup");

        if (install) {
            setupMode();

        } else {
            normalMode();
            startHttpServer();
        }
    } catch (Exception t) {
        startupException = t;
        LOG.error("Failed to start the server. " + t.getMessage(), t);
        return 1;
    }

    return null;
}

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

public int stop(int exitCode) {
    if (log != null) {
        if (log.isInfoEnabled()) {
            if (restarting)
                log.info("Restarting the server.");
            else/*from  w  ww .  j  a  v  a2s .c o  m*/
                log.info("Shutting down the server.");
        }
    }
    if (useWrapper) {
        WrapperManager.signalStopping(20000);
    }

    // TODO This really screws up wrapper on windows - no idea why
    if (server != null && server.isStarted()) {
        try {
            server.stop(false);
        } catch (InterruptedException e) {
            if (log != null) {
                if (log.isInfoEnabled())
                    log.info("Failed to stop server.", e);
            }
        }
    }

    // Inform all context listeners of what is happening
    for (ContextListener l : contextListeners) {
        new StopContextListenerThread(l).waitForStop();
    }

    // 
    if (log.isInfoEnabled()) {
        log.info("Flushing preferences");
    }
    try {
        ContextHolder.getContext().getPreferences().flush();
    } catch (BackingStoreException bse) {
        log.error("Failed to flush context preferences.", bse);
    }
    try {
        Preferences.systemRoot().flush();
    } catch (IllegalStateException ise) {
    } catch (BackingStoreException bse) {
        log.error("Failed to flush system preferences");
    }

    return exitCode;
}

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

public int stop(int exitCode) {
    if (LOG != null) {
        if (LOG.isInfoEnabled()) {
            if (restarting) {
                LOG.info("Restarting the server.");
            } else {
                LOG.info("Shutting down the server.");
            }//from www .ja va 2 s  .  c om
        }
    }
    if (useWrapper) {
        WrapperManager.signalStopping(20000);
    }

    // TODO This really screws up wrapper on windows - no idea why
    if (server != null && server.isStarted()) {
        try {
            server.stop(false);
        } catch (InterruptedException e) {
            if (LOG != null) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("Failed to stop server.", e);
                }
            }
        }
    }

    // Inform all context listeners of what is happening
    for (ContextListener l : contextListeners) {
        new StopContextListenerThread(l).waitForStop();
    }

    // 
    if (LOG.isInfoEnabled()) {
        LOG.info("Flushing preferences");
    }
    try {
        ContextHolder.getContext().getPreferences().flush();
    } catch (BackingStoreException bse) {
        LOG.error("Failed to flush context preferences.", bse);
    }
    try {
        Preferences.systemRoot().flush();
    } catch (IllegalStateException ise) {
    } catch (BackingStoreException bse) {
        LOG.error("Failed to flush system preferences");
    }

    return exitCode;
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Get the site.//from w w w.  j a  v a 2 s.  c  o m
 *
 * @return the site.
 */
public CmsSite getSite() {
    long siteId = Preferences.systemRoot().getLong(PREF_KEY_PROFILE_SITE, 0);
    final CmsSite site = _cmsFrontendDAO.getSite(siteId);
    assert site != null;
    return site;
}