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

Bind and lookup - serialized data - FS service provider

The most obvious approach to storing an object in a directory is to store the serialized representation of an object. The only requirement is that the object's class implement the Serializable interface.

When an object is serialized, its state becomes transformed into a stream of bytes. The service provider takes the stream of bytes and stores it in the directory. When a client looks up the object, the service provider reconstructs it from the stored data.

The following code demonstrates how to bind a Fruit to an entry in an JNDI service:

public class Fruit implements Serializable{
 String fruit;
 String taste;
 public Fruit(String f, String t) {
  fruit = f;
  taste = t;
 }
 public String toString() {
  return fruit+" tastes "+taste;
 }
}

Hashtable<String, String> env = new Hashtable<String, String>(2);
  env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  //this configuration is required for binding
  env.put(Context.PROVIDER_URL, "file:/C:/Users/eIPe/Desktop/temp");
  
  Context ctx = new InitialContext(env);

     Fruit fruit = new Fruit("orange","sweet");

     ctx.rebind("favorite", fruit);

     Fruit newFruit = (Fruit) ctx.lookup("favorite");
     System.out.println(newFruit);
    
     ctx.close();

When you run this you will get
javax.naming.OperationNotSupportedException: Can only bind References or Referenceable objects
This is because the file system service provider does not support binding objects themselves.

No comments:

Post a Comment