Package netscape.ldap

Class LDAPSchema

java.lang.Object
netscape.ldap.LDAPSchema
All Implemented Interfaces:
Serializable

public class LDAPSchema extends Object implements Serializable
This object represents the schema of an LDAP v3 server. You can use the fetchSchema method to retrieve the schema used by a server. (The server must support LDAP v3 and the capability to retrieve the schema over the LDAP protocol.)

After you retrieve the schema, you can use this object to get the object class, attribute type, and matching rule descriptions in the schema. You can also add your own object classes, attribute types, and matching rules to the schema.

To remove any object classes, attribute types, and matching rules that you added, call the remove methods of the LDAPObjectClassSchema, LDAPAttributeSchema, and LDAPMatchingRuleSchema classes. (This method is inherited from the LDAPSchemaElement class.)

The following class is an example of an LDAP client that can fetch the schema, get and print object class descriptions and attribute type descriptions, and add object classes and attribute types to the schema over the LDAP protocol.

 import netscape.ldap.*;
 public class TestSchema {
     public static void main( String[] args ) {
         String HOSTNAME = "ldap.netscape.com";
         int PORT_NUMBER = DEFAULT_PORT;
         String ROOT_DN = "cn=Directory Manager";
         String ROOT_PASSWORD = "23skidoo";

         LDAPConnection ld = new LDAPConnection();

         // Construct a new LDAPSchema object to get the schema.
         LDAPSchema dirSchema = new LDAPSchema();

         try {
             // Connect to the server.
             ld.connect( HOSTNAME, PORT_NUMBER );

             // Get the schema from the directory.
             dirSchema.fetchSchema( ld );

             // Get and print the inetOrgPerson object class description.
             LDAPObjectClassSchema objClass = dirSchema.getObjectClass(
                 "inetOrgPerson" );
             if ( objClass != null ) {
                 System.out.println("inetOrgPerson := "+objClass.toString());
             }

             // Get and print the definition of the userPassword attribute.
             LDAPAttributeSchema attrType = dirSchema.getAttribute(
                 "userpassword" );
             if ( attrType != null ) {
                 System.out.println("userPassword := " + attrType.toString());
             }

             // Create a new object class definition.
             String[] requiredAttrs = {"cn", "mail"};
             String[] optionalAttrs = {"sn", "phoneNumber"};
             LDAPObjectClassSchema newObjClass =
                     new LDAPObjectClassSchema( "newInetOrgPerson",
                                                "1.2.3.4.5.6.7",
                                                "top",
                                                "Experiment",
                                                requiredAttrs,
                                                optionalAttrs );

             // Authenticate as root DN to get permissions to edit the schema.
             ld.authenticate( ROOT_DN, ROOT_PASSWORD );

             // Add the new object class to the schema.
             newObjClass.add( ld );

             // Create a new attribute type "hairColor".
             LDAPAttributeSchema newAttrType =
                     new LDAPAttributeSchema( "hairColor",
                                              "1.2.3.4.5.4.3.2.1",
                                              "Blonde, red, etc",
                                              LDAPAttributeSchema.cis,
                                              false );
             // Add a custom qualifier
             newObjClass.setQualifier( "X-OWNER", "John Jacobson" );

             // Add the new attribute type to the schema.
             newAttrType.add( ld );

             // Fetch the schema again to verify that changes were made.
             dirSchema.fetchSchema( ld );

             // Get and print the new attribute type.
             newAttrType = dirSchema.getAttribute( "hairColor" );
             if ( newAttrType != null ) {
                 System.out.println("hairColor := " + newAttrType.toString());
             }

             // Get and print the new object class.
             newObjClass = dirSchema.getObjectClass( "newInetOrgPerson" );
             if ( newObjClass != null ) {
                 System.out.println("newInetOrgPerson := " +newObjClass.toString());
             }

             ld.disconnect();

         } catch ( Exception e ) {
             System.err.println( e.toString() );
             System.exit( 1 );
         }

         System.exit( 0 );
     }
 }
 
If you are using the Netscape Directory Server 3.0, you can also verify that the class and attribute type have been added through the directory server manager (go to Schema | Edit or View Attributes or Schema | Edit or View Object Classes).

To remove the classes and attribute types added by the example, see the examples under the LDAPSchemaElement class.

Version:
1.0
Author:
Rob Weltman
See Also: