![]() ![]() ![]() ![]() |
Object Factories |
The reference exampleillustrates how an instance of a Referenceable class Fruit is stored and subsequently looked up from the directory. When the reference is looked up from the directory, the Context.lookup()
method turned the data read from the directory into an instance of Fruit.
This is because the service provider being used (the LDAP provider) invoked NamingManager.getObjectInstance()Fruit f2 = (Fruit) ctx.lookup("cn=favorite");on the data (a reference) the provider read from the directory for the entry "cn=favorite". The reference identified FruitFactory as the object factory's class name.
FruitFactory.getObjectInstance() is very simple. It first verifies that it can do something with the data (that is, that the data is a Reference containing an address of type "fruit", and that the reference is for objects of class Fruit). If this verification fails, the factory returns null so that other factories, if any, can be attempted. If it succeeds, the contents of the address (in this case "orange") are used to create a new instance of Fruit, which is then returned.
The NamingManager, having gotten a non-null answer from an object factory, will return this value to lookup(). The definition of FruitFactory.getObjectInstance() is as follows.
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env) throws Exception { if (obj instanceof Reference) { Reference ref = (Reference)obj; if (ref.getClassName().equals(Fruit.class.getClassName())) { RefAddr addr = ref.get("fruit"); if (addr != null) { return new Fruit((String)addr.getContent()); } } } return null; }
![]() ![]() ![]() ![]() |
Object Factories |