mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

david_page
28.55.2007 d99ee98b0f2edaaeea89e1028b28f7626c0c80dd
Issue 466 preparation

Clean up duplicate code in Installer.updateADS in preparation for adding instance public-key certificate to properties. External behavior (e.g., registeredNewServerOnRemote) should be unchanged.

Ran precommit tests and various setup hand tests.
1 files modified
320 ■■■■■ changed files
opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java 320 ●●●●● patch | view | raw | blame | history
opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
@@ -1170,7 +1170,8 @@
          {
            try
            {
              adsContext.unregisterServer(getNewServerAdsProperties());
              adsContext.unregisterServer(getNewServerAdsProperties(
                                                                getUserData()));
            }
            catch (ADSContextException ace)
            {
@@ -1189,7 +1190,8 @@
          }
          if (createdAdministrator)
          {
            adsContext.deleteAdministrator(getAdministratorProperties());
            adsContext.deleteAdministrator(getAdministratorProperties(
                                                                getUserData()));
          }
        }
        notifyListeners(getFormattedDone());
@@ -1853,49 +1855,127 @@
  /**
   * This method updates the ADS contents (and creates the according suffixes).
   * NOTE: this method assumes that the server is running.
   * If the user specified an existing topology, the new instance is
   * registered with that ADS (the ADS might need to be created), and the
   * local ADS will be populated when the local server is added to the remote
   * server's ADS replication domain in a subsequent step. Otherwise, an ADS
   * is created on the new instance and the server is registered with the new
   * ADS. NOTE: this method assumes that the local server and any remote server
   * are running.
   * @throws ApplicationException if something goes wrong.
   */
  protected void updateADS() throws ApplicationException
  {
    /*
     * First check if the remote server contains an ADS: if it is the case the
     * best is to update its contents with the new data and then configure the
     * local server to be replicated with the remote server.
     */
    DataReplicationOptions repl = getUserData().getReplicationOptions();
    boolean remoteServer =
      repl.getType() == DataReplicationOptions.Type.IN_EXISTING_TOPOLOGY;
    InitialLdapContext localCtx = null;
    AuthenticationData auth = (remoteServer) ? repl.getAuthenticationData()
                                             : null;
    InitialLdapContext remoteCtx = null; // Bound to remote ADS host (if any).
    InitialLdapContext localCtx = null; // Bound to local server.
    ADSContext adsContext = null; // Bound to ADS host (via one of above).
    /* Outer try-catch-finally to convert ADSContextException to
       ApplicationException and clean up JNDI contexts. */
    try
    {
    if (remoteServer)
    {
      // Try to connect
      AuthenticationData auth = repl.getAuthenticationData();
        /* In case the user specified an existing topology... */
        try
        {
      String ldapUrl = getLdapUrl(auth);
      String dn = auth.getDn();
      String pwd = auth.getPwd();
      InitialLdapContext ctx = null;
      try
      {
        if (auth.useSecureConnection())
        {
          ApplicationTrustManager trustManager = getTrustManager();
          trustManager.setHost(auth.getHostName());
          ctx = createLdapsContext(ldapUrl, dn, pwd,
            remoteCtx = createLdapsContext(ldapUrl, dn, pwd,
              getDefaultLDAPTimeout(), null, trustManager);
        }
        else
        {
          ctx = createLdapContext(ldapUrl, dn, pwd,
            remoteCtx = createLdapContext(ldapUrl, dn, pwd,
              getDefaultLDAPTimeout(), null);
        }
          adsContext = new ADSContext(remoteCtx); // adsContext owns remoteCtx
        // Check the remote server for ADS. If it does not exist, create the
        // initial ADS there. Otherwise, create a global administrator if the
        // user requested one.
        ADSContext adsContext = new ADSContext(ctx);
        if (adsContext.hasAdminData())
          /* Check the remote server for ADS. If it does not exist, create the
             initial ADS there and register the server with itself. */
          if (! adsContext.hasAdminData())
        {
            notifyListeners(getFormattedWithPoints(
               INFO_PROGRESS_CREATING_ADS_ON_REMOTE.get(getHostDisplay(auth))));
            adsContext.createAdminData(null);
            adsContext.registerServer(
                    getRemoteServerProperties(adsContext.getDirContext()));
            createdRemoteAds = true;
            notifyListeners(getFormattedDone());
            notifyListeners(getLineBreak());
            checkAbort();
          }
        }
        catch (NoPermissionException x)
        {
          throw new ApplicationException(
                  ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
                  INFO_CANNOT_CONNECT_TO_REMOTE_PERMISSIONS.get(
                          getHostDisplay(auth)), x);
        }
        catch (NamingException ne)
        {
          throw new ApplicationException(
                  ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
                  INFO_CANNOT_CONNECT_TO_REMOTE_GENERIC.get(
                          getHostDisplay(auth), ne.getLocalizedMessage()), ne);
        }
      }
      /* Act on local server depending on if using remote or local ADS */
      notifyListeners(getFormattedWithPoints(INFO_PROGRESS_CREATING_ADS.get()));
      try
      {
        localCtx = createLocalContext();
        if (remoteServer)
        {
          /* Create an empty ADS suffix on the local server. */
          ADSContext localAdsContext = new ADSContext(localCtx);
          localAdsContext.createAdministrationSuffix(null);
        }
        else
        {
          /* Configure local server to have an ADS */
          adsContext = new ADSContext(localCtx); // adsContext owns localCtx
          adsContext.createAdminData(null);
        }
      }
      catch (NamingException t)
      {
        Message failedMsg = getThrowableMsg(
                INFO_ERROR_CONNECTING_TO_LOCAL.get(), t);
        throw new ApplicationException(
                ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
                failedMsg, t);
      }
      assert null != adsContext ; // Bound either to local or remote ADS.
      /* Register new server in ADS. */
      if (0 != adsContext.registerOrUpdateServer(getNewServerAdsProperties(
              getUserData())))
      {
        LOG.log(Level.WARNING, "Server was already registered. Updating " +
                "server registration.");
      }
      else if (remoteServer)
      {
        registeredNewServerOnRemote = true;
      }
      notifyListeners(getFormattedDone());
      notifyListeners(getLineBreak());
      checkAbort();
          /* Add global administrator if the user specified one. */
          if (getUserData().mustCreateAdministrator())
          {
@@ -1903,8 +1983,9 @@
            {
              notifyListeners(getFormattedWithPoints(
                  INFO_PROGRESS_CREATING_ADMINISTRATOR.get()));
              adsContext.createAdministrator(getAdministratorProperties());
              createdAdministrator = true;
          adsContext.createAdministrator(getAdministratorProperties(
                  getUserData()));
          if (remoteServer && !createdRemoteAds) createdAdministrator = true;
              notifyListeners(getFormattedDone());
              notifyListeners(getLineBreak());
              checkAbort();
@@ -1924,175 +2005,21 @@
            }
          }
        }
        else
        {
          notifyListeners(getFormattedWithPoints(
              INFO_PROGRESS_CREATING_ADS_ON_REMOTE.get(getHostDisplay(auth))));
          adsContext.createAdminData(null);
          adsContext.createAdministrator(getAdministratorProperties());
          adsContext.registerServer(
              getRemoteServerProperties(adsContext.getDirContext()));
          createdRemoteAds = true;
          notifyListeners(getFormattedDone());
          notifyListeners(getLineBreak());
          checkAbort();
        }
        // Create an empty ADS suffix on the local server.
        notifyListeners(getFormattedWithPoints(
            INFO_PROGRESS_CREATING_ADS.get()));
        try
        {
          localCtx = createLocalContext();
        }
        catch (Throwable t)
        {
          Message failedMsg = getThrowableMsg(
                  INFO_ERROR_CONNECTING_TO_LOCAL.get(), t);
          throw new ApplicationException(
              ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
              failedMsg, t);
        }
        try
        {
          ADSContext localAdsContext = new ADSContext(localCtx);
          localAdsContext.createAdministrationSuffix(null);
        }
        catch (ADSContextException ace)
        {
          throw ace;
        }
        catch (Throwable t)
        {
          throw new ApplicationException(
                  ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
                  getThrowableMsg(INFO_BUG_MSG.get(), t), t);
        }
        notifyListeners(getFormattedDone());
        notifyListeners(getLineBreak());
        checkAbort();
        /* Register new server in remote ADS. */
        if(0 != adsContext.registerOrUpdateServer(getNewServerAdsProperties()))
        {
          LOG.log(Level.WARNING, "Server was already registered. Updating " +
            "server registration.");
        }
        registeredNewServerOnRemote = true;
      }
      catch (NoPermissionException x)
      {
        throw new ApplicationException(
            ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
            INFO_CANNOT_CONNECT_TO_REMOTE_PERMISSIONS.get(
                    getHostDisplay(auth)), x);
      }
      catch (NamingException ne)
      {
        throw new ApplicationException(
            ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
            INFO_CANNOT_CONNECT_TO_REMOTE_GENERIC.get(
                    getHostDisplay(auth), ne.getLocalizedMessage()), ne);
      }
      catch (ADSContextException ace)
      {
        throw new ApplicationException(
            ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
            INFO_REMOTE_ADS_EXCEPTION.get(
                    getHostDisplay(auth), ace.getReason()), ace);
              ((remoteServer)
                      ? INFO_REMOTE_ADS_EXCEPTION.get(
                             getHostDisplay(auth), ace.getReason())
                      : INFO_ADS_EXCEPTION.get(ace.toString())), ace);
      }
      finally
      {
        if (ctx != null)
        {
          try
          {
            ctx.close();
          }
          catch (Throwable t)
          {
          }
        }
        if (localCtx != null)
        {
          try
          {
            localCtx.close();
          }
          catch (Throwable t)
          {
          }
        }
      }
    }
    else
    {
      try
      {
        /* Configure local server to have an ADS */
        notifyListeners(getFormattedWithPoints(
            INFO_PROGRESS_CREATING_ADS.get()));
        try
        {
          localCtx = createLocalContext();
        }
        catch (Throwable t)
        {
          Message failedMsg = getThrowableMsg(
                  INFO_ERROR_CONNECTING_TO_LOCAL.get(),
                  t);
          throw new ApplicationException(
              ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
              failedMsg, t);
        }
        try
        {
          ADSContext localAdsContext = new ADSContext(localCtx);
          localAdsContext.createAdminData(null);
          localAdsContext.registerServer(getNewServerAdsProperties());
          if (getUserData().mustCreateAdministrator())
          {
            localAdsContext.createAdministrator(getAdministratorProperties());
          }
        }
        catch (ADSContextException ace)
        {
          throw ace;
        }
        catch (Throwable t)
        {
          throw new ApplicationException(
                  ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
                  getThrowableMsg(INFO_BUG_MSG.get(), t), t);
        }
        notifyListeners(getFormattedDone());
        notifyListeners(getLineBreak());
      }
      catch (ADSContextException ace)
      {
        throw new ApplicationException(
            ApplicationReturnCode.ReturnCode.CONFIGURATION_ERROR,
            INFO_ADS_EXCEPTION.get(ace.toString()), ace);
      }
      finally
      {
        if (localCtx != null)
        {
          try
          {
            localCtx.close();
          }
          catch (Throwable t)
          {
          }
        }
      }
      if (null != remoteCtx)
        try { remoteCtx.close(); } catch (NamingException x){ }
      if (null != localCtx)
        try { localCtx.close(); } catch (NamingException x){ }
    }
  }
