Example usage for javax.naming.directory DirContext createSubcontext

List of usage examples for javax.naming.directory DirContext createSubcontext

Introduction

In this page you can find the example usage for javax.naming.directory DirContext createSubcontext.

Prototype

public DirContext createSubcontext(String name, Attributes attrs) throws NamingException;

Source Link

Document

Creates and binds a new context, along with associated attributes.

Usage

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Creates a user. String array contains:
 * 1) first name/* w w  w  .j av a 2  s .c o m*/
 * 2) surname
 * 3) email
 * 4) username
 * 5) password
 * 
 * @param userDetails
 * @return
 * @throws NamingException
 */
public boolean createNewUser(UserLogin userLogin) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("sn", userLogin.getSurname()));
    attributes.put(new BasicAttribute("givenName", userLogin.getFirstName()));
    attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname()));
    attributes.put(new BasicAttribute("mail", userLogin.getEmail()));
    if (userLogin.getTelephone() != null) {
        attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone()));
    }
    attributes.put(new BasicAttribute("userPassword", userLogin.getPassword()));
    attributes.put(new BasicAttribute("objectClass", "top"));
    attributes.put(new BasicAttribute("objectClass", "person"));
    attributes.put(new BasicAttribute("objectClass", "organizationalPerson"));
    attributes.put(new BasicAttribute("objectClass", "inetorgperson"));
    String contextName = "uid=" + userLogin.getUsername();
    String fullContextName = contextName + "," + ctx.getNameInNamespace();

    //add the user to ldap
    ctx.createSubcontext(contextName, attributes);

    //need to add user to group
    for (int i = 0; i < userGroups.length; i++) {
        DirContext groupContext = getGroupContext();
        Attributes groupAttributes = groupContext.getAttributes(userGroups[i]);
        groupAttributes.get("uniqueMember").add(fullContextName);
        groupContext.modifyAttributes(userGroups[i], DirContext.REPLACE_ATTRIBUTE, groupAttributes);
    }
    return true;
}

From source file:CreateJavaSchema.java

protected void updateObjectClasses(DirContext ocRoot, String[] ocIDs) throws NamingException {
    /* Get rid of old OCs - reverse order */
    for (int i = ocIDs.length - 1; i >= 0; i--) {
        ocRoot.destroySubcontext(ocIDs[i]);
    }//from w  w  w .j  a v a2  s.c  om

    // javaContainer
    Attributes attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.1");
    attrs.put("NAME", "javaContainer");
    attrs.put("DESC", "Container for a Java object");
    attrs.put("SUP", "top");
    attrs.put("STRUCTURAL", "true");
    Attribute jcMust = new BasicAttribute("MUST", "cn");

    if (netscape41bug) {
        jcMust.add("objectClass");
    }
    attrs.put(jcMust);

    ocRoot.createSubcontext("javaContainer", attrs);
    System.out.println("Created javaContainer object class");

    // javaObject
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.4");
    attrs.put("NAME", "javaObject");
    attrs.put("DESC", "Java object representation");
    attrs.put("SUP", "top");
    attrs.put("ABSTRACT", "true");
    Attribute joMust = new BasicAttribute("MUST", "javaClassName");

    if (netscape41bug) {
        joMust.add("objectClass");
    }
    attrs.put(joMust);

    Attribute optional = new BasicAttribute("MAY", "javaCodebase");
    optional.add("javaClassNames");
    optional.add("javaDoc");
    optional.add("description");
    attrs.put(optional);
    ocRoot.createSubcontext("javaObject", attrs);
    System.out.println("Created javaObject object class");

    // javaSerializedObject
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.5");
    attrs.put("NAME", "javaSerializedObject");
    attrs.put("DESC", "Java serialized object");
    attrs.put("SUP", "javaObject");
    attrs.put("AUXILIARY", "true");
    Attribute jsoMust = new BasicAttribute("MUST", "javaSerializedData");

    if (netscape41bug) {
        jsoMust.add("objectClass");
    }

    if (netscapebug) {
        // Netscape ignores 'SUP' so we must add explicitly
        attrs.put(optional);
        jsoMust.add("javaClassName");
    }
    attrs.put(jsoMust);
    ocRoot.createSubcontext("javaSerializedObject", attrs);
    System.out.println("Created javaSerializedObject object class");

    // javaMarshalledObject
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.8");
    attrs.put("NAME", "javaMarshalledObject");
    attrs.put("DESC", "Java marshalled object");
    attrs.put("SUP", "javaObject");
    attrs.put("AUXILIARY", "true");

    if (netscapebug) {
        // Netscape ignores 'SUP' so we must add explicitly
        attrs.put(optional);
    }
    attrs.put(jsoMust); // re-use the MUST from javaSerializedObject
    ocRoot.createSubcontext("javaMarshalledObject", attrs);
    System.out.println("Created javaMarshalledObject object class");

    // javaNamingReference
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.7");
    attrs.put("NAME", "javaNamingReference");
    attrs.put("DESC", "JNDI reference");
    attrs.put("SUP", "javaObject");
    attrs.put("AUXILIARY", "true");

    if (netscapebug) {
        // Netscape ignores 'SUP' so we must add explicitly
        attrs.put("MUST", "javaClassName");
    } else {
        optional = new BasicAttribute("MAY");
    }

    optional.add("javaReferenceAddress");
    optional.add("javaFactory");
    attrs.put(optional);
    ocRoot.createSubcontext("javaNamingReference", attrs);
    System.out.println("Created javaNamingReference object class");
}

