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

Jean-Noël Rouvignac
08.38.2016 fdb3fa313407639240f75ec566865cea25bc039b
Partial OPENDJ-2625 Convert all code that uses JNDI to use the SDK instead

Convert dsreplication
5 files modified
320 ■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/admin/ads/ADSContext.java 27 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/admin/ads/TopologyCache.java 77 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/guitools/uninstaller/Uninstaller.java 13 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/PurgeHistoricalUserData.java 93 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java 110 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/admin/ads/ADSContext.java
@@ -35,9 +35,6 @@
import java.util.SortedSet;
import java.util.TreeSet;
import javax.naming.NamingException;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.Rdn;
import org.forgerock.i18n.LocalizableMessage;
@@ -54,7 +51,9 @@
import org.forgerock.opendj.ldap.ModificationType;
import org.forgerock.opendj.ldap.RDN;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.controls.SubtreeDeleteRequestControl;
import org.forgerock.opendj.ldap.requests.AddRequest;
import org.forgerock.opendj.ldap.requests.DeleteRequest;
import org.forgerock.opendj.ldap.requests.ModifyRequest;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.responses.Result;
@@ -959,36 +958,22 @@
      removeAdministrators ? getAdministratorContainerDN() : null };
    try
    {
      Control[] controls = new Control[] { new SubtreeDeleteControl() };
      LdapContext tmpContext = connectionWrapper.getLdapContext().newInstance(controls);
      try
      {
        for (String dn : dns)
        {
          if (dn != null)
          {
            if (isExistingEntry(dn))
            {
              tmpContext.destroySubcontext(dn);
            DeleteRequest request = newDeleteRequest(dn)
                .addControl(SubtreeDeleteRequestControl.newControl(true));
            connectionWrapper.getConnection().delete(request);
            }
          }
        }
      }
      finally
      {
        try
        {
          tmpContext.close();
        }
        catch (Exception ex)
        {
          logger.warn(LocalizableMessage.raw("Error while closing LDAP connection after removing admin data", ex));
        }
      }
      // Recreate the container entries:
      createAdminDataContainers();
    }
    catch (NamingException x)
    catch (LdapException x)
    {
      throw new ADSContextException(ErrorType.ERROR_UNEXPECTED, x);
    }
opendj-server-legacy/src/main/java/org/opends/admin/ads/TopologyCache.java
@@ -16,6 +16,7 @@
 */
