![]() ![]() ![]() ![]() |
Object Factories |
An object factory implements the ObjectFactoryinterface in the javax.naming.spi
package. This interface has one method: getObjectInstance
.
This method accepts as arguments information about the object (info), the name of the object (name) relative to the context (nameCtx) in which it is bound, and the environment properties to be used when creating an instance of the object.public Object getObjectInstance( Object info, Name name, Context nameCtx, Hashtable environment) throws Exception;For example, if the information stored in the directory is a Reference
, info would be an instance of Reference.
The name and nameCtx arguments are provided to the object factory in case the factory requires additional information from the directory in order to create the object. The env argument is usually the environment properties of the context that is using and invoking the object factory. See the Beyond the Basics
trail for details about environment properties.
Accessibility
In addition to implementing the ObjectFactory interface and providing an implementation for the getObjectInstance() method, the object factory class must be public and it must have a public constructor that accepts no arguments.Job Description
Typically, object factories are quite simple and small. Typically, their main role is to collect the information necessary to create an instance of the intended object('s class), and then to invoke that class's constructor. However, the complexity of the objects that they create can vary significantly. For example, the object factory examples given in this lesson are pretty trivial and the objects they create are also trivial. An LDAP object factory, on the other hand, creates an LDAP context, which creates and manages connections to an LDAP server. In this case, a relatively simple object factory is creating a very complex object.
In general, the information that object factories use to create objects comes from the directory. Consequently, there is a close relationship between the representation of the object as stored in the directory and what the object factories must do to create the object. For example, if the object is represented as a set of attributes in the directory, then the corresponding object factory must know to extract information from those attributes to create the object.
If All Else Fails
An object factory usually cannot expect that it can create an object for any arguments supplied to it. In fact, in many cases, as explained in the Providers section, the JNDI will ask an object factory to try to create an instance of an object that was intended for another object factory. Therefore, if an object factory finds that it cannot create an object based on the arguments supplied, it should return null. Only if the object factory knows for sure that it is supposed to create the object, but it can't, should it throw an exception. Otherwise, throwing an exception would preclude other object factories from being tried.
![]() ![]() ![]() ![]() |
Object Factories |