public void save() {
    lock.lock();

    configurationFile.getParentFile().mkdirs();

    Writer fw = null;

    try {
      // this is sort of dirty...
      String clearPass = null;

      // change the password to be encrypted
      if (configuration.getConnectionInfo() != null
          && StringUtils.isNotEmpty(configuration.getConnectionInfo().getSystemPassword())) {
        try {
          clearPass = configuration.getConnectionInfo().getSystemPassword();
          configuration.getConnectionInfo().setSystemPassword(passwordHelper.encrypt(clearPass));
        } catch (PlexusCipherException e) {
          getLogger().error("Failed to encrypt password while storing configuration file", e);
        }
      }

      fw = new OutputStreamWriter(new FileOutputStream(configurationFile));

      LdapConfigurationXpp3Writer writer = new LdapConfigurationXpp3Writer();

      writer.write(fw, configuration);

      // now reset the password
      if (configuration.getConnectionInfo() != null) {
        configuration.getConnectionInfo().setSystemPassword(clearPass);
      }

    } catch (IOException e) {
      getLogger().error("IOException while storing configuration file", e);
    } finally {
      if (fw != null) {
        try {
          fw.flush();

          fw.close();
        } catch (IOException e) {
          // just closing if open
        }
      }

      lock.unlock();
    }

    // fire clear cache event
    this.applicationEventMulticaster.notifyEventListeners(new LdapClearCacheEvent(null));
  }
  private Configuration getConfiguration() {
    Reader fr = null;
    FileInputStream is = null;

    try {

      lock.lock();

      if (configuration != null) {
        return configuration;
      }

      is = new FileInputStream(configurationFile);

      LdapConfigurationXpp3Reader reader = new LdapConfigurationXpp3Reader();

      fr = new InputStreamReader(is);

      configuration = reader.read(fr);

      ValidationResponse vr = validator.validateModel(new ValidationRequest(configuration));

      if (vr.getValidationErrors().size() > 0) {
        // TODO need to code the handling of invalid config
        configuration = new Configuration();
      }

      // decrypt the password, if it fails assume the password is clear text.
      // If the password is wrong the the LDAP Realm will not work, which is no different. If the
      // user typed in the
      // password wrong.
      if (configuration.getConnectionInfo() != null
          && StringUtils.isNotEmpty(configuration.getConnectionInfo().getSystemPassword())) {
        try {
          configuration
              .getConnectionInfo()
              .setSystemPassword(
                  passwordHelper.decrypt(configuration.getConnectionInfo().getSystemPassword()));
        } catch (PlexusCipherException e) {
          this.getLogger()
              .error(
                  "Failed to decrypt password, assuming the password in file: '"
                      + configurationFile.getAbsolutePath()
                      + "' is clear text.",
                  e);
        }
      }

    } catch (FileNotFoundException e) {
      // This is ok, may not exist first time around
      configuration = this.getDefaultConfiguration();
    } catch (IOException e) {
      getLogger().error("IOException while retrieving configuration file", e);
    } catch (XmlPullParserException e) {
      getLogger().error("Invalid XML Configuration", e);
    } finally {
      if (fr != null) {
        try {
          fr.close();
        } catch (IOException e) {
          // just closing if open
        }
      }

      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          // just closing if open
        }
      }

      lock.unlock();
    }

    return configuration;
  }