package org.opends.admin.ads;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
@@ -25,19 +26,19 @@
import java.util.Map;
import java.util.Set;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapName;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.EntryNotFoundException;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.opends.admin.ads.ADSContext.ServerProperty;
import org.opends.admin.ads.util.ApplicationTrustManager;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.admin.ads.util.ConnectionWrapper;
import org.opends.admin.ads.util.PreferredConnection;
import org.opends.admin.ads.util.ServerLoader;
@@ -45,6 +46,8 @@
import static com.forgerock.opendj.cli.Utils.*;
import static org.forgerock.opendj.ldap.SearchScope.*;
import static org.opends.admin.ads.util.ConnectionUtils.*;
import static org.opends.messages.QuickSetupMessages.*;
/**
@@ -195,10 +198,10 @@
          {
            updateReplicas(server, candidateReplicas, updatedReplicas);
          }
          catch (NamingException ne)
          catch (NamingException | IOException e)
          {
            server.setLastException(new TopologyCacheException(
                TopologyCacheException.Type.GENERIC_READING_SERVER, ne));
                TopologyCacheException.Type.GENERIC_READING_SERVER, e));
          }
          replicasToUpdate.removeAll(updatedReplicas);
        }
@@ -430,33 +433,23 @@
  private void updateReplicas(ServerDescriptor replicationServer,
                              Collection<ReplicaDescriptor> candidateReplicas,
                              Collection<ReplicaDescriptor> updatedReplicas)
      throws NamingException
      throws NamingException, IOException
  {
    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(
        new String[]
        {
          "approx-older-change-not-synchronized-millis", "missing-changes",
          "domain-name", "server-id"
        });
    NamingEnumeration<SearchResult> monitorEntries = null;
    ServerLoader loader = getServerLoader(replicationServer.getAdsProperties());
    try (ConnectionWrapper conn = loader.createConnectionWrapper())
    SearchRequest request=Requests.newSearchRequest("cn=monitor", WHOLE_SUBTREE, "(missing-changes=*)",
        "approx-older-change-not-synchronized-millis", "missing-changes", "domain-name", "server-id");
    try (ConnectionWrapper conn = loader.createConnectionWrapper();
        ConnectionEntryReader entryReader = conn.getConnection().search(request))
    {
      monitorEntries = conn.getLdapContext().search(
          new LdapName("cn=monitor"), "(missing-changes=*)", ctls);
      while (monitorEntries.hasMore())
      while (entryReader.hasNext())
      {
        SearchResult sr = monitorEntries.next();
        SearchResultEntry sr = entryReader.readEntry();
        String dn = ConnectionUtils.getFirstValue(sr, "domain-name");
        String dn = firstValueAsString(sr, "domain-name");
        int replicaId = -1;
        try
        {
          String sid = ConnectionUtils.getFirstValue(sr, "server-id");
          Integer sid = asInteger(sr, "server-id");
          if (sid == null)
          {
            // This is not a replica, but a replication server. Skip it
@@ -466,8 +459,7 @@
        }
        catch (Throwable t)
        {
          logger.warn(LocalizableMessage.raw("Unexpected error reading replica ID: " + t,
              t));
          logger.warn(LocalizableMessage.raw("Unexpected error reading replica ID: " + t, t));
        }
        for (ReplicaDescriptor replica : candidateReplicas)
@@ -484,34 +476,19 @@
        }
      }
    }
    catch (NameNotFoundException nse)
    catch (EntryNotFoundException e)
    {
    }
    finally
    {
      if (monitorEntries != null)
      {
        try
        {
          monitorEntries.close();
        }
        catch (Throwable t)
        {
          logger.warn(LocalizableMessage.raw(
              "Unexpected error closing enumeration on monitor entries" + t, t));
        }
      }
    }
  }
  private void setMissingChanges(ReplicaDescriptor replica, SearchResult sr) throws NamingException
  private void setMissingChanges(ReplicaDescriptor replica, SearchResultEntry sr) throws NamingException
  {
    String s = ConnectionUtils.getFirstValue(sr, "missing-changes");
    if (s != null)
    Integer value = asInteger(sr, "missing-changes");
    if (value != null)
    {
      try
      {
        replica.setMissingChanges(Integer.valueOf(s));
        replica.setMissingChanges(value);
      }
      catch (Throwable t)
      {
@@ -521,9 +498,9 @@
    }
  }
  private void setAgeOfOldestMissingChange(ReplicaDescriptor replica, SearchResult sr) throws NamingException
  private void setAgeOfOldestMissingChange(ReplicaDescriptor replica, SearchResultEntry sr) throws NamingException
  {
    String s = ConnectionUtils.getFirstValue(sr, "approx-older-change-not-synchronized-millis");
    String s = firstValueAsString(sr, "approx-older-change-not-synchronized-millis");
    if (s != null)
    {
      try
opendj-server-legacy/src/main/java/org/opends/guitools/uninstaller/Uninstaller.java
@@ -33,8 +33,6 @@
import java.util.Map;
import java.util.Set;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@@ -1471,16 +1469,7 @@
      getUninstallUserData().setAdminUID(loginDialog.getAdministratorUid());
      getUninstallUserData().setAdminPwd(loginDialog.getAdministratorPwd());
      final ConnectionWrapper connWrapper = loginDialog.getConnection();
      try
      {
        getUninstallUserData().setLocalServerUrl(
            (String)connWrapper.getLdapContext().getEnvironment().get(Context.PROVIDER_URL));
      }
      catch (NamingException ne)
      {
        logger.warn(LocalizableMessage.raw("Could not find local server: "+ne, ne));
        getUninstallUserData().setLocalServerUrl("ldap://localhost:389");
      }
      getUninstallUserData().setLocalServerUrl(connWrapper.getLdapUrl());
      getUninstallUserData().setReplicationServer(
          loginDialog.getHostName() + ":" +
          conf.getReplicationServerPort());
opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/PurgeHistoricalUserData.java
@@ -16,21 +16,11 @@
 */
