WebLogic Server 6.0 Code Examples, BEA Systems, Inc.

examples.jndi
Class WebLogicContextExample

java.lang.Object
  |
  +--examples.jndi.WebLogicContextExample

public class WebLogicContextExample
extends java.lang.Object

This example illustrates how to obtain and use a WebLogic naming context. The same code will work on both a client and a server. If it is run on a client, it will delegate to a remote context running on the server specified by java.naming.provider.url. If it is run on the server specified by the java.naming.provider.url, a local context is used. If it is run on some other server, the context will again delegate to a context located on the server specified by java.naming.provider.url.

The example first creates a default naming context, then binds an object into that context under a newly created subcontext, and finally looks up the object. Note that the binding created here will be available for the life of the server to any client that obtains a WebLogic InitialContext through the same server.

The following sections cover what to do:

  1. Build the Example
  2. Run the Example

Build the Example

  1. Set up your development shell, as described in Setting up your environment.

  2. Compile the example by executing the following command or by executing the build script provided for this example in the samples/examples/jndi directory. The script will perform the following step:

    1. Compile the WebLogicContextExample as shown in this example for Windows NT/2000:

      $ javac -d %CLIENT_CLASSES% WebLogicContextExample.java

Run the Example

In your development shell, run the client with the following command:

$ java examples.jndi.WebLogicContextExample t3://hostname:port
where:
hostname
Host name of the WebLogic Server.
port
Port where the WebLogic Server is listening for connections (weblogic.system.ListenPort).
If no argument is provided, the URL defaults to "t3://localhost:7001".

Try running this example a second time within the same WebLogic Server session. Note that although the JNDI binding has already been created (the first time you ran this example), no exception in thrown when the subcontext is recreated. WebLogic's context implementation does not throw an exception if the object that is bound to an existing name is identical to the current object that is bound to that name. This example generates a unique string (using the date) for the bound object. When binding this object for the second time, an NameAlreadyBoundException is thrown since the object is not identical to the object that is currently bound to the same name. Now, the rebind() method must be used to successfully bind the new object.

The key to creating an InitialContext is correctly specifying the required properties. The java.naming.factory.initial property (referred to here via the static InitialContext.INITIAL_CONTEXT_FACTORY) specifies the factory that is used to create the context. Other properties (like java.naming.provider.url) are used to pass on parameters to the specified factory.

These properties can either be provided as system properties or passed explicitly to the InitialContext constructor. This example creates and passes the properties explicitly for clarity. Note that passing the properties explicitly is the only alternative when working in an applet, since an applet is not allowed to modify or read all system properties.

Author:
Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.

Constructor Summary
WebLogicContextExample()
           
 
Method Summary
static void main(java.lang.String[] args)
          Runs this example from the command line.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebLogicContextExample

public WebLogicContextExample()
Method Detail

main

public static void main(java.lang.String[] args)
Runs this example from the command line.

Documentation is available at
http://e-docs.bea.com/wls/docs60

Copyright © 2000 BEA Systems, Inc. All Rights Reserved.