From source file:CreateJavaSchema.java

protected void updateAttributes(DirContext attrRoot, String[] attrIDs) throws NamingException {

    /* Get rid of old attr IDs */
    for (int i = 0; i < attrIDs.length; i++) {
        attrRoot.destroySubcontext(attrIDs[i]);
    }//from w  ww.  jav  a2  s.  co  m

    // javaSerializedData
    Attributes attrs = new BasicAttributes(true); // ignore case
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.8");
    attrs.put("NAME", "javaSerializedData");
    attrs.put("DESC", "Serialized form of a Java object");
    if (netscape41bug) {
        // DS 4.1 doesn't like Octet String
        attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.5");
    } else {
        attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.40");
    }

    attrs.put("SINGLE-VALUE", "true");
    attrRoot.createSubcontext("javaSerializedData", attrs);
    System.out.println("Created javaSerializedData attribute");

    // javaCodebase
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.7");
    attrs.put("NAME", "javaCodebase");
    attrs.put("DESC", "URL(s) specifying the location of class definition");
    attrs.put("EQUALITY", "caseExactIA5Match");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.26");
    attrRoot.createSubcontext("javaCodebase", attrs);
    System.out.println("Created javaCodebase attribute");

    // javaClassName
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.6");
    attrs.put("NAME", "javaClassName");
    attrs.put("DESC", "Fully qualified name of distinguished class or interface");
    attrs.put("EQUALITY", "caseExactMatch");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrs.put("SINGLE-VALUE", "true");
    attrRoot.createSubcontext("javaClassName", attrs);
    System.out.println("Created javaClassName attribute");

    // javaClassNames
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.13");
    attrs.put("NAME", "javaClassNames");
    attrs.put("DESC", "Fully qualified Java class or interface name");
    attrs.put("EQUALITY", "caseExactMatch");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrRoot.createSubcontext("javaClassNames", attrs);
    System.out.println("Created javaClassNames attribute");

    // javaFactory
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.10");
    attrs.put("NAME", "javaFactory");
    attrs.put("DESC", "Fully qualified Java class name of a JNDI object factory");
    attrs.put("EQUALITY", "caseExactMatch");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrs.put("SINGLE-VALUE", "true");
    attrRoot.createSubcontext("javaFactory", attrs);
    System.out.println("Created javaFactory attribute");

    // javaReferenceAddress
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.11");
    attrs.put("NAME", "javaReferenceAddress");
    attrs.put("DESC", "Addresses associated with a JNDI Reference");
    attrs.put("EQUALITY", "caseExactMatch");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrRoot.createSubcontext("javaReferenceAddress", attrs);
    System.out.println("Created javaReferenceAddress attribute");

    // javaDoc
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.12");
    attrs.put("NAME", "javaDoc");
    attrs.put("DESC", "The Java documentation for the class");
    attrs.put("EQUALITY", "caseExactIA5Match");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.26");
    attrRoot.createSubcontext("javaDoc", attrs);
    System.out.println("Created javaDoc attribute");
}

From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java

