Getting the Environment for a

   

Getting the Environment for a Context Object

The method getEnvironment in the Context interface will return a Hashtable with all the environment properties that are in effect for the Context . To modify the environment properties for a Context , you must use the addToEnvironment and removeFromEnvironment methods .

Let's look at an example of getting the environment for a Context . For this example, we are going to create a jndi.properties resource file and let the InitialContext discover the environment properties through this method, instead of specifying them in a Hashtable as Listing 4.2 did. Listing 4.4 shows the jndi.properties file that we will be using.

Listing 4.4 The jndi.properties File for Listing 4.5
 java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory  java.naming.provider.url=file:///c:/jndi_root/ 

The jndi.properties can be anywhere in your system classpath if you are using Java 2. Refer to the "Setting the JNDI Environment Properties" section earlier in this chapter for more help on where to put this file based on your version of the SDK.

Listing 4.5 shows an example of how to get the environment properties programmatically and print them out.

Listing 4.5 An Example Showing How to Get All the Environment Properties for a Context
 import javax.naming.*;  import java.util.Hashtable;  import java.util.Iterator;  import java.util.Properties;  import java.util.Set;  public class ListJNDIEnvironment {   // Default Constructor    public ListJNDIEnvironment(){     super();    }    public void listEnvironmentProperties(){     Context initCtx = null;      try {       // Create the InitialContext. The jndi.properties resource file        // will be used for the environment properties        initCtx = new InitialContext();        // List all of the environment properties for this Context        Hashtable env = initCtx.getEnvironment();        Set keys = env.keySet();        Iterator iter = keys.iterator();        while( iter.hasNext() ) {         String key = (String)iter.next();          String value = (String)env.get( key );          System.out.println( key + "=" + value );        }      }catch( NoInitialContextException ex ){       System.out.println( "You did not specify an InitialContext Factory" );        System.out.println( "Check the jndi.properties resource file" );      }catch( NamingException ex ){       ex.printStackTrace();      }finally{       // Close up the open resources        closeInitialContext( initCtx );      }    }    // Always close your InitialContext when you are done with it    public void closeInitialContext( Context ctx ){     try {       if ( ctx != null )          System.out.println( "Closing the InitialContext" );          ctx.close();      }catch( NamingException ex ) {       ex.printStackTrace();      }    }    public static void main( String[] args ){     ListJNDIEnvironment client = new ListJNDIEnvironment();      // List the environment properties      client.listEnvironmentProperties();    }  } 

The output should look similar to this:

 C:\ejb20book\ejb20book\classes>java ListJNDIEnvironment  java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory  java.naming.provider.url=file:///c:/jndi_root/  Closing the InitialContext  C:\ejb20book\ejb20book\classes> 

The output might be different if you are using a different service provider or URL to run the example.

graphics/01icon01.gif

If you are having trouble getting the ListJNDIEnvironment program to find your jndi.properties resource file, check the " Troubleshooting " section at the end of this chapter.



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