![]() ![]() ![]() ![]() |
Object Factories |
The attributes exampleillustrates how an instance of a DirContext class Drink is stored and subsequently looked up from the directory. When the Drink object's attributes are looked up from the directory, the Context.lookup()
method turned these attributes into an instance of Drink.
This was made possible because:Drink d2 = (Drink) ctx.lookup("cn=favDrink");DrinkFactory.getObjectInstance() first verifies that the object is intended for its factory. It does this by checking that the object is a DirContext and that it contains a "drinkType" attribute. If this verification fails, it returns null. Otherwise, it gets the value of the "drinkType" attribute (in this case "water") and uses it to create an instance of Drink.
- The service provider being used (Sun's LDAP provider) invoked DirectoryManager.getObjectInstance()
and supplied the method the data (a DirContext) that the provider read from the directory for the entry "cn=favDrink".
- The client program identified the object factory (DrinkFactory) to use when it created the initial context:
// So that object factory can be found env.put(Context.OBJECT_FACTORIES, "DrinkFactory");The definition of DrinkFactory.getObjectInstance() is as follows:
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env, Attributes inAttrs) throws Exception { if (obj instanceof DirContext) { try { Attribute dt; if (inAttrs != null && (dt=inAttrs.get("drinktype")) != null) { String drinkType = (String)dt.get(); return new Drink(drinkType); } } catch (NamingException e) { // debug System.err.println(e); e.printStackTrace(); } } // return null to indicate other factories should be tried return null; }
![]() ![]() ![]() ![]() |
Object Factories |