package org.opends.server.tools.dsreplication;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import org.forgerock.opendj.ldap.ByteString;
import org.opends.server.admin.client.cli.TaskScheduleArgs;
import org.opends.server.tools.tasks.TaskClient;
import org.opends.server.tools.tasks.TaskScheduleUserData;
import org.opends.server.types.HostPort;
import org.opends.server.types.RawAttribute;
/** This class is used to store the information provided by the user to purge historical data. */
public class PurgeHistoricalUserData extends MonoServerReplicationUserData
@@ -150,87 +140,4 @@
    uData.setMaximumDuration(argParser.getMaximumDurationOrDefault());
  }
  /**
   * Commodity method that returns the list of basic task attributes required
   * to launch a task corresponding to the provided user data.
   * @param uData the user data describing the purge historical to be executed.
   * @return the list of basic task attributes required
   * to launch a task corresponding to the provided user data.
   */
  public static BasicAttributes getTaskAttributes(PurgeHistoricalUserData uData)
  {
    PurgeHistoricalScheduleInformation information =
      new PurgeHistoricalScheduleInformation(uData);
    return getAttributes(TaskClient.getTaskAttributes(information));
  }
  private static BasicAttributes getAttributes(ArrayList<RawAttribute> rawAttrs)
  {
    BasicAttributes attrs = new BasicAttributes();
    for (RawAttribute rawAttr : rawAttrs)
    {
      BasicAttribute attr = new BasicAttribute(rawAttr.getAttributeType());
      for (ByteString v : rawAttr.getValues())
      {
        attr.add(v.toString());
      }
      attrs.put(attr);
    }
    return attrs;
  }
  /**
   * Returns the DN of the task corresponding to the provided list of
   * attributes.  The code assumes that the attributes have been generated
   * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}.
   * @param attrs the attributes of the task entry.
   * @return the DN of the task entry.
   */
  public static String getTaskDN(BasicAttributes attrs)
  {
    ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs);
    return TaskClient.getTaskDN(rawAttrs);
  }
  /**
   * Returns the ID of the task corresponding to the provided list of
   * attributes.  The code assumes that the attributes have been generated
   * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}.
   * @param attrs the attributes of the task entry.
   * @return the ID of the task entry.
   */
  public static String getTaskID(BasicAttributes attrs)
  {
    ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs);
    return TaskClient.getTaskID(rawAttrs);
  }
  private static ArrayList<RawAttribute> getRawAttributes(BasicAttributes attrs)
  {
    try
    {
      ArrayList<RawAttribute> rawAttrs = new ArrayList<>();
      NamingEnumeration<Attribute> nAtt = attrs.getAll();
      while (nAtt.hasMore())
      {
        Attribute attr = nAtt.next();
        NamingEnumeration<?> values = attr.getAll();
        ArrayList<ByteString> rawValues = new ArrayList<>();
        while (values.hasMore())
        {
          Object v = values.next();
          rawValues.add(ByteString.valueOfUtf8(v.toString()));
        }
        RawAttribute rAttr = RawAttribute.create(attr.getID(), rawValues);
        rawAttrs.add(rAttr);
      }
      return rawAttrs;
    }
    catch (NamingException ne)
    {
      // This is a bug.
      throw new RuntimeException("Unexpected error: "+ne, ne);
    }
  }
}
opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java
@@ -64,14 +64,7 @@
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapName;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
@@ -93,6 +86,7 @@
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.EntryNotFoundException;
import org.forgerock.opendj.ldap.LdapException;
import org.forgerock.opendj.ldap.LinkedAttribute;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.requests.AddRequest;
import org.forgerock.opendj.ldap.requests.SearchRequest;
@@ -145,6 +139,7 @@
import org.opends.server.tasks.PurgeConflictsHistoricalTask;
import org.opends.server.tools.dsreplication.EnableReplicationUserData.EnableReplicationServerData;
import org.opends.server.tools.dsreplication.ReplicationCliArgumentParser.ServerArgs;
import org.opends.server.tools.tasks.TaskClient;
import org.opends.server.tools.tasks.TaskEntry;
import org.opends.server.tools.tasks.TaskScheduleInteraction;
import org.opends.server.tools.tasks.TaskScheduleUserData;
@@ -152,6 +147,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.NullOutputStream;
import org.opends.server.types.OpenDsException;
import org.opends.server.types.RawAttribute;
import org.opends.server.util.BuildVersion;
import org.opends.server.util.ServerConstants;
import org.opends.server.util.SetupUtils;
@@ -1349,29 +1345,29 @@
        }
        argParser.setResetChangeNumber(newStartCN);
      }
      SearchControls ctls = new SearchControls();
      ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
      ctls.setReturningAttributes(
          new String[] {
      SearchResultEntry sr;
      SearchRequest request=newSearchRequest("cn=changelog", WHOLE_SUBTREE, "(changeNumber=" + newStartCN + ")",
              "changeNumber",
              "replicationCSN",
              "targetDN"
          });
      NamingEnumeration<SearchResult> listeners = connSource.getLdapContext().search(
          new LdapName("cn=changelog"), "(changeNumber=" + newStartCN + ")", ctls);
      if (!listeners.hasMore())
          "targetDN");
      try (ConnectionEntryReader entryReader = connSource.getConnection().search(request))
      {
        if (!entryReader.hasNext())
      {
        errPrintln(ERROR_RESET_CHANGE_NUMBER_UNKNOWN_NUMBER.get(newStartCN, uData.getSourceHostPort()));
        return ERROR_UNKNOWN_CHANGE_NUMBER;
      }
      SearchResult sr = listeners.next();
      String newStartCSN = getFirstValue(sr, "replicationCSN");
        sr = entryReader.readEntry();
      }
      String newStartCSN = firstValueAsString(sr, "replicationCSN");
      if (newStartCSN == null)
      {
        errPrintln(ERROR_RESET_CHANGE_NUMBER_NO_CSN_FOUND.get(newStartCN, uData.getSourceHostPort()));
        return ERROR_RESET_CHANGE_NUMBER_NO_CSN;
      }
      String targetDN = getFirstValue(sr, "targetDN");
      String targetDN = firstValueAsString(sr, "targetDN");
      DN targetBaseDN = DN.rootDN();
      try
      {
@@ -1406,7 +1402,7 @@
      waitUntilResetChangeNumberTaskEnds(connDest, taskDN);
      return SUCCESSFUL;
    }
    catch (ReplicationCliException | NamingException | LdapException | NullPointerException e)
    catch (ReplicationCliException | IOException | NullPointerException e)
    {
      errPrintln(ERROR_RESET_CHANGE_NUMBER_EXCEPTION.get(e.getLocalizedMessage()));
      return ERROR_RESET_CHANGE_NUMBER_PROBLEM;
@@ -1417,21 +1413,16 @@
  {
    try
    {
      SearchControls ctls = new SearchControls();
      ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
      ctls.setReturningAttributes(new String[] {"lastChangeNumber"});
      NamingEnumeration<SearchResult> results = conn.getLdapContext().search(new LdapName(""), "objectclass=*", ctls);
      if (results.hasMore()) {
        return getFirstValue(results.next(), "lastChangeNumber");
      SearchResultEntry sr =
          conn.getConnection().searchSingleEntry("", BASE_OBJECT, "objectclass=*", "lastChangeNumber");
      return firstValueAsString(sr, "lastChangeNumber");
      }
    }
    catch (NamingException e)
    catch (LdapException e)
    {
      errPrintln(ERROR_RESET_CHANGE_NUMBER_EXCEPTION.get(e.getLocalizedMessage()));
    }
    return "";
  }
  }
  private void waitUntilResetChangeNumberTaskEnds(ConnectionWrapper conn, String taskDN)
      throws ReplicationCliException
