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

matthew_swift
04.27.2007 cc08b5573ab5fb3b88ef64a48d3edeae475b852a
Move the DNBuilder implementation into the core ManagedObjectPath
class and add the following methods:

ManagedObjectPath.toDN()

Converts a ManagedObjectPath to a DN


ManagedObjectPath.matches(ManagedObjectPath)

Compares the two paths to see if they refer to the same location.
This differs from equals() which performs an exact comparisons
where types must match as well.

This will be required in order to support aggregations, where
property values are represented using just their common name,
but their LDAP encoding is the full DN.
4 files modified
342 ■■■■■ changed files
opends/src/server/org/opends/server/admin/ManagedObjectPath.java 130 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/admin/server/DNBuilder.java 116 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java 84 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java 12 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/admin/ManagedObjectPath.java
@@ -38,6 +38,12 @@
import org.opends.server.admin.std.client.RootCfgClient;
import org.opends.server.admin.std.meta.RootCfgDefn;
import org.opends.server.admin.std.server.RootCfg;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.RDN;
@@ -114,6 +120,95 @@
    S extends Configuration> {
  /**
   * A serialize which is used to generate the toDN representation.
   */
  private static final class DNSerializer implements
      ManagedObjectPathSerializer {
    // The current DN.
    private DN dn;
    // The LDAP profile.
    private final LDAPProfile profile;
    // Create a new DN builder.
    private DNSerializer() {
      this.dn = DN.nullDN();
      this.profile = LDAPProfile.getInstance();
    }
    /**
     * {@inheritDoc}
     */
    public <C extends ConfigurationClient, S extends Configuration>
    void appendManagedObjectPathElement(
        InstantiableRelationDefinition<? super C, ? super S> r,
        AbstractManagedObjectDefinition<C, S> d, String name) {
      // Add the RDN sequence representing the relation.
      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
      // Now add the single RDN representing the named instance.
      String type = profile.getInstantiableRelationChildRDNType(r);
      AttributeType atype = DirectoryServer.getAttributeType(
          type.toLowerCase(), true);
      AttributeValue avalue = new AttributeValue(atype, name);
      dn = dn.concat(RDN.create(atype, avalue));
    }
    /**
     * {@inheritDoc}
     */
    public <C extends ConfigurationClient, S extends Configuration>
    void appendManagedObjectPathElement(
        OptionalRelationDefinition<? super C, ? super S> r,
        AbstractManagedObjectDefinition<C, S> d) {
      // Add the RDN sequence representing the relation.
      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
    }
    /**
     * {@inheritDoc}
     */
    public <C extends ConfigurationClient, S extends Configuration>
    void appendManagedObjectPathElement(
        SingletonRelationDefinition<? super C, ? super S> r,
        AbstractManagedObjectDefinition<C, S> d) {
      // Add the RDN sequence representing the relation.
      appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
    }
    // Appends the RDN sequence representing the provided relation.
    private void appendManagedObjectPathElement(RelationDefinition<?, ?> r) {
      // Add the RDN sequence representing the relation.
      try {
        DN localName = DN.decode(profile.getRelationRDNSequence(r));
        dn = dn.concat(localName);
      } catch (DirectoryException e) {
        throw new RuntimeException(e);
      }
    }
    // Gets the serialized DN value.
    private DN toDN() {
      return dn;
    }
  }
  /**
   * Abstract path element.
   */
  private static abstract class Element<C extends ConfigurationClient,
@@ -943,6 +1038,27 @@
  /**
   * Determines whether this managed object path references the same
   * location as the provided managed object path.
   * <p>
   * This method differs from <code>equals</code> in that it ignores
   * sub-type definitions.
   *
   * @param other
   *          The managed object path to be compared.
   * @return Returns <code>true</code> if this managed object path
   *         references the same location as the provided managed
   *         object path.
   */
  public boolean matches(ManagedObjectPath<?, ?> other) {
    DN thisDN = toDN();
    DN otherDN = other.toDN();
    return thisDN.equals(otherDN);
  }
  /**
   * Creates a new parent managed object path representing the
   * immediate parent of this path. This method is a short-hand for
   * <code>parent(1)</code>.
@@ -1065,6 +1181,20 @@
  /**
   * Creates a DN representation of this managed object path.
   *
   * @return Returns a DN representation of this managed object path.
   */
  public DN toDN() {
    // Use a simple serializer to create the contents.
    DNSerializer serializer = new DNSerializer();
    serialize(serializer);
    return serializer.toDN();
  }
  /**
   * {@inheritDoc}
   */
  @Override
opends/src/server/org/opends/server/admin/server/DNBuilder.java
@@ -29,41 +29,30 @@
import org.opends.server.admin.AbstractManagedObjectDefinition;
import org.opends.server.admin.Configuration;
import org.opends.server.admin.ConfigurationClient;
import org.opends.server.admin.InstantiableRelationDefinition;
import org.opends.server.admin.LDAPProfile;
import org.opends.server.admin.ManagedObjectPath;
import org.opends.server.admin.ManagedObjectPathSerializer;
import org.opends.server.admin.OptionalRelationDefinition;
import org.opends.server.admin.RelationDefinition;
import org.opends.server.admin.SingletonRelationDefinition;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.RDN;
/**
 * A strategy for creating <code>DN</code>s from managed object paths.
 * A factory class for creating <code>DN</code>s from managed
 * object paths.
 */
final class DNBuilder implements ManagedObjectPathSerializer {
final class DNBuilder {
  /**
   * Creates a new DN representing the specified managed object path.
   *
   * @param path
   *          The managed object path.
   * @return Returns a new DN representing the specified managed object path.
   * @return Returns a new DN representing the specified managed
   *         object path.
   */
  public static DN create(ManagedObjectPath<?, ?> path) {
    DNBuilder builder = new DNBuilder();
    path.serialize(builder);
    return builder.getInstance();
    return path.toDN();
  }
@@ -81,61 +70,12 @@
   */
  public static DN create(ManagedObjectPath<?, ?> path,
      RelationDefinition<?, ?> relation) {
    DNBuilder builder = new DNBuilder();
    path.serialize(builder);
    builder.appendManagedObjectPathElement(relation);
    return builder.getInstance();
  }
    DN dn = path.toDN();
  // The current DN.
  private DN dn;
  // The LDAP profile.
  private final LDAPProfile profile;
  /**
   * Create a new DN builder.
   */
  public DNBuilder() {
    this.dn = DN.nullDN();
    this.profile = LDAPProfile.getInstance();
  }
  /**
   * {@inheritDoc}
   */
  public <C extends ConfigurationClient, S extends Configuration>
      void appendManagedObjectPathElement(
          InstantiableRelationDefinition<? super C, ? super S> r,
          AbstractManagedObjectDefinition<C, S> d, String name) {
    // Add the RDN sequence representing the relation.
    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
    // Now add the single RDN representing the named instance.
    String type = profile.getInstantiableRelationChildRDNType(r);
    AttributeType atype = DirectoryServer.getAttributeType(type.toLowerCase(),
                                                           true);
    AttributeValue avalue = new AttributeValue(atype, name);
    dn = dn.concat(RDN.create(atype, avalue));
  }
  /**
   * Appends the RDN sequence representing the provided relation.
   *
   * @param r
   *          The relation definition.
   */
  public void appendManagedObjectPathElement(RelationDefinition<?, ?> r) {
    // Add the RDN sequence representing the relation.
    try {
      DN localName = DN.decode(profile.getRelationRDNSequence(r));
      dn = dn.concat(localName);
      LDAPProfile profile = LDAPProfile.getInstance();
      DN localName = DN.decode(profile.getRelationRDNSequence(relation));
      return dn.concat(localName);
    } catch (DirectoryException e) {
      throw new RuntimeException(e);
    }
@@ -143,38 +83,8 @@
  /**
   * {@inheritDoc}
   */
  public <C extends ConfigurationClient, S extends Configuration>
      void appendManagedObjectPathElement(
          OptionalRelationDefinition<? super C, ? super S> r,
          AbstractManagedObjectDefinition<C, S> d) {
    // Add the RDN sequence representing the relation.
    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
  }
  /**
   * {@inheritDoc}
   */
  public <C extends ConfigurationClient, S extends Configuration>
      void appendManagedObjectPathElement(
          SingletonRelationDefinition<? super C, ? super S> r,
          AbstractManagedObjectDefinition<C, S> d) {
    // Add the RDN sequence representing the relation.
    appendManagedObjectPathElement((RelationDefinition<?, ?>) r);
  }
  /**
   * Create a new DN using the current state of this DN builder.
   *
   * @return Returns the new DN instance.
   */
  public DN getInstance() {
    return dn;
  // Prevent instantiation.
  private DNBuilder() {
    // No implementation required.
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/ManagedObjectPathTest.java
@@ -47,6 +47,7 @@
import org.opends.server.admin.std.server.ConnectionHandlerCfg;
import org.opends.server.admin.std.server.GlobalCfg;
import org.opends.server.admin.std.server.LDAPConnectionHandlerCfg;
import org.opends.server.types.DN;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -258,4 +259,87 @@
        ManagedObjectPath
            .valueOf("/relation=synchronization-provider+type=multimaster-synchronization-provider+name=MMR/relation=multimaster-domain+name=Domain"));
  }
  /**
   * Tests matches and equals methods behave differently.
   */
  @Test
  public void testMatches() {
    ManagedObjectPath<?, ?> path = ManagedObjectPath.emptyPath();
    ManagedObjectPath<ConnectionHandlerCfgClient, ConnectionHandlerCfg> child1 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            "LDAP connection handler");
    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child2 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            LDAPConnectionHandlerCfgDefn.getInstance(),
            "LDAP connection handler");
    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child3 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            LDAPConnectionHandlerCfgDefn.getInstance(),
            "Another LDAP connection handler");
    assertTrue(child1.matches(child1));
    assertTrue(child2.matches(child2));
    assertTrue(child1.matches(child2));
    assertTrue(child2.matches(child1));
    assertTrue(child1.equals(child1));
    assertTrue(child2.equals(child2));
    assertFalse(child1.equals(child2));
    assertFalse(child2.equals(child1));
    assertFalse(child1.matches(child3));
    assertFalse(child2.matches(child3));
    assertFalse(child3.matches(child1));
    assertFalse(child3.matches(child2));
    assertFalse(child1.equals(child3));
    assertFalse(child2.equals(child3));
    assertFalse(child3.equals(child1));
    assertFalse(child3.equals(child2));
  }
  /**
   * Tests toDN method.
   *
   * @throws Exception
   *           If an unexpected error occurred.
   */
  @Test
  public void testToDN() throws Exception {
    ManagedObjectPath<?, ?> path = ManagedObjectPath.emptyPath();
    ManagedObjectPath<ConnectionHandlerCfgClient, ConnectionHandlerCfg> child1 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            "LDAP connection handler");
    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child2 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            LDAPConnectionHandlerCfgDefn.getInstance(),
            "LDAP connection handler");
    ManagedObjectPath<LDAPConnectionHandlerCfgClient, LDAPConnectionHandlerCfg> child3 = path
        .child(RootCfgDefn.getInstance()
            .getConnectionHandlersRelationDefinition(),
            LDAPConnectionHandlerCfgDefn.getInstance(),
            "Another LDAP connection handler");
    DN expectedEmpty = DN.nullDN();
    DN expectedChild1 = DN.decode("cn=LDAP connection handler,cn=connection handlers,cn=config");
    DN expectedChild2 = DN.decode("cn=LDAP connection handler,cn=connection handlers,cn=config");
    DN expectedChild3 = DN.decode("cn=Another LDAP connection handler,cn=connection handlers,cn=config");
    assertEquals(path.toDN(), expectedEmpty);
    assertEquals(child1.toDN(), expectedChild1);
    assertEquals(child2.toDN(), expectedChild2);
    assertEquals(child3.toDN(), expectedChild3);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/server/DNBuilderTest.java
@@ -101,9 +101,7 @@
        .getTestChildrenRelationDefinition(), "test-child-1");
    // Now serialize it.
    DNBuilder builder = new DNBuilder();
    path.serialize(builder);
    DN actual = builder.getInstance();
    DN actual = DNBuilder.create(path);
    DN expected = DN
        .decode("cn=test-child-1,cn=test children,cn=test-parent-1,cn=test parents,cn=config");
@@ -152,9 +150,7 @@
    // Now serialize it.
    LDAPProfile.getInstance().pushWrapper(wrapper);
    try {
      DNBuilder builder = new DNBuilder();
      path.serialize(builder);
      DN actual = builder.getInstance();
      DN actual = DNBuilder.create(path);
      DN expected = DN
          .decode("cn=singleton-test-child,cn=test-parent-1,cn=test parents,cn=config");
@@ -184,9 +180,7 @@
        .getOptionalTestChildRelationDefinition());
    // Now serialize it.
    DNBuilder builder = new DNBuilder();
    path.serialize(builder);
    DN actual = builder.getInstance();
    DN actual = DNBuilder.create(path);
    DN expected = DN
        .decode("cn=optional test child,cn=test-parent-1,cn=test parents,cn=config");