Add new object class in LDAP schema using JNDI
I wrote code to add object class in LDAP schema. It works fine with SunOne directory service. But it gives "InvalidAttributeValueException" in case of OpenLdap and gives "OperationNotSupportedException" in case of IBM TDS. Does anyone know a generalised code for these 3 directory services.
my code:
package demo;
import javax.naming.; import javax.naming.directory.; import java.util.Hashtable;
public class AddObjectClass {
public static void main(String args[])
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS,"cantsay");
Attributes attrs = new BasicAttributes(true); // ignore case
attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.3.1.1.45");
attrs.put("NAME", "myObjectClass");
attrs.put("DESC", "for JNDI example only");
attrs.put("SUP", "top");
attrs.put("STRUCTURAL", "true");
Attribute must = new BasicAttribute("MUST", "cn");
must.add("objectclass");
attrs.put(must);
try
{
DirContext ctx = new InitialDirContext(env);
DirContext schema = ctx.getSchema("");
schema.createSubcontext("ClassDefinition/开发者_如何学编程myObjectClass", attrs);
System.out.println("added");
ctx.close();
}catch(Exception e){e.printStackTrace();}
}
}
Here is the begining of an answer. On LDAP V3 RFCs it's documented that the schema is dynamic. It's what your are using in Sun one. I do quite the same thing in Active-Directory with LDIF files. But there is a trick, you add to load the new attributes, and to "reload the schema" usin a special attribute before you load the classes.
I just want to let you know that the dynamic feature is not always present on all LDAP V3 directories. And when it's present it's not so standard.
I 'am not able to talk abount IBM TDS. But the thing I'am sure is that even if OpenLDAP is able to support dynamic schema, most of the distribution I used where compiled with a text schema loaded during slapd start. These text files are in /etc/openldap/schema/. And you have to restart the deamon slapd to use a new schema.
For OpenLDAP perhaps have a look here
This article talks about dynamic schema on IBM TDS.
I hope it helps.
JP
精彩评论