@@ -2200,19 +2127,20 @@
    return auth.getHostName()+":"+auth.getPort();
  }
  private Map<ADSContext.ServerProperty, Object> getNewServerAdsProperties()
  private Map<ADSContext.ServerProperty, Object>
  getNewServerAdsProperties(UserData userData)
  {
    Map<ADSContext.ServerProperty, Object> serverProperties =
      new HashMap<ADSContext.ServerProperty, Object>();
    serverProperties.put(ADSContext.ServerProperty.HOST_NAME,
          getUserData().getHostName());
          userData.getHostName());
    serverProperties.put(ADSContext.ServerProperty.LDAP_PORT,
        String.valueOf(getUserData().getServerPort()));
        String.valueOf(userData.getServerPort()));
    serverProperties.put(ADSContext.ServerProperty.LDAP_ENABLED, "true");
    // TODO: even if the user does not configure SSL maybe we should choose
    // a secure port that is not being used and that we can actually use.
    SecurityOptions sec = getUserData().getSecurityOptions();
    SecurityOptions sec = userData.getSecurityOptions();
    if (sec.getEnableSSL())
    {
      serverProperties.put(ADSContext.ServerProperty.LDAPS_PORT,
@@ -2240,7 +2168,7 @@
    String path;
    if (isWebStart())
    {
      path = getUserData().getServerLocation();
      path = userData.getServerLocation();
    }
    else
    {
@@ -2249,7 +2177,7 @@
    serverProperties.put(ADSContext.ServerProperty.INSTANCE_PATH, path);
    String serverID = serverProperties.get(ADSContext.ServerProperty.HOST_NAME)+
    ":"+getUserData().getServerPort();
    ":"+userData.getServerPort();
    /* TODO: do we want to ask this specifically to the user? */
    serverProperties.put(ADSContext.ServerProperty.ID, serverID);
@@ -2260,14 +2188,14 @@
  }
  private Map<ADSContext.AdministratorProperty, Object>
  getAdministratorProperties()
  getAdministratorProperties(UserData userData)
  {
    Map<ADSContext.AdministratorProperty, Object> adminProperties =
      new HashMap<ADSContext.AdministratorProperty, Object>();
    adminProperties.put(ADSContext.AdministratorProperty.UID,
        getUserData().getGlobalAdministratorUID());
        userData.getGlobalAdministratorUID());
    adminProperties.put(ADSContext.AdministratorProperty.PASSWORD,
        getUserData().getGlobalAdministratorPassword());
        userData.getGlobalAdministratorPassword());
    adminProperties.put(ADSContext.AdministratorProperty.DESCRIPTION,
        INFO_GLOBAL_ADMINISTRATOR_DESCRIPTION.get().toString());
    return adminProperties;
@@ -3941,8 +3869,6 @@
  private static int getRandomInt(Random random,int modulo)
  {
    int value = 0;
    value = (random.nextInt() & modulo);
    return value;
    return (random.nextInt() & modulo);
  }
}