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

LDAP Operation: Store information as attributes

The last technique to store non-serializable and non-referenceable objects involves storing attributes rather than the object or a reference to the object. If the bound object implements the DirContext interface, the LDAP ADD operation extracts and stores the object's attributes.

This technique doesn't store the actual object, but rather stores the attributes inside that object.
Consider this User class,
public class User {
 String employer;
 public User(String employer){
  this.employer = employer;
 }
}

To store a instance of User using this technique we make User a DirContext by implementing DirContext interface.
public class User implements DirContext {
 String employer;
 Attributes myAttrs;

 public User(String employer){
    this.employer = employer;
    myAttrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("organizationalUnit"); 
    objclass.add("top");
    objclass.add("extensibleObject");

   
    myAttrs.put(objclass);
        myAttrs.put("name", employer);
       
       
    }
 @Override
 public Attributes getAttributes(String name) throws NamingException {
  if (!name.equals("")) {
   throw new NameNotFoundException();
  }
  return (Attributes) myAttrs.clone();
 }
 @Override
 public Attributes getAttributes(Name name) throws NamingException {
  return getAttributes(name.toString());
 }
 @Override
 public Attributes getAttributes(String name, String[] ids)
   throws NamingException {
  if (!name.equals(""))
   throw new NameNotFoundException();
  Attributes answer = new BasicAttributes(true);
  Attribute target;
  for (int i = 0; i < ids.length; i++) {
   target = myAttrs.get(ids[i]);
   if (target != null) {
    answer.put(target);
   }
  }
  return answer;
 }
 @Override
 public Attributes getAttributes(Name name, String[] ids)
   throws NamingException {
  return getAttributes(name.toString(), ids);
 }
 // other methods
}
Note that we chose a AUXILARY object - "extensibleObject". ExtensibleObject gives you a whole range of attributes (almost all from LDAP schema) to create a custom Object of your taste. Note that I used a optional attribute "name" to store employer.
Now the factory implementation,
public class UserFactory implements DirObjectFactory {

 public UserFactory() {
 }

 @SuppressWarnings("rawtypes")
 public Object getObjectInstance(Object obj, Name name, Context ctx,
   Hashtable env, Attributes inAttrs) throws Exception {
  System.out.println("UserFactory.getObjectInstance");
  if (obj instanceof DirContext) {
   try {
    Attribute dt;
    if (inAttrs != null && (dt = inAttrs.get("name")) != null) {
     String employer = (String) dt.get();
     return new User(employer);
    }
   } catch (NamingException e) {
    // debug
    System.err.println(e);
    e.printStackTrace();
   }
  }

  // return null to indicate other factories should be tried
  return null;
 }

 public Object getObjectInstance(Object obj, Name name, Context ctx,
   Hashtable env) throws Exception {
  return getObjectInstance(obj, name, ctx, env, null);
 }
}
And now the test class,
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");
 env.put(Context.OBJECT_FACTORIES, "sample.UserFactory");
 // Create the initial context
 DirContext ctx = new InitialDirContext(env);
 
 User usr = new User("Goro");
 ctx.rebind("ou=Kill Bill, ou=employees", null, usr.getAttributes(""));
 //Check that it is bound
 User obj = (User)ctx.lookup("ou=Kill Bill, ou=employees");

 System.out.println(obj);
 ctx.close();
}catch(NamingException e){
 e.printStackTrace();
}
Output:
UserFactory.getObjectInstance sample.User@4bd66d2femployer: Goro

No comments:

Post a Comment