/** * Save the configuration. Before this method can be called a valid file name must have been set. * * @throws ConfigurationException if an error occurs or no file name has been set yet */ public void save() throws ConfigurationException { if (getFileName() == null) { throw new ConfigurationException("No file name has been set!"); } if (sourceURL != null) { save(sourceURL); } else { save(fileName); } strategy.init(); }
/** Save the configuration if the automatic persistence is enabled and if a file is specified. */ protected void possiblySave() { if (autoSave && fileName != null) { try { save(); } catch (ConfigurationException e) { throw new ConfigurationRuntimeException("Failed to auto-save", e); } } }
/** * Save the configuration to the specified file. The file is created automatically if it doesn't * exist. This doesn't change the source of the configuration, use {@link #setFile} if you need * it. * * @param file the target file * @throws ConfigurationException if an error occurs during the save operation */ public void save(File file) throws ConfigurationException { OutputStream out = null; try { // create the file if necessary createPath(file); out = new FileOutputStream(file); save(out); } catch (IOException e) { throw new ConfigurationException("Unable to save the configuration to the file " + file, e); } finally { closeSilent(out); } }
/** * Save the configuration to the specified file. This doesn't change the source of the * configuration, use setFileName() if you need it. * * @param fileName the file name * @throws ConfigurationException if an error occurs during the save operation */ public void save(String fileName) throws ConfigurationException { try { File file = ConfigurationUtils.getFile(basePath, fileName); if (file == null) { throw new ConfigurationException("Invalid file name for save: " + fileName); } save(file); } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException( "Unable to save the configuration to the file " + fileName, e); } }
/** * Save the configuration to the specified URL. This doesn't change the source of the * configuration, use setURL() if you need it. * * @param url the URL * @throws ConfigurationException if an error occurs during the save operation */ public void save(URL url) throws ConfigurationException { // file URLs have to be converted to Files since FileURLConnection is // read only (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4191800) File file = ConfigurationUtils.fileFromURL(url); if (file != null) { save(file); } else { // for non file URLs save through an URLConnection OutputStream out = null; try { URLConnection connection = url.openConnection(); connection.setDoOutput(true); // use the PUT method for http URLs if (connection instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) connection; conn.setRequestMethod("PUT"); } out = connection.getOutputStream(); save(out); // check the response code for http URLs and throw an exception if an error occured if (connection instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) connection; if (conn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new IOException( "HTTP Error " + conn.getResponseCode() + " " + conn.getResponseMessage()); } } } catch (IOException e) { throw new ConfigurationException("Could not save to URL " + url, e); } finally { closeSilent(out); } } }
/** * Save the configuration to the specified stream, using the specified encoding. If the encoding * is null the default encoding is used. * * @param out the output stream * @param encoding the encoding to use * @throws ConfigurationException if an error occurs during the save operation */ public void save(OutputStream out, String encoding) throws ConfigurationException { Writer writer = null; if (encoding != null) { try { writer = new OutputStreamWriter(out, encoding); } catch (UnsupportedEncodingException e) { throw new ConfigurationException( "The requested encoding is not supported, try the default encoding.", e); } } if (writer == null) { writer = new OutputStreamWriter(out); } save(writer); }
/** * Save the configuration to the specified stream, using the encoding returned by {@link * #getEncoding()}. * * @param out the output stream * @throws ConfigurationException if an error occurs during the save operation */ public void save(OutputStream out) throws ConfigurationException { save(out, getEncoding()); }