コード例 #1
0
  /**
   * 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);
    }
  }
コード例 #2
0
  /**
   * 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);
      }
    }
  }