WebLogic Server 7.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. Build the example by executing the build script provided for this example.

    The build.xml script is stored in the SAMPLES_HOME\server\src\examples\jndi directory, where SAMPLES_HOME refers to the directory for all samples and examples for the WebLogic Platform.

    To build the example, move to the JNDI example directory and run the script using the following Java ant command:

    prompt> ant 

Run the Example

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

prompt> ant run_weblogiccontext 
Parameters contained within the run script are:
hostname
Host name of the WebLogic Server.
port
Port where the WebLogic Server is listening for connections.
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) 2002 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/docs70

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