public synchronized MavenResolver getMavenResolver() throws RuntimeException {
    if (mavenResolver == null) {
      BundleContext context = CoreRuntimePlugin.getInstance().getBundle().getBundleContext();
      ServiceReference<ManagedService> managedServiceRef =
          context.getServiceReference(ManagedService.class);
      if (managedServiceRef != null) {
        String repositories = "";
        NexusServerBean customServer = getCustomNexusServer();
        if (customServer != null) {
          // custom nexus server should use snapshot repository
          repositories = customServer.getRepositoryUrl() + NexusConstants.SNAPSHOTS + ",";
        }
        final NexusServerBean officailServer = getLibrariesNexusServer();
        repositories = repositories + officailServer.getRepositoryUrl();

        ManagedService managedService = context.getService(managedServiceRef);
        Dictionary<String, String> props = new Hashtable<String, String>();
        props.put(
            ServiceConstants.PID + '.' + ServiceConstants.PROPERTY_REPOSITORIES, repositories);

        // get the setting file same as M2E preference in M2eUserSettingForTalendLoginTask.
        String settingsFile = MavenPlugin.getMavenConfiguration().getUserSettingsFile();
        if (settingsFile != null && new File(settingsFile).exists()) {
          props.put(
              ServiceConstants.PID + '.' + ServiceConstants.PROPERTY_SETTINGS_FILE, settingsFile);
        }

        try {
          managedService.updated(props);
        } catch (ConfigurationException e) {
          throw new RuntimeException("Failed to modifiy the service properties"); // $NON-NLS-1$
        }
      } else {
        throw new RuntimeException(
            "Failed to load the service :"
                + ManagedService.class.getCanonicalName()); // $NON-NLS-1$
      }

      ServiceReference<org.ops4j.pax.url.mvn.MavenResolver> mavenResolverService =
          context.getServiceReference(org.ops4j.pax.url.mvn.MavenResolver.class);
      if (mavenResolverService != null) {
        mavenResolver = context.getService(mavenResolverService);
      } else {
        throw new RuntimeException("Unable to acquire org.ops4j.pax.url.mvn.MavenResolver");
      }
    }

    return mavenResolver;
  }
 public NexusServerBean getLibrariesNexusServer() {
   NexusServerBean serverBean = new NexusServerBean();
   serverBean.setServer(
       System.getProperty("org.talend.libraries.repo.url", NexusServerUtils.TALEND_LIB_SERVER));
   serverBean.setUserName(NexusServerUtils.TALEND_LIB_USER);
   serverBean.setPassword(NexusServerUtils.TALEND_LIB_PASSWORD);
   serverBean.setRepositoryId(NexusServerUtils.TALEND_LIB_REPOSITORY);
   serverBean.setOfficial(true);
   String server = serverBean.getServer();
   // remove the trailing slash
   if (server.endsWith(NexusConstants.SLASH)) {
     server = server.substring(0, server.length() - 1);
   }
   String newUrl =
       server
           + NexusConstants.CONTENT_REPOSITORIES
           + serverBean.getRepositoryId()
           + "@id="
           + serverBean.getRepositoryId(); // $NON-NLS-1$
   serverBean.setRepositoryUrl(newUrl);
   return serverBean;
 }
  public NexusServerBean getCustomNexusServer() {
    NexusServerBean serverBean = null;
    try {
      IProxyRepositoryFactory factory = CoreRuntimePlugin.getInstance().getProxyRepositoryFactory();
      RepositoryContext repositoryContext = factory.getRepositoryContext();
      if (factory.isLocalConnectionProvider() || repositoryContext.isOffline()) {
        return null;
      }
      if (repositoryContext != null && repositoryContext.getFields() != null) {
        String adminUrl = repositoryContext.getFields().get(RepositoryConstants.REPOSITORY_URL);
        String userName = "";
        String password = "";
        User user = repositoryContext.getUser();
        if (user != null) {
          userName = user.getLogin();
          password = repositoryContext.getClearPassword();
        }

        if (adminUrl != null
            && !"".equals(adminUrl)
            && GlobalServiceRegister.getDefault().isServiceRegistered(IRemoteService.class)) {
          IRemoteService remoteService =
              (IRemoteService) GlobalServiceRegister.getDefault().getService(IRemoteService.class);
          JSONObject libServerObject;
          libServerObject = remoteService.getLibNexusServer(userName, password, adminUrl);
          if (libServerObject != null) {
            String nexus_url = libServerObject.getString(NexusServerUtils.KEY_NEXUS_RUL);
            String nexus_user = libServerObject.getString(NexusServerUtils.KEY_NEXUS_USER);
            String nexus_pass = libServerObject.getString(NexusServerUtils.KEY_NEXUS_PASS);
            String repositoryId =
                libServerObject.getString(NexusServerUtils.KEY_CUSTOM_LIB_REPOSITORY);

            // TODO check if custom nexus is valid , only check http response for now , need check
            // if it is
            // snapshot latter
            boolean connectionOk =
                NexusServerUtils.checkConnectionStatus(
                    nexus_url, repositoryId, nexus_user, nexus_pass);
            if (!connectionOk) {
              return null;
            }

            String newUrl = nexus_url;
            if (newUrl.endsWith(NexusConstants.SLASH)) {
              newUrl = newUrl.substring(0, newUrl.length() - 1);
            }
            if (nexus_user != null && !"".equals(nexus_user)) { // $NON-NLS-1$
              String[] split = newUrl.split("://"); // $NON-NLS-1$
              if (split.length != 2) {
                throw new RuntimeException(
                    "Nexus url is not valid ,please contract the administrator");
              }
              newUrl =
                  split[0]
                      + ":"
                      + nexus_user
                      + ":"
                      + nexus_pass
                      + "@//" //$NON-NLS-1$
                      + split[1];
            }
            newUrl =
                newUrl
                    + NexusConstants.CONTENT_REPOSITORIES
                    + repositoryId
                    + "@id="
                    + repositoryId; //$NON-NLS-1$

            serverBean = new NexusServerBean();
            serverBean.setServer(nexus_url);
            serverBean.setUserName(nexus_user);
            serverBean.setPassword(nexus_pass);
            serverBean.setRepositoryId(repositoryId);
            serverBean.setRepositoryUrl(newUrl);
          }
        }
      }
    } catch (Exception e) {
      serverBean = null;
      ExceptionHandler.process(e);
    }
    if (previousCustomBean == null && serverBean != null
        || previousCustomBean != null && !previousCustomBean.equals(serverBean)) {
      mavenResolver = null;
    }
    previousCustomBean = serverBean;
    return serverBean;
  }