public void contextInitialized(ServletContextEvent event) {
    this.context = event.getServletContext();
    RepositorySystem sys = newRepositorySystem();
    // setting repository system
    this.context.setAttribute("RepositorySystem", sys);
    Settings settings = null;
    try {
      settings =
          loadSettings(
              context.getInitParameter("user-settings"),
              context.getInitParameter("global-settings"));
    } catch (SettingsBuildingException e) {
      this.context.log(e.getMessage(), e);
    }

    List<RemoteRepository> repos = new ArrayList<RemoteRepository>();
    RemoteRepository central =
        new RemoteRepository("central", "default", getDefaultRemoteRepository());
    this.context.log("using remote repo : " + central.getUrl());
    repos.add(central);
    if (context.getInitParameter("snapshots-remote-repo") != null) {
      this.context.log(
          "using snapshot remote repo : " + context.getInitParameter("snapshots-remote-repo"));
      RemoteRepository snaps =
          new RemoteRepository(
              "snapshot", "default", context.getInitParameter("snapshots-remote-repo"));
      snaps.setPolicy(true, null);
      repos.add(snaps);
    }
    // setting remote repositories
    this.context.setAttribute("repositories", repos);
    // setting default session
    this.context.setAttribute("session", newSystemSession(sys, settings));
  }
  public void update(Settings settings, File settingsFile) throws ManipulationException {
    try {
      Settings defaultSettings = new Settings();

      if (settingsFile.exists()) {
        DefaultSettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
        settingsRequest.setGlobalSettingsFile(settingsFile);
        defaultSettings = settingsBuilder.build(settingsRequest).getEffectiveSettings();
      }

      for (Profile profile : settings.getProfiles()) {
        Iterator<Profile> i = defaultSettings.getProfiles().iterator();
        while (i.hasNext()) {
          if (i.next().getId().equals(profile.getId())) {
            i.remove();
          }
        }
        defaultSettings.addProfile(profile);
      }
      for (String activeProfile : settings.getActiveProfiles()) {
        Iterator<String> i = defaultSettings.getActiveProfiles().iterator();
        while (i.hasNext()) {
          if (i.next().equals(activeProfile)) {
            i.remove();
          }
        }
        defaultSettings.addActiveProfile(activeProfile);
      }
      for (Mirror mirror : settings.getMirrors()) {
        defaultSettings.addMirror(mirror);
      }
      for (Proxy proxy : settings.getProxies()) {
        defaultSettings.addProxy(proxy);
      }
      for (Server server : settings.getServers()) {
        defaultSettings.addServer(server);
      }
      for (String pluginGroup : settings.getPluginGroups()) {
        defaultSettings.addPluginGroup(pluginGroup);
      }
      if (settings.getLocalRepository() != null) {
        defaultSettings.setLocalRepository(settings.getLocalRepository());
      }

      write(defaultSettings, settingsFile);
    } catch (SettingsBuildingException e) {
      throw new ManipulationException(
          "Failed to build existing settings.xml for repo removal backup.",
          e,
          settingsFile,
          e.getMessage());
    }
  }
  private synchronized Settings getSettings() {
    if (settings == null) {
      DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
      request.setUserSettingsFile(getUserSettings());
      request.setGlobalSettingsFile(getGlobalSettings());
      request.setSystemProperties(getSystemProperties());
      request.setUserProperties(getUserProperties());

      try {
        settings = settingsBuilder.build(request).getEffectiveSettings();
      } catch (SettingsBuildingException e) {
        project.log("Could not process settings.xml: " + e.getMessage(), e, Project.MSG_WARN);
      }

      SettingsDecryptionResult result =
          settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(settings));
      settings.setServers(result.getServers());
      settings.setProxies(result.getProxies());
    }
    return settings;
  }
  /**
   * Builds Maven settings from request.
   *
   * @param request The request for new settings
   */
  public Settings buildSettings(SettingsBuildingRequest request) {
    SettingsBuildingResult result;
    try {
      SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();

      if (request.getGlobalSettingsFile() != null) {
        if (log.isLoggable(Level.FINE)) {
          log.fine(
              "Using "
                  + request.getGlobalSettingsFile().getAbsolutePath()
                  + " to get global Maven settings.xml");
        }
      }
      final File userSettingsFile = request.getUserSettingsFile();
      if (userSettingsFile != null) {
        if (log.isLoggable(Level.FINE)) {
          log.fine(
              "Using " + userSettingsFile.getAbsolutePath() + " to get user Maven settings.xml");
        }

        // Maven will not check the format passed in (any XML will do), so let's ensure we have a
        // settings.xml by checking just the top-level element
        final XMLStreamReader reader;
        try {
          reader =
              XMLInputFactory.newInstance()
                  .createXMLStreamReader(new FileInputStream(userSettingsFile));
          // get the first element name
          while (reader.hasNext()) {
            if (reader.next() == XMLStreamConstants.START_ELEMENT) {
              break;
            }
          }
          final String topLevel = reader.getLocalName();

          if (!"settings".equals(topLevel)) {
            throw new InvalidConfigurationFileException(
                "Invalid format settings.xml found: " + userSettingsFile);
          }
        } catch (final FileNotFoundException e) {
          // Ignore at this level
        } catch (final XMLStreamException xmlse) {
          throw new RuntimeException(
              "Could not check file format of specified settings.xml: " + userSettingsFile, xmlse);
        }
      }

      result = builder.build(request);
    }
    // wrap exception message
    catch (SettingsBuildingException e) {
      StringBuilder sb =
          new StringBuilder("Found ")
              .append(e.getProblems().size())
              .append(
                  " problems while building settings.xml model from both global Maven configuration file")
              .append(request.getGlobalSettingsFile())
              .append(" and/or user configuration file: ")
              .append(request.getUserSettingsFile())
              .append("\n");

      int counter = 1;
      for (SettingsProblem problem : e.getProblems()) {
        sb.append(counter++).append("/ ").append(problem).append("\n");
      }

      throw new InvalidConfigurationFileException(sb.toString());
    }

    // get settings object and update it according to property values
    Settings settings = result.getEffectiveSettings();
    settings = enrichWithLocalRepository(settings);
    settings = enrichWithOfflineMode(settings);
    return settings;
  }