protected void updateGroups(DirContext dirContext, SearchControls controls) {
    String ldapSuffix = getLDAPSuffix();
    String ldapGroups = getLDAPGroups();
    NamingEnumeration<SearchResult> namingEnum = null;
    try {//from   ww w  .  ja  v a2s .c  om
        boolean schemaExists = false;
        try {
            namingEnum = dirContext.search(ldapSuffix, ldapGroups, controls);
            schemaExists = namingEnum.hasMore();
        } catch (NamingException ex) {
            throw new RuntimeException("Could find groups schema", ex);
        }
        if (schemaExists) {
            logger.info("LDAP schema for " + ldapGroups + " already exists");
        } else {
            String dn = ldapGroups + "," + ldapSuffix;
            Attributes attributes = new BasicAttributes();
            Attribute objclass = new BasicAttribute("objectclass");
            objclass.add("organizationalUnit");
            attributes.put(objclass);
            attributes.put(ldapGroups.substring(0, ldapGroups.indexOf("=")),
                    ldapGroups.substring(ldapGroups.indexOf("=") + 1));
            try {
                dirContext.createSubcontext(dn, attributes);
            } catch (NamingException ex) {
                throw new RuntimeException("Could not create groups schema", ex);
            }
            logger.info("Created LDAP schema for " + ldapGroups);
        }
    } finally {
        if (namingEnum != null) {
            try {
                namingEnum.close();
            } catch (NamingException ex) {
                throw new RuntimeException("Could not close the naming enumeration for the ldap groups schema",
                        ex);
            }
        }
    }
}

From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java

protected void updatePeople(DirContext dirContext, SearchControls controls) {
    String ldapSuffix = getLDAPSuffix();
    String ldapPeople = getLDAPPeople();
    NamingEnumeration<SearchResult> namingEnum = null;
    try {// www .j  a va2 s  .c om
        boolean schemaExists = false;
        try {
            namingEnum = dirContext.search(ldapSuffix, ldapPeople, controls);
            schemaExists = namingEnum.hasMore();
        } catch (NamingException ex) {
            throw new RuntimeException("Could find people schema", ex);
        }
        if (schemaExists) {
            logger.info("LDAP schema for " + ldapPeople + " already exists");
        } else {
            String dn = ldapPeople + "," + ldapSuffix;
            Attributes attributes = new BasicAttributes();
            Attribute objclass = new BasicAttribute("objectclass");
            objclass.add("organizationalUnit");
            attributes.put(objclass);
            attributes.put(ldapPeople.substring(0, ldapPeople.indexOf("=")),
                    ldapPeople.substring(ldapPeople.indexOf("=") + 1));
            try {
                dirContext.createSubcontext(dn, attributes);
            } catch (NamingException ex) {
                throw new RuntimeException("Could not create people schema", ex);
            }
            logger.info("Created LDAP schema for " + ldapPeople);
        }
    } finally {
        if (namingEnum != null) {
            try {
                namingEnum.close();
            } catch (NamingException ex) {
                throw new RuntimeException("Could not close the naming enumeration for the ldap people schema",
                        ex);
            }
        }
    }
}

From source file:CreateCorbaSchema.java

protected void updateObjectClasses(DirContext ocRoot, String[] ocIDs) throws NamingException {

    /* Get rid of old OCs - reverse order */
    for (int i = ocIDs.length - 1; i >= 0; i--) {
        ocRoot.destroySubcontext(ocIDs[i]);
    }/*from   w w w  .  j  a va 2 s  .  c  o  m*/

    // corbaObject
    Attributes attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.9");
    attrs.put("NAME", "corbaObject");
    attrs.put("DESC", "CORBA object representation");
    attrs.put("SUP", "top");
    attrs.put("ABSTRACT", "true");
    Attribute optional = new BasicAttribute("MAY", "corbaRepositoryId");
    optional.add("description");
    attrs.put(optional);
    ocRoot.createSubcontext("corbaObject", attrs);
    System.out.println("Created corbaObject object class");

    // corbaObjectReference
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.11");
    attrs.put("NAME", "corbaObjectReference");
    attrs.put("DESC", "CORBA interoperable object reference");
    attrs.put("SUP", "corbaObject");
    attrs.put("AUXILIARY", "true");
    Attribute corMust = new BasicAttribute("MUST", "corbaIor");

    if (netscape41bug) {
        corMust.add("objectclass");
    }

    if (netscapebug) {
        // Netscape ignores 'SUP' so we must add explicitly
        attrs.put(optional);
    }
    attrs.put(corMust);
    ocRoot.createSubcontext("corbaObjectReference", attrs);
    System.out.println("Created corbaObjectReference object class");

    // corbaContainer
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.10");
    attrs.put("NAME", "corbaContainer");
    attrs.put("DESC", "Container for a CORBA object");
    attrs.put("SUP", "top");
    attrs.put("STRUCTURAL", "true");
    Attribute ccMust = new BasicAttribute("MUST", "cn");

    if (netscape41bug) {
        ccMust.add("objectclass");
    }

    attrs.put(ccMust);
    ocRoot.createSubcontext("corbaContainer", attrs);
    System.out.println("Created corbaContainer object class");
}