@@ -1557,23 +1548,28 @@
    String taskID = null;
    while (!taskCreated)
    {
      BasicAttributes attrs = PurgeHistoricalUserData.getTaskAttributes(uData);
      dn = PurgeHistoricalUserData.getTaskDN(attrs);
      taskID = PurgeHistoricalUserData.getTaskID(attrs);
      List<RawAttribute> rawAttrs = TaskClient.getTaskAttributes(new PurgeHistoricalScheduleInformation(uData));
      dn = TaskClient.getTaskDN(rawAttrs);
      taskID = TaskClient.getTaskID(rawAttrs);
      AddRequest request = newAddRequest(dn);
      for (RawAttribute rawAttr : rawAttrs)
      {
        request.addAttribute(new LinkedAttribute(rawAttr.getAttributeType(), rawAttr.getValues()));
      }
      try
      {
        DirContext dirCtx = conn.getLdapContext().createSubcontext(dn, attrs);
        conn.getConnection().add(request);
        taskCreated = true;
        logger.info(LocalizableMessage.raw("created task entry: "+attrs));
        dirCtx.close();
        logger.info(LocalizableMessage.raw("created task entry: " + request));
      }
      catch (NamingException ne)
      catch (LdapException e)
      {
        logger.error(LocalizableMessage.raw("Error creating task "+attrs, ne));
        logger.error(LocalizableMessage.raw("Error creating task " + request, e));
        LocalizableMessage msg = ERR_LAUNCHING_PURGE_HISTORICAL.get();
        ReplicationCliReturnCode code = ERROR_LAUNCHING_PURGE_HISTORICAL;
        throw new ReplicationCliException(
            getThrowableMsg(msg, ne), code, ne);
        throw new ReplicationCliException(getThrowableMsg(msg, e), code, e);
      }
    }
