@Override
  public void saveRepositories() {
    if (storage != null) {
      try {
        OutputStream output = storage.createOutputStream();
        try {
          repositoryRegistry.eResource().save(output, null);
        } finally {
          if (output != null) {
            IOUtil.closeSilent(output);
          }
        }
      } catch (IOException e) {
        Activator.log.error(
            "Failed to save model repositories to custom storage.", e); // $NON-NLS-1$
      }
    } else {
      try {
        repositoryRegistry.eResource().save(null);
      } catch (IOException e) {
        Activator.log.error("Failed to save model repositories.", e); // $NON-NLS-1$
      }
    }

    // save passwords, if any
    try {
      SecurePreferencesFactory.getDefault().flush();
    } catch (IOException e) {
      Activator.log.error(
          "Failed to save repository passwords to secure storage.", e); // $NON-NLS-1$
    }
  }
  private RepositoryRegistry loadRepositories() {
    ResourceSet rset = new ResourceSetImpl();
    rset.getResourceFactoryRegistry()
        .getExtensionToFactoryMap()
        .put("xml", new XMLResourceFactoryImpl()); // $NON-NLS-1$

    File repositoriesFile =
        new File(
            Activator.getDefault().getStateLocation().toFile(), "repositories.xml"); // $NON-NLS-1$
    URI uri = URI.createFileURI(repositoriesFile.getAbsolutePath());
    Resource resource = rset.createResource(uri);

    if (storage != null) {
      try {
        InputStream input = storage.createInputStream();
        if (input != null) {
          try {
            resource.load(input, null);
          } finally {
            IOUtil.closeSilent(input);
          }
        }
      } catch (Exception e) {
        Activator.log.error(
            "Failed to load repository registry from custom storage.", //$NON-NLS-1$
            e);
      }
    } else {
      if (repositoriesFile.exists()) {
        try {
          resource.load(null);
        } catch (Exception e) {
          Activator.log.error(
              "Failed to load repository registry.", //$NON-NLS-1$
              e);
          // if there's any junk, clear it out
          resource.getContents().clear();
        }
      } else {
        resource = rset.createResource(uri);
      }
    }

    RepositoryRegistry result =
        (RepositoryRegistry)
            EcoreUtil.getObjectByType(
                resource.getContents(), RepositoriesPackage.Literals.REPOSITORY_REGISTRY);

    if (result == null) {
      result = RepositoriesFactory.eINSTANCE.createRepositoryRegistry();
      resource.getContents().add(result);
    }

    return result;
  }