List of usage examples for java.util.prefs Preferences importPreferences
public static void importPreferences(InputStream is) throws IOException, InvalidPreferencesFormatException
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create an input stream on a file InputStream is = new BufferedInputStream(new FileInputStream("output.xml")); // Import preference data Preferences.importPreferences(is); }
From source file:de.tbuchloh.kiskis.KisKis.java
private static void initPreferences() { final File p = KisKis.PREF_FILE; if (p.exists()) { try {/*ww w . java 2 s. c o m*/ LOG.debug("importing preferences from " + p); Preferences.importPreferences(new FileInputStream(p)); } catch (final Exception e) { LOG.error("The preference file " + p + " could not be imported!", e); } } }
From source file:PreferencesTest.java
public PreferencesFrame() { // get position, size, title from preferences Preferences root = Preferences.userRoot(); final Preferences node = root.node("/com/horstmann/corejava"); int left = node.getInt("left", 0); int top = node.getInt("top", 0); int width = node.getInt("width", DEFAULT_WIDTH); int height = node.getInt("height", DEFAULT_HEIGHT); setBounds(left, top, width, height); // if no title given, ask user String title = node.get("title", ""); if (title.equals("")) title = JOptionPane.showInputDialog("Please supply a frame title:"); if (title == null) title = ""; setTitle(title);/* w ww. ja v a 2 s . com*/ // set up file chooser that shows XML files final JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); // accept all files ending with .xml chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory(); } public String getDescription() { return "XML files"; } }); // set up menus JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem exportItem = new JMenuItem("Export preferences"); menu.add(exportItem); exportItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) { try { OutputStream out = new FileOutputStream(chooser.getSelectedFile()); node.exportSubtree(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } } }); JMenuItem importItem = new JMenuItem("Import preferences"); menu.add(importItem); importItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) { try { InputStream in = new FileInputStream(chooser.getSelectedFile()); Preferences.importPreferences(in); in.close(); } catch (Exception e) { e.printStackTrace(); } } } }); JMenuItem exitItem = new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { node.putInt("left", getX()); node.putInt("top", getY()); node.putInt("width", getWidth()); node.putInt("height", getHeight()); node.put("title", getTitle()); System.exit(0); } }); }
From source file:com.symbian.utils.config.ConfigUtils.java
/** * load the Configuration settings from files. * //from w ww . j a va 2 s.c o m * @param aPrefFile * The Java Preference file to import/load. * * @throws IOException * If the load doesn't work. */ public void importConfig(final File aPrefFile) throws IOException { try { Preferences.importPreferences(new FileInputStream(aPrefFile)); iPrefrences = Preferences.userRoot().node(iNodeName); } catch (FileNotFoundException lFileNotFoundException) { throw new IOException("Cannot create new properties file: " + aPrefFile.getPath() + ": " + lFileNotFoundException.getMessage()); } catch (IOException lIOException) { throw new IOException( "Properties file I/O error: " + aPrefFile.getPath() + ", " + lIOException.getMessage()); } catch (InvalidPreferencesFormatException lIPFE) { throw new IOException( "Prefrences files are not valid: " + aPrefFile.getPath() + ", " + lIPFE.getMessage()); } }
From source file:net.sf.jabref.JabRefPreferences.java
/** * Imports Preferences from an XML file. * * @param filename String File to import from * @throws JabRefException thrown if importing the preferences failed due to an InvalidPreferencesFormatException * or an IOException *///from w w w .ja v a2 s . c om public void importPreferences(String filename) throws JabRefException { File f = new File(filename); try (InputStream is = new FileInputStream(f)) { Preferences.importPreferences(is); } catch (InvalidPreferencesFormatException | IOException ex) { throw new JabRefException("Could not import preferences", Localization.lang("Could not import preferences"), ex); } }
From source file:org.rhq.enterprise.server.agent.EmbeddedAgentBootstrapService.java
/** * Loads the {@link #getConfigurationFile() configuration file}. The file location will first be checked for * existence on the file system and then as a URL. If it cannot be found, it will be assumed the file location * specifies the file as found in the current class loader and the file will be searched there. An exception is * thrown if the file cannot be found anywhere. * * @return the configuration that was loaded * * @throws IOException if failed to load the configuration file * @throws InvalidPreferencesFormatException if the configuration file had an invalid format * @throws BackingStoreException if failed to access the preferences persistence store * @throws Exception on other failures *//*from w ww . j av a 2s. co m*/ private AgentConfiguration loadConfigurationFile() throws Exception { String file_name = getConfigurationFile(); String preferences_node_name = getPreferencesNodeName(); InputStream config_file_input_stream = null; // first see if the file was specified as a path on the local file system try { File config_file = new File(file_name); if (config_file.exists()) { config_file_input_stream = new FileInputStream(config_file); } } catch (Exception e) { // isn't really an error - this just isn't a file on the local file system } // see if the file was specified as a URL if (config_file_input_stream == null) { try { URL config_file = new URL(file_name); config_file_input_stream = config_file.openStream(); } catch (Exception e) { // isn't really an error - this just isn't a URL } } // if neither a file path or URL, assume the config file can be found in the classloader if (config_file_input_stream == null) { config_file_input_stream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(file_name); } if (config_file_input_stream == null) { throw new IOException("Bad config file: " + file_name); } // We need to clear out any previous configuration in case the current config file doesn't specify a preference // that already exists in the preferences node. In this case, the configuration file wants to fall back on the // default value and if we don't clear the preferences, we aren't guaranteed the value stored in the backing // store is the default value. // But first we need to backup these original preferences in case the config file fails to load - // we'll restore the original values in that case. Preferences preferences_node = getPreferencesNode(); ByteArrayOutputStream backup = new ByteArrayOutputStream(); preferences_node.exportSubtree(backup); preferences_node.clear(); // now load in the preferences try { ByteArrayOutputStream raw_config_file = new ByteArrayOutputStream(); StreamUtil.copy(config_file_input_stream, raw_config_file, true); String new_config = StringPropertyReplacer.replaceProperties(raw_config_file.toString()); ByteArrayInputStream new_config_input_stream = new ByteArrayInputStream(new_config.getBytes()); Preferences.importPreferences(new_config_input_stream); if (new AgentConfiguration(preferences_node).getAgentConfigurationVersion() == 0) { throw new IllegalArgumentException("Bad node name: " + preferences_node_name); } } catch (Exception e) { // a problem occurred importing the config file; let's restore our original values try { Preferences.importPreferences(new ByteArrayInputStream(backup.toByteArray())); } catch (Exception e1) { // its conceivable the same problem occurred here as with the original exception (backing store problem?) // let's throw the original exception, not this one } throw e; } AgentConfiguration agent_configuration = new AgentConfiguration(preferences_node); return agent_configuration; }
From source file:org.rhq.server.control.command.AbstractInstall.java
private void configureAgent(File agentBasedir, CommandLine commandLine) throws Exception { // If the user provided us with an agent config file, we will use it. // Otherwise, we are going to use the out-of-box agent config file. ///*from w w w .j a v a2 s. c o m*/ // Because we want to accept all defaults and consider the agent fully configured, we need to set // rhq.agent.configuration-setup-flag=true // This tells the agent not to ask any setup questions at startup. // We do this whether using a custom config file or the default config file - this is because // we cannot allow the agent to ask the setup questions (rhqctl doesn't support that). // // Note that agent preferences found in the config file can be overridden with // the AGENT_PREFERENCE settings (you can set more than one). try { File agentConfDir = new File(agentBasedir, "conf"); File agentConfigFile = new File(agentConfDir, "agent-configuration.xml"); if (commandLine.hasOption(AGENT_CONFIG_OPTION)) { log.info("Configuring the RHQ agent with custom configuration file: " + commandLine.getOptionValue(AGENT_CONFIG_OPTION)); replaceAgentConfigIfNecessary(commandLine); } else { log.info("Configuring the RHQ agent with default configuration file: " + agentConfigFile); } // we require our agent preference node to be the user node called "default" Preferences preferencesNode = getAgentPreferences(); // read the comments in AgentMain.loadConfigurationFile(String) to know why we do all of this String securityToken = preferencesNode.get(PREF_RHQ_AGENT_SECURITY_TOKEN, null); ByteArrayOutputStream rawConfigFileData = new ByteArrayOutputStream(); StreamUtil.copy(new FileInputStream(agentConfigFile), rawConfigFileData, true); String newConfig = rawConfigFileData.toString().replace("${rhq.agent.preferences-node}", "default"); ByteArrayInputStream newConfigInputStream = new ByteArrayInputStream(newConfig.getBytes()); Preferences.importPreferences(newConfigInputStream); if (securityToken != null) { preferencesNode.put(PREF_RHQ_AGENT_SECURITY_TOKEN, securityToken); } // get the configured server endpoint information and tell the agent so it knows where the server is. Properties serverEndpoint = getAgentServerEndpoint(); String endpointTransport = serverEndpoint.getProperty(PREF_RHQ_AGENT_SERVER_TRANSPORT); String endpointAddress = serverEndpoint.getProperty(PREF_RHQ_AGENT_SERVER_BINDADDRESS); String endpointPort = serverEndpoint.getProperty(PREF_RHQ_AGENT_SERVER_BINDPORT); String endpointParams = serverEndpoint.getProperty(PREF_RHQ_AGENT_SERVER_TRANSPORTPARAMS); if (endpointTransport != null) { preferencesNode.put(PREF_RHQ_AGENT_SERVER_TRANSPORT, endpointTransport); } if (endpointAddress != null) { preferencesNode.put(PREF_RHQ_AGENT_SERVER_BINDADDRESS, endpointAddress); } if (endpointPort != null) { preferencesNode.put(PREF_RHQ_AGENT_SERVER_BINDPORT, endpointPort); } if (endpointParams != null) { preferencesNode.put(PREF_RHQ_AGENT_SERVER_TRANSPORTPARAMS, endpointParams); } // if the user provided any overrides to the agent config, use them. overrideAgentPreferences(commandLine, preferencesNode); // set some prefs that must be a specific value // - do not tell this agent to auto-update itself - this agent must be managed by rhqctl only // - set the config setup flag to true to prohibit the agent from asking setup questions at startup String agentUpdateEnabledPref = PREF_RHQ_AGENT_AUTO_UPDATE_FLAG; preferencesNode.putBoolean(agentUpdateEnabledPref, false); String setupPref = PREF_RHQ_AGENT_CONFIGURATION_SETUP_FLAG; preferencesNode.putBoolean(setupPref, true); try { preferencesNode.flush(); preferencesNode.sync(); } catch (BackingStoreException bse) { log.error("Failed to store agent preferences, for Linux systems we require writable user.home [" + System.getProperty("user.home") + "]. You can also set different location for agent preferences by setting \"-Djava.util.prefs.userRoot=/some/path/\"" + " java system property. You may need to put this property to RHQ_CONTROL_ADDIDIONAL_JAVA_OPTS and RHQ_AGENT_ADDIDIONAL_JAVA_OPTS env variables."); throw bse; } log.info("Finished configuring the agent"); } catch (Exception e) { log.error("An error occurred while configuring the agent: " + e.getMessage()); throw e; } }