The JNDI Environment Properties

   

Before the client can use the services offered by JNDI, it must first locate the service on the network and get a connection. It's sort of a paradox if you think about it. A client uses JNDI to locate remote services, but the JNDI service is possibly a remote service itself. So how is a naming service located if you don't have a naming service to start with? The JNDI answer is by using environment properties.

Environment properties are the way that a JNDI client application communicates various preferences and properties that are used to establish a connection with the JNDI environment. Not only properties about where to find the JNDI naming service, but many more types of properties can be specified. For example, you might need to specify security credentials, such as a username and password, to connect to the naming service. A client can specify these by adding them to the JNDI environment properties. The properties are specific to a client. Each JNDI vendor implementation might have some common environment properties (such as where the JNDI service is), but also might contain specific properties (such as username and password of the client).

Each JNDI service provider can use the properties in different ways. In fact, each provider might have provider-specific properties that are used only by it. The specific environment properties that must be defined for a client to connect to the naming service are provider-dependent. For example, for BEA's WebLogic EJB server, you must tell the client application that you will be using the WebLogic-provided JNDI service and where it can be found on the network. With the file system service provider, you will also need to set these environment properties, but the format and type of properties are somewhat different.

Note

Check with your JNDI provider documentation to see which properties are important and which ones are not for that specific provider. You might be wondering how a client can be portable if the properties are different across JNDI providers. As you'll see later in this chapter, you can specify these properties outside your code in properties files. This enables the client application to switch the properties without negatively affecting the application source code.


Other information might be required depending on the SPI and naming service implementation. Typically, you will create a set of key/value pairs, where the keys are strings, which define a specific property. An example of one of these keys is

 java.naming.provider.url 

You must associate a value with this key. It might be something like this:

 ldap://ldap.wiz.com:389 

The entire environment property might look like this:

 java.naming.provider.url=ldap://ldap.wiz.com:389 

As you can see, the format is key = value .

Table 4.1 describes all the standard environment properties that are available to a JNDI application and what each environment property is used for.

Table 4.1. Standard Environment Properties That Can Be Used by JNDI

Property Value

Description

java.naming.applet

When using applets, this value is the java.applet.Applet instance that is being executed.

java.naming. authoritative

This is a normally a true or false value. If true, it bypasses any cache or replicas that are being used by JNDI. The default value is false.

java.naming.batchsize

Used to specify the batch size to use when returning data via the service's protocol. It must be a string representation of an integer. It is only a hint to the service provider and may be ignored.

java.naming.dns.url

Used to specify the DNS host and domain names to use for the JNDI URL context. Not used by all service providers.

java.naming.factory.initial

Used to specify which factory class will create the initial context objects. It must be the fully qualified class name .

java.naming.language

Specifies the preferred language to use. If not specified, the language will be determined by the provider.

java.naming.factory.object

A list of fully qualified class names that will be used as factories for creating JNDI objects. If a list is used, a colon is used to separate the items.

java.naming.provider.url

Used to specify configuration information for the provider to use. This should be a URL string such as ldap://ldap.wiz.com:38 .

java.naming.referral

Used to specify how referrals encountered by the provider should be handled. It can be "follow," "ignore," or "throw."

java.naming.security.authentication

Used to specify the security level to use. It can be "none," "simple," or "strong."

java.naming.security.credentials

Specifies the credentials (password) of the principal. Depends on the authentication scheme used. If the value is not specified, the behavior depends on the provider.

java.naming.security.principal

Used to specify the identity (username) of the principal for authenticating the caller to the service. Depends on the authentication scheme specified.

java.naming.security.protocol

Used to specify the security protocol to use. Its value depends on the service provider. An example is "SSL."

java.naming.factory.state

This property should be a colon-separated list of the fully qualified factory class names that will be used to get an object's state given the object itself.

java.naming.factory.url.pkgs

This property should be a colon-separated list of package prefixes for the class name of the factory class that will create a URL context factory.

Note

Notice that the property names specified in Table 4.1 have a prefix of java.naming and not javax.naming . You would think that because the APIs are defined in the javax.naming package, this would be the prefix, but it's not. Don't get the two confused . If you are including the JNDI packages in your applications, you will need to import javax.naming , but if you are using the environment property values, they begin with java.naming .


For each environment property in Table 4.1, the javax.naming.Context interface also defines a static String constant that can be used programmatically. So, if you needed to specify the java.naming.initial.context property in your code, you could instead use the constant

 Context.INITIAL_CONTEXT_FACTORY 

It's recommended that you use these constants in your source code. Of course, you can't use them in the resource files or system properties, but using them in your source code will help with readability.

Two environment properties are very important, no matter which service provider you are using. These two JNDI environment properties are

  • java.naming.factory.initial

  • java.naming.provider.url

A client sets these two JNDI properties so that the naming service can be located. Most of the other environment properties are optional, but these two are generally required. The next two sections cover each of these two properties in detail.

java.naming.factory.initial

This value is a string that holds the name specifying the initial context factory that should be used to create new InitialContext references.

Note

We have not covered what the Context or InitialContext objects are yet. For now, you can think of the Context as a reference to a binding within the naming service and the InitialContext as a reference to the root level of that naming service. If you imagine a tree hierarchy, the InitialContext reference is a reference to the top of the tree and a Context reference is a reference to a branch in the tree. The Context and InitialContext are covered in detail later in this chapter.


The value of this property should be the fully qualified class name of the factory class to use. Here's an example using a BEA WebLogic JNDI factory:

 java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory 

If you don't specify the initial context factory property or if you specify it incorrectly, you will get a NoInitialContextException or possibly a NamingException thrown when attempting to connect to the naming service.

graphics/01icon01.gif

If you are getting a NoInitialContextException while trying to create the InitialContext , see the " Troubleshooting " section at the end of the chapter.

java.naming.provider.url

This property value is also a string. This property holds the name of the environment value for specifying configuration information for the SPI to use. Although its syntax is somewhat dependent on the provider, the property typically follows the following format:

 protocol://host:port 

Here's an example using WebLogic:

 java.naming.provider.url=t3://localhost:7001 

The protocol t3 you see here is a BEA-specific protocol. A few other protocols that you might see are iiop and ldap . This URL informs the SPI where the JNDI naming service is located so that objects can be found during a lookup.



Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net