예제 #1
0
  /**
   * Save the preferences state in memory (i.e for the current session) to file.
   *
   * @param comment comment to be included in the preference file.
   * @throws PreferencesException if the preference file could not be written.
   */
  public final void saveToFile(final String comment) throws PreferencesException {
    // Store current Preference object revision number
    setPreference(JMCS_STRUCTURE_VERSION_NUMBER_ID, getJmcsStructureVersionNumber());
    setPreference(PREFERENCES_VERSION_NUMBER_ID, getPreferencesVersionNumber());

    OutputStream outputFile = null;
    try {
      outputFile = new BufferedOutputStream(new FileOutputStream(_fullFilepath));

      _logger.info("Saving '{}' preference file.", _fullFilepath);

      _currentProperties.storeToXML(outputFile, comment);

    } catch (IOException ioe) {
      throw new PreferencesException("Cannot store preferences to file " + _fullFilepath, ioe);
    } finally {
      FileUtils.closeStream(outputFile);
    }
  }
예제 #2
0
  /**
   * Load preferences from file if any, or reset to default values and notify listeners.
   *
   * @warning Any preference value change not yet saved will be LOST.
   */
  public final void loadFromFile() {
    resetToDefaultPreferences(true);

    // Loading preference file
    _logger.info("Loading '{}' preference file.", _fullFilepath);

    InputStream inputFile = null;
    try {
      inputFile = new BufferedInputStream(new FileInputStream(_fullFilepath));
    } catch (FileNotFoundException fnfe) {
      _logger.warn("Cannot load '{}' preference file: ", _fullFilepath, fnfe.getMessage());
    }

    if (inputFile != null) {
      boolean ok = false;
      try {
        _currentProperties.loadFromXML(inputFile);
        ok = true;

      } catch (InvalidPropertiesFormatException ipfe) {
        _logger.error("Cannot parse '{}' preference file: ", _fullFilepath, ipfe);
      } catch (IOException ioe) {
        _logger.warn("Cannot load '{}' preference file: ", _fullFilepath, ioe);
      } finally {
        FileUtils.closeStream(inputFile);
      }

      if (ok) {
        handlePreferenceUpdates();
      } else {
        // Do nothing just default values will be into the preferences.
        _logger.info("Failed loading preference file, so fall back to default values instead.");

        resetToDefaultPreferences();
      }
    }

    // Notify all preferences listener of maybe new values coming from file.
    triggerObserversNotification();
  }