blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

LDAP Operation: Store Java objects themselves

We proceed the same way we did for naming services.

We will instantiate and store Staff.

import java.io.Serializable;
public class Staff implements Serializable {
String msg;
public void setMsg(String msg){
this.msg = msg;
}
public String toString() {
return msg;
}
}

Main code:

try {
//In the main() method of the program, create an initial directory context. 
//This is similar to creating an initial context in the previous naming example, 
//except that you use the constructor for InitialDirContext
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");

// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Create attributes to be associated with the new context
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("organizationalUnit");
//use codebase if necessary
//Attribute codebase = new BasicAttributes("javaCodebase", locationtoclassfile);
//attrs.put(codebase);
attrs.put(objclass);

Staff staff = new Staff();
staff.setMsg("hello Staffs");

// Perform bind
ctx.bind("ou=Staff", staff, attrs);

// Check that it is bound
Staff obj = (Staff)ctx.lookup("ou=Staff");

System.out.println(obj);
ctx.close();
}catch(NamingException e){
e.printStackTrace();
}


Note each entry in DIT always has one parent entry (except for the DIT root) and zero or more child entries (objects).
An entry must comprise one (and only one) STRUCTURAL objectClass but may contain any number of AUXILIARY objectClasses.
The data content of an entry consist of one or more attributes one (or more) of which is (are) used as the naming attribute
(more correctly the RDN) to uniquely identify this object in the DIT.

So in this case,
I choose organizationalUnit as my STRUCTURAL objectClass, top as ABSTRACT objectClass and no auxillary objectClass.

You could select any STRUCTURAL, AUXILARY or ABSTRACT objectClasses as long as it's defined in the LDAP schema.

Use the Schema browser to verify/select.


Note that ou and objectClass are MUST attributes.
We need not specify an objectClass for builtin objectClasses. But we do specify ou while binding.
ctx.bind("ou=Staff", staff, attrs);

Output:
hello Staffs


When a serialized object is bound in the directory as shown in the previous example, applications that read the serialized object from the directory must have access to the class definitions necessary to deserialize the object. Alternatively, you can record a codebase with the serialized object in the directory, either when you bind the object or subsequently by adding an attribute by using DirContext.modifyAttributes().

You can use any attribute to record this codebase and have your application read that attribute from the directory and use it appropriately. Or you can use the "javaCodebase" attribute specified in RFC 2713.

In the latter case, Sun's LDAP service provider will automatically use the attribute to load the class definitions as needed. "javaCodebase" should contain the URL of a codebase directory or a JAR file. (Note that JAR files work only with the Java 2 platform). If the codebase contains more than one URL, then each URL must be separated by a space character.

No comments:

Post a Comment