177 Chapter 9: Accessing Directory Services with JNDI Cross−Reference You used JNDI as a naming service in Chapter 7 to fetch implementations of the...

Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.

In this first example you will use the RMI registry naming service. This example will enable you to perform the same functions using JNDI that you would otherwise have found in the java.rmi.registry package and java.rmi.naming class.
Cross−Reference
The RMI example in this chapter uses the classes and server defined in Chapter 15.
If you are not already familiar with RMI, you may wish to read that chapter.
Using system properties
JNDI uses quite a collection of system properties to define its behavior. Although Table 9−1 only lists the most important ones, many more are in use — particularly for directory−services implementations in which you need passwords and user names for security reasons (we cover these implementations later in the chapter, in Table 9−2).
Table 9−1: The list of standard JNDI properties used to control context handling Property Name
Description
java.naming.factory.initial
The name of the class that is the factory for providing the
InitialContext implementation from the service provider.
java.naming.provider.url
The initial URL used for configuration of the initial
context. Dependent on the service provider in use.
java.naming.factory.object
The name of the factory or factories to use for creating
objects used in the name−to−object mapping. Works for
both NameClassPair and References.
java.naming.factory.state
The name of the factory or factories to use for creating
JNDI state objects.
For convenience, a number of these properties exist as constants defined in the Context interface. For example, to set the initial factory within the code (not on the command line) you can use the following statement to set the service provider to be the Sun's file−system implementation: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.FSContextFactory");
Defining the service provider
The first step in connecting to a service is to nominate the service provider you want to use. Table 9−1 lists all the system properties you can use to define the behavior of the JNDI initial lookup. If none of these properties are defined, JNDI will look up the internal property file called jndi.properties for the default values, where you can also place default values.
But how do you know where and when to create service providers? Well, for everyone but those rare few who may implement a service provider, the service provider comes in a neat pre−packaged form from some other company or service. This service provider should provide documentation about what class names are to be used for the various factories used to create the initial context. The normal installation process will place the JAR files in the JRE extensions directory, along with all your other extensions, such as JDBC drivers and so on.
178
Chapter 9: Accessing Directory Services with JNDI Tip By default, the JNDI that comes with the J2SE v1.4 release includes service providers for DNS, CORBA, RMIRegistry, and LDAP. Version 1.3 does not include the DNS service provider. For J2EE, you need to check which service providers come with your J2EE environment, because each vendor will be different.
You can define system properties in the usual way — through System.setProperty(), on the command line using the –D option, or in the jndi.properties file. Whichever way you do it, you must make sure that you at least define the initial factory (java.naming.factory.initial) and the service provider's URL. Say you're using Sun's RMI service provider: The class you need is com.sun.jndi.rmi.registery.RegistryContextFactory.
Looking up the object
Now that the system properties are set, the next step is to create the initial context. For this you just need to create an instance of InitialContext with the following code:
InitialContext ctx = new InitialContext();
Powered by wordpress | Theme: simpletex | © Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.