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

Matthew Swift
19.39.2011 b7b1ee33661feee0d7ebf39bdc29f61e43bd2b50
Fix OPENDJ-159 - LDAP connections use stale default schema if it is changed after factory creation

Modify the default schema resolution to dynamically retrieve the default schema each time it is called.
4 files modified
89 ■■■■■ changed files
opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java 3 ●●●● patch | view | raw | blame | history
opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java 15 ●●●●● patch | view | raw | blame | history
opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java 65 ●●●●● patch | view | raw | blame | history
opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java 6 ●●●● patch | view | raw | blame | history
opendj3/opendj-sdk/src/main/java/org/opends/sdk/DecodeOptions.java
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 */
package org.opends.sdk;
@@ -92,7 +93,7 @@
  {
    this.attributeFactory = LinkedAttribute.FACTORY;
    this.entryFactory = LinkedHashMapEntry.FACTORY;
    this.schemaResolver = new FixedSchemaResolver(Schema.getDefaultSchema());
    this.schemaResolver = SchemaResolver.DEFAULT;
  }
opendj3/opendj-sdk/src/main/java/org/opends/sdk/SchemaResolver.java
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 */
package org.opends.sdk;
@@ -47,6 +48,20 @@
 */
public interface SchemaResolver
{
  /**
   * A schema resolver which always returns the current default schema as
   * returned by {@link Schema#getDefaultSchema()}.
   */
  public static final SchemaResolver DEFAULT = new SchemaResolver()
  {
    public Schema resolveSchema(String dn)
    {
      return Schema.getDefaultSchema();
    }
  };
  /**
   * Finds the appropriate schema for use with the provided distinguished name.
opendj3/opendj-sdk/src/test/java/org/opends/sdk/ConnectionFactoryTestCase.java
@@ -23,11 +23,13 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 */
package org.opends.sdk;
import static org.fest.assertions.Assertions.assertThat;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
@@ -43,6 +45,9 @@
import org.opends.sdk.requests.DigestMD5SASLBindRequest;
import org.opends.sdk.requests.Requests;
import org.opends.sdk.requests.SearchRequest;
import org.opends.sdk.responses.SearchResultEntry;
import org.opends.sdk.schema.Schema;
import org.opends.sdk.schema.SchemaBuilder;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
@@ -301,8 +306,66 @@
  {
    final Connection con = factory.getConnection();
    assertNotNull(con);
    // quickly check if iit is a valid connection.
    // quickly check if it is a valid connection.
    assertTrue(con.readRootDSE().getEntry().getName().isRootDN());
    con.close();
  }
  /**
   * Verifies that LDAP connections take into consideration changes to the
   * default schema post creation. See OPENDJ-159.
   * <p>
   * This test is disabled because it cannot be run in parallel with rest of the
   * test suite, because it modifies the global default schema.
   *
   * @throws Exception
   *           If an unexpected error occurred.
   */
  @Test(enabled = false)
  public void testSchemaUsage() throws Exception
  {
    // Create a connection factory: this should always use the default schema,
    // even if it is updated.
    final ConnectionFactory factory = new LDAPConnectionFactory(
        "localhost", TestCaseUtils.getLdapPort());
    final Schema defaultSchema = Schema.getDefaultSchema();
    final Connection connection = factory.getConnection();
    try
    {
      // Simulate a client which reads the schema from the server and then
      // sets it as the application default. We then want subsequent
      // operations to use this schema, not the original default.
      final SchemaBuilder builder = new SchemaBuilder(
          Schema.getCoreSchema());
      builder
          .addAttributeType(
              "( 0.9.2342.19200300.100.1.3 NAME 'mail' EQUALITY "
                  + "caseIgnoreIA5Match SUBSTR caseIgnoreIA5SubstringsMatch "
                  + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26{256} )",
              false);
      final Schema testSchema = builder.toSchema().nonStrict();
      assertThat(testSchema.getWarnings()).isEmpty();
      Schema.setDefaultSchema(testSchema);
      // Read an entry containing the mail attribute.
      final SearchResultEntry e = connection
          .readEntry("uid=user.0,ou=people,o=test");
      assertThat(e.getAttribute("mail")).isNotNull();
      assertThat(
          e.getAttribute(AttributeDescription.valueOf("mail",
              testSchema))).isNotNull();
    }
    finally
    {
      // Restore original schema.
      Schema.setDefaultSchema(defaultSchema);
      // Close connection.
      connection.close();
    }
  }
}
opendj3/opendj-sdk/src/test/java/org/opends/sdk/LDAPServer.java
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 */
package org.opends.sdk;
@@ -708,10 +709,13 @@
      final String sn = String.format("sn: %d", i);
      final String uid = String.format("uid: user.%d", i);
      // See org.opends.sdk.ConnectionFactoryTestCase.testSchemaUsage().
      final String mail = String.format("mail: user.%d@example.com", i);
      final DN d = DN.valueOf(dn);
      final Entry e = new LinkedHashMapEntry("dn: " + dn,
          "objectclass: person", "objectclass: inetorgperson",
          "objectclass: top", cn, sn, uid);
          "objectclass: top", cn, sn, uid, mail);
      entryMap.put(d, Entries.unmodifiableEntry(e));
    }
  }