Day 9

Quiz

A1:

To register a server object with the naming service, use the bind() method of the Context object.

A2:

Use the lookup() method of the Context object to look up the server object registered in the naming service. Cast the object to its appropriate type to use it in the application.

A3:

To reregister a server object in the naming service, you need to use the rebind() method of the Context object. The rebind() method overwrites the current object reference in the naming service with the new object reference.

Exercises

A1:

After you have created your context object in Listing 9.5, add the following code, which gets the namespace of the Context and then enumerates the contents:

 String ctxName = ctx.getNameInNamespace();  NamingEnumeration myNamingEnum = ctx.list(ctxName); while(myNamingEnum.hasMoreElements()){    System.out.println("Naming Enum : "+myNamingEnum.nextElement()); } 

A2:

The following code is a solution for this exercise. You first need to obtain the InitialContext object. Then create a server object of any type. Here you have created an object of type String. Then create a subcontext using the method createSubcontext() of the Context class. Get its namespace, enumerate the contents, and then destroy the subcontext using the destroySubcontext() method of the Context class:

 import java.rmi.RemoteException;  import java.util.Properties; import javax.naming.*; import java.util.*; import java.io.*; import java.text.*; import javax.rmi.PortableRemoteObject; class CreatingSubcontext {   public static void main(String[] args){     try{        //Creating the context        Hashtable h = new Hashtable();        h.put(Context.INITIAL_CONTEXT_FACTORY,                "weblogic.jndi. WLInitialContextFactory");        h.put(Context.PROVIDER_URL, "t3://localhost:7001");        Context ctx = new InitialContext(h);        System.out.println("Initial context to look up created "+ctx);        //create a server object to bind        String myString = "My string to bind";        System.out.println(myString);        Context myNewSubcontext = ctx.createSubcontext(myString);        System.out.println("Sub context created "+myNewSubcontext);        String newSubctxName = myNewSubcontext.getNameInNamespace();        System.out.println("newSubctxName "+myNewSubcontext);        NamingEnumeration myNamingEnum = ctx.list(newSubctxName);        while(myNamingEnum.hasMoreElements()){          System.out.println("Naming Enum : "+myNamingEnum.nextElement());        };        ctx.destroySubcontext(newSubctxName);        //can even do a ctx.destroySubcontext(myString);        System.out.println("Deleted sub context");     }     catch(Exception e){       System.out.println("Error in the subcontext application");       e.printStackTrace();     }   } } 

A3:

The following code listing shows the solution. You first create a DirContext object using InitialDirContext. Then, get the name space of the DirContext object using the fscontext context factory and the URL of your local directory. You can then list the contents of this directory by getting an enumeration of the directory contents:

 import java.rmi.RemoteException;  import java.util.Properties; import javax.naming.*; import javax.naming.directory.*; import java.util.*; import java.io.*; import java.text.*; import javax.rmi.PortableRemoteObject; class CreatingFSContext {   public static void main(String[] args){     try{        //Creating the context        Hashtable h = new Hashtable();        h.put(Context.INITIAL_CONTEXT_FACTORY,                "com.sun.jndi.fscontext.RefFSContextFactory");        h.put(Context.PROVIDER_URL,"file:/c:/book");        DirContext dirCtx = new InitialDirContext(h);        System.out.println("Initial context to look up created "+dirCtx);        //create a new file object        String newDirSubctxName = dirCtx.getNameInNamespace();        System.out.println(newDirSubctxName);        NamingEnumeration myNamingEnum = dirCtx.list(newDirSubctxName);        while(myNamingEnum.hasMoreElements()){          System.out.println("Naming Enum : "+myNamingEnum.nextElement());        };     }     catch(Exception e){        System.out.println("Error in the subcontext application");        e.printStackTrace();     }   } } 



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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