From source file:CreateCorbaSchema.java

/**
 * Add new attributes: corbaIor corbaRepositoryId
 *///from  w  w  w  . j  a v  a 2s. co m
protected void updateAttributes(DirContext attrRoot, String[] attrIDs) throws NamingException {

    /* Get rid of old attr IDs */
    for (int i = 0; i < attrIDs.length; i++) {
        attrRoot.destroySubcontext(attrIDs[i]);
    }

    /* Add new and updated attr definitions */
    // corbaIor
    Attributes attrs = new BasicAttributes(true); // ignore case
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.14");
    attrs.put("NAME", "corbaIor");
    attrs.put("DESC", "Stringified interoperable object reference of a CORBA object");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.26");
    attrs.put("EQUALITY", "caseIgnoreIA5Match");
    attrs.put("SINGLE-VALUE", "true");
    attrRoot.createSubcontext("corbaIor", attrs);
    System.out.println("Created corbaIor attribute");

    // corbaRepositoryId
    attrs = new BasicAttributes(true);
    attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.15");
    attrs.put("NAME", "corbaRepositoryId");
    attrs.put("DESC", "Repository ids of interfaces implemented by a CORBA object");
    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
    attrs.put("EQUALITY", "caseExactMatch");
    attrRoot.createSubcontext("corbaRepositoryId", attrs);
    System.out.println("Created corbaRepositoryId attribute");
}

From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java

protected void updateUser(String user, String encryptedPassword, DirContext dirContext,
        SearchControls controls) {
    NamingEnumeration<SearchResult> namingEnum = null;
    try {/*from  ww  w.  j  av a  2s  .c  om*/
        String ldapPeople = getLDAPPeople();
        String ldapSuffix = getLDAPSuffix();
        boolean schemaExists = false;
        try {
            namingEnum = dirContext.search(ldapPeople + "," + ldapSuffix, "uid=" + user, controls);
            schemaExists = namingEnum.hasMore();
        } catch (NamingException ex) {
            throw new RuntimeException("Could find schema for: " + user, ex);
        }
        if (schemaExists) {
            logger.info("LDAP schema for user " + user + " already exists");
        } else {
            String dn = "uid=" + user + "," + ldapPeople + "," + ldapSuffix;
            Attributes attributes = new BasicAttributes();
            Attribute objclass = new BasicAttribute("objectclass");
            objclass.add("inetOrgPerson");
            attributes.put(objclass);
            attributes.put("uid", user);
            attributes.put("sn", user);
            attributes.put("cn", user);
            attributes.put("userPassword", encryptedPassword);
            try {
                dirContext.createSubcontext(dn, attributes);
            } catch (NamingException ex) {
                throw new RuntimeException("Could not create schema for: " + user, ex);
            }
            logger.info("Created LDAP schema for " + user);
        }
    } finally {
        if (namingEnum != null) {
            try {
                namingEnum.close();
            } catch (NamingException ex) {
                throw new RuntimeException(
                        "Could not close the naming enumeration for the ldap schema: " + user, ex);
            }
        }
    }
}

From source file:CreateJavaSchema.java