@@ -1585,21 +1581,21 @@
      sleepCatchInterrupt(500);
      try
      {
        SearchResult sr = getFirstSearchResult(conn, dn,
        SearchResultEntry sr = getFirstSearchResult(conn, dn,
            "ds-task-log-message",
            "ds-task-state",
            "ds-task-purge-conflicts-historical-purged-values-count",
            "ds-task-purge-conflicts-historical-purge-completed-in-time",
            "ds-task-purge-conflicts-historical-purge-completed-in-time",
            "ds-task-purge-conflicts-historical-last-purged-changenumber");
        String logMsg = getFirstValue(sr, "ds-task-log-message");
        String logMsg = firstValueAsString(sr, "ds-task-log-message");
        if (logMsg != null && !logMsg.equals(lastLogMsg))
        {
          logger.info(LocalizableMessage.raw(logMsg));
          lastLogMsg = logMsg;
        }
        String state = getFirstValue(sr, "ds-task-state");
        String state = firstValueAsString(sr, "ds-task-state");
        TaskState taskState = TaskState.fromString(state);
        if (TaskState.isDone(taskState) || taskState == STOPPED_BY_ERROR)
        {
@@ -1619,15 +1615,14 @@
          }
        }
      }
      catch (NameNotFoundException x)
      catch (EntryNotFoundException x)
      {
        isOver = true;
      }
      catch (NamingException ne)
      catch (LdapException e)
      {
        LocalizableMessage msg = ERR_READING_SERVER_TASK_PROGRESS.get();
        throw new ReplicationCliException(
          getThrowableMsg(msg, ne), ERROR_CONNECTING, ne);
        throw new ReplicationCliException(getThrowableMsg(msg, e), ERROR_CONNECTING, e);
      }
    }
@@ -1638,24 +1633,11 @@
    return returnCode;
  }
  private SearchResult getFirstSearchResult(ConnectionWrapper conn, String dn, String... returnedAttributes)
      throws NamingException
  private SearchResultEntry getFirstSearchResult(ConnectionWrapper conn, String dn, String... returnedAttributes)
      throws LdapException
  {
    SearchControls searchControls = new SearchControls();
    searchControls.setCountLimit(1);
    searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
    searchControls.setReturningAttributes(returnedAttributes);
    NamingEnumeration<SearchResult> res = conn.getLdapContext().search(dn, "objectclass=*", searchControls);
    try
    {
      SearchResult sr = null;
      sr = res.next();
      return sr;
    }
    finally
    {
      res.close();
    }
    SearchRequest request = newSearchRequest(dn, BASE_OBJECT, "(objectclass=*)", returnedAttributes).setSizeLimit(1);
    return conn.getConnection().searchSingleEntry(request);
  }
  private LocalizableMessage getPurgeErrorMsg(String lastLogMsg, String state, ConnectionWrapper conn)