/**
  * Load the configuration from the underlying location.
  *
  * @throws ConfigurationException if loading of the configuration fails
  */
 public void load() throws ConfigurationException {
   if (sourceURL != null) {
     load(sourceURL);
   } else {
     load(getFileName());
   }
 }
  /**
   * Performs a reload operation if necessary. This method is called on each access of this
   * configuration. It asks the associated reloading strategy whether a reload should be performed.
   * If this is the case, the configuration is cleared and loaded again from its source. If this
   * operation causes an exception, the registered error listeners will be notified. The error event
   * passed to the listeners is of type <code>EVENT_RELOAD</code> and contains the exception that
   * caused the event.
   */
  public void reload() {
    synchronized (reloadLock) {
      if (noReload == 0) {
        try {
          enterNoReload(); // avoid reentrant calls

          if (strategy.reloadingRequired()) {
            if (getLogger().isInfoEnabled()) {
              getLogger().info("Reloading configuration. URL is " + getURL());
            }
            fireEvent(EVENT_RELOAD, null, getURL(), true);
            setDetailEvents(false);
            boolean autoSaveBak = this.isAutoSave(); // save the current state
            this.setAutoSave(false); // deactivate autoSave to prevent information loss
            try {
              clear();
              load();
            } finally {
              this.setAutoSave(autoSaveBak); // set autoSave to previous value
              setDetailEvents(true);
            }
            fireEvent(EVENT_RELOAD, null, getURL(), false);

            // notify the strategy
            strategy.reloadingPerformed();
          }
        } catch (Exception e) {
          fireError(EVENT_RELOAD, null, null, e);
          // todo rollback the changes if the file can't be reloaded
        } finally {
          exitNoReload();
        }
      }
    }
  }
  /**
   * Load the configuration from the specified URL. This does not change the source of the
   * configuration (i.e. the internally maintained file name). Use on of the setter methods for this
   * purpose.
   *
   * @param url the URL of the file to be loaded
   * @throws ConfigurationException if an error occurs
   */
  public void load(URL url) throws ConfigurationException {
    if (sourceURL == null) {
      if (StringUtils.isEmpty(getBasePath())) {
        // ensure that we have a valid base path
        setBasePath(url.toString());
      }
      sourceURL = url;
    }

    // throw an exception if the target URL is a directory
    File file = ConfigurationUtils.fileFromURL(url);
    if (file != null && file.isDirectory()) {
      throw new ConfigurationException("Cannot load a configuration from a directory");
    }

    InputStream in = null;

    try {
      in = url.openStream();
      load(in);
    } catch (ConfigurationException e) {
      throw e;
    } catch (Exception e) {
      throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
    } finally {
      // close the input stream
      try {
        if (in != null) {
          in.close();
        }
      } catch (IOException e) {
        getLogger().warn("Could not close input stream", e);
      }
    }
  }
 /**
  * Load the configuration from the specified file. This does not change the source of the
  * configuration (i.e. the internally maintained file name). Use one of the setter methods for
  * this purpose.
  *
  * @param file the file to load
  * @throws ConfigurationException if an error occurs
  */
 public void load(File file) throws ConfigurationException {
   try {
     load(ConfigurationUtils.toURL(file));
   } catch (ConfigurationException e) {
     throw e;
   } catch (Exception e) {
     throw new ConfigurationException("Unable to load the configuration file " + file, e);
   }
 }
  /**
   * Creates and loads the configuration from the specified URL.
   *
   * @param url The location of the file to load.
   * @throws ConfigurationException Error while loading the file
   * @since 1.1
   */
  public AbstractFileConfiguration(URL url) throws ConfigurationException {
    this();

    // set the URL and update the base path and the file name
    setURL(url);

    // load the file
    load();
  }
  /**
   * Creates and loads the configuration from the specified file. The passed in string must be a
   * valid file name, either absolute or relativ.
   *
   * @param fileName The name of the file to load.
   * @throws ConfigurationException Error while loading the file
   * @since 1.1
   */
  public AbstractFileConfiguration(String fileName) throws ConfigurationException {
    this();

    // store the file name
    setFileName(fileName);

    // load the file
    load();
  }
  /**
   * Creates and loads the configuration from the specified file.
   *
   * @param file The file to load.
   * @throws ConfigurationException Error while loading the file
   * @since 1.1
   */
  public AbstractFileConfiguration(File file) throws ConfigurationException {
    this();

    // set the file and update the url, the base path and the file name
    setFile(file);

    // load the file
    if (file.exists()) {
      load();
    }
  }
  /**
   * Locate the specified file and load the configuration. This does not change the source of the
   * configuration (i.e. the internally maintained file name). Use one of the setter methods for
   * this purpose.
   *
   * @param fileName the name of the file to be loaded
   * @throws ConfigurationException if an error occurs
   */
  public void load(String fileName) throws ConfigurationException {
    try {
      URL url = ConfigurationUtils.locate(basePath, fileName);

      if (url == null) {
        throw new ConfigurationException("Cannot locate configuration source " + fileName);
      }
      load(url);
    } catch (ConfigurationException e) {
      throw e;
    } catch (Exception e) {
      throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
    }
  }
  /**
   * Load the configuration from the specified stream, using the specified encoding. If the encoding
   * is null the default encoding is used.
   *
   * @param in the input stream
   * @param encoding the encoding used. <code>null</code> to use the default encoding
   * @throws ConfigurationException if an error occurs during the load operation
   */
  public void load(InputStream in, String encoding) throws ConfigurationException {
    Reader reader = null;

    if (encoding != null) {
      try {
        reader = new InputStreamReader(in, encoding);
      } catch (UnsupportedEncodingException e) {
        throw new ConfigurationException(
            "The requested encoding is not supported, try the default encoding.", e);
      }
    }

    if (reader == null) {
      reader = new InputStreamReader(in);
    }

    load(reader);
  }
 /**
  * Load the configuration from the specified stream, using the encoding returned by {@link
  * #getEncoding()}.
  *
  * @param in the input stream
  * @throws ConfigurationException if an error occurs during the load operation
  */
 public void load(InputStream in) throws ConfigurationException {
   load(in, getEncoding());
 }