/**
 * Inserts object class definitions from RFC 2713 into the schema.
 * /*from  w ww  .jav a 2s  . c  o m*/
 * This method maps the LDAP schema definitions in RFC 2713 onto the
 * proprietary attributes required by the Active Directory schema.
 * 
 * The resulting object class definitions differ from those of RFC 2713 in the
 * following ways:
 *  - Abstract and auxiliary classes are now defined as structural. - The
 * javaObject class now inherits from javaContainer. - The
 * javaNamingReference, javaSerializedObject and javaMarshalledObject now
 * inherit from javaObject.
 * 
 * The effect of these differences is that Java objects cannot be mixed-in
 * with other directory entries, they may only be stored as stand-alone
 * entries.
 * 
 * The reason for these differences is due to the way auxiliary classes are
 * supported the Active Directory. Only the names of structural classes (not
 * auxiliary) may appear in the object class attribute of an entry. Therefore,
 * the abstract and auxiliary classes in the Java schema definition are
 * re-defined as structural.
 */
protected void insertADObjectClasses(DirContext rootCtx, DirContext schemaCtx) throws NamingException {

    System.out.println("  [inserting new object class definitions ...]");

    String dn = schemaCtx.getNameInNamespace();
    String attrID;

    attrID = new String("javaContainer");
    Attributes attrs1 = new BasicAttributes();

    attrs1.put(new BasicAttribute("objectClass", "classSchema"));
    attrs1.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs1.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.1"));
    attrs1.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs1.put(new BasicAttribute("mustContain", "cn"));
    attrs1.put(new BasicAttribute("objectClassCategory", "1"));
    attrs1.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs1.put(new BasicAttribute("subclassOf", "top"));
    attrs1.put(new BasicAttribute("possSuperiors", "top")); // any superior
    attrs1.put(new BasicAttribute("description", "Container for a Java object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs1);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // because javaObject relys on javaContainer

    attrID = new String("javaObject");
    Attributes attrs2 = new BasicAttributes();

    attrs2.put(new BasicAttribute("objectClass", "classSchema"));
    attrs2.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs2.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.4"));
    attrs2.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs2.put(new BasicAttribute("mustContain", "javaClassName"));

    Attribute joMay = new BasicAttribute("mayContain");
    joMay.add("javaClassNames");
    joMay.add("javaCodeBase");
    joMay.add("javaDoc");
    joMay.add("description");
    attrs2.put(joMay);

    attrs2.put(new BasicAttribute("objectClassCategory", "1"));
    attrs2.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs2.put(new BasicAttribute("subclassOf", "javaContainer"));
    attrs2.put(new BasicAttribute("description", "Java object representation"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs2);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // because next 3 rely on javaObject

    attrID = new String("javaSerializedObject");
    Attributes attrs3 = new BasicAttributes();

    attrs3.put(new BasicAttribute("objectClass", "classSchema"));
    attrs3.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs3.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.5"));
    attrs3.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs3.put(new BasicAttribute("mustContain", "javaSerializedData"));
    attrs3.put(new BasicAttribute("objectClassCategory", "1"));
    attrs3.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs3.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs3.put(new BasicAttribute("description", "Java serialized object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs3);
    System.out.println("    [" + attrID + "]");

    attrID = new String("javaNamingReference");
    Attributes attrs4 = new BasicAttributes();

    attrs4.put(new BasicAttribute("objectClass", "classSchema"));
    attrs4.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs4.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.7"));
    attrs4.put(new BasicAttribute("lDAPDisplayName", attrID));

    Attribute jnrMay = new BasicAttribute("mayContain");
    jnrMay.add("javaReferenceAddress");
    jnrMay.add("javaFactory");
    attrs4.put(jnrMay);

    attrs4.put(new BasicAttribute("objectClassCategory", "1"));
    attrs4.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs4.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs4.put(new BasicAttribute("description", "JNDI reference"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs4);
    System.out.println("    [" + attrID + "]");

    attrID = new String("javaMarshalledObject");
    Attributes attrs5 = new BasicAttributes();

    attrs5.put(new BasicAttribute("objectClass", "classSchema"));
    attrs5.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs5.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.8"));
    attrs5.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs5.put(new BasicAttribute("mustContain", "javaSerializedData"));
    attrs5.put(new BasicAttribute("objectClassCategory", "1"));
    attrs5.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs5.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs5.put(new BasicAttribute("description", "Java marshalled object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs5);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // finally
}

From source file:CreateJavaSchema.java

/**
 * Inserts object class definitions from RFC 2713 into the schema.
 *
 * This method maps the LDAP schema definitions in RFC 2713 onto the
 * proprietary attributes required by the Active Directory schema.
 *
 * The resulting object class definitions differ from those of RFC 2713
 * in the following ways://from w w w  .ja  va2s . c o  m
 *
 *     - Abstract and auxiliary classes are now defined as structural.
 *     - The javaObject class now inherits from javaContainer.
 *     - The javaNamingReference, javaSerializedObject and
 *       javaMarshalledObject now inherit from javaObject.
 *
 * The effect of these differences is that Java objects cannot be
 * mixed-in with other directory entries, they may only be stored as
 * stand-alone entries.
 *
 * The reason for these differences is due to the way auxiliary classes
 * are supported the Active Directory. Only the names of structural
 * classes (not auxiliary) may appear in the object class attribute of
 * an entry. Therefore, the abstract and auxiliary classes in the Java
 * schema definition are re-defined as structural.
 */
protected void insertADObjectClasses(DirContext rootCtx, DirContext schemaCtx) throws NamingException {

    System.out.println("  [inserting new object class definitions ...]");

    String dn = schemaCtx.getNameInNamespace();
    String attrID;

    attrID = new String("javaContainer");
    Attributes attrs1 = new BasicAttributes();

    attrs1.put(new BasicAttribute("objectClass", "classSchema"));
    attrs1.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs1.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.1"));
    attrs1.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs1.put(new BasicAttribute("mustContain", "cn"));
    attrs1.put(new BasicAttribute("objectClassCategory", "1"));
    attrs1.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs1.put(new BasicAttribute("subclassOf", "top"));
    attrs1.put(new BasicAttribute("possSuperiors", "top")); //any superior
    attrs1.put(new BasicAttribute("description", "Container for a Java object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs1);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // because javaObject relys on javaContainer

    attrID = new String("javaObject");
    Attributes attrs2 = new BasicAttributes();

    attrs2.put(new BasicAttribute("objectClass", "classSchema"));
    attrs2.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs2.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.4"));
    attrs2.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs2.put(new BasicAttribute("mustContain", "javaClassName"));

    Attribute joMay = new BasicAttribute("mayContain");
    joMay.add("javaClassNames");
    joMay.add("javaCodeBase");
    joMay.add("javaDoc");
    joMay.add("description");
    attrs2.put(joMay);

    attrs2.put(new BasicAttribute("objectClassCategory", "1"));
    attrs2.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs2.put(new BasicAttribute("subclassOf", "javaContainer"));
    attrs2.put(new BasicAttribute("description", "Java object representation"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs2);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // because next 3 rely on javaObject

    attrID = new String("javaSerializedObject");
    Attributes attrs3 = new BasicAttributes();

    attrs3.put(new BasicAttribute("objectClass", "classSchema"));
    attrs3.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs3.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.5"));
    attrs3.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs3.put(new BasicAttribute("mustContain", "javaSerializedData"));
    attrs3.put(new BasicAttribute("objectClassCategory", "1"));
    attrs3.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs3.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs3.put(new BasicAttribute("description", "Java serialized object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs3);
    System.out.println("    [" + attrID + "]");

    attrID = new String("javaNamingReference");
    Attributes attrs4 = new BasicAttributes();

    attrs4.put(new BasicAttribute("objectClass", "classSchema"));
    attrs4.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs4.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.7"));
    attrs4.put(new BasicAttribute("lDAPDisplayName", attrID));

    Attribute jnrMay = new BasicAttribute("mayContain");
    jnrMay.add("javaReferenceAddress");
    jnrMay.add("javaFactory");
    attrs4.put(jnrMay);

    attrs4.put(new BasicAttribute("objectClassCategory", "1"));
    attrs4.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs4.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs4.put(new BasicAttribute("description", "JNDI reference"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs4);
    System.out.println("    [" + attrID + "]");

    attrID = new String("javaMarshalledObject");
    Attributes attrs5 = new BasicAttributes();

    attrs5.put(new BasicAttribute("objectClass", "classSchema"));
    attrs5.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs5.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.8"));
    attrs5.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs5.put(new BasicAttribute("mustContain", "javaSerializedData"));
    attrs5.put(new BasicAttribute("objectClassCategory", "1"));
    attrs5.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs5.put(new BasicAttribute("subclassOf", "javaObject"));
    attrs5.put(new BasicAttribute("description", "Java marshalled object"));

    schemaCtx.createSubcontext("CN=" + attrID, attrs5);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // finally
}