DEVELOPING WITH THE NAMING SERVICE

Một phần của tài liệu sams java distributed objects (Trang 537 - 540)

At this point in our discussion of the Naming Service, you should have a solid

understanding of its purpose and feature set. Additionally, the prior examination of the IDL should give you a basic understanding of how to interact with the service. In this section, we first develop a small application that performs name binding and resolution.

After this initial development effort, we look at how the Naming Service can be used in the real world by writing a wine cellar management application.

Because binding and name resolution are probably the most common tasks performed using the Naming Service, we’ll begin our development effort there. Listing 30.2 contains the code for a class called NameServiceDemo . The class binds to the Naming Service, creates a name graph, binds an object to that graph, and then resolves the object using the initial name graph. Let’s stop for a minute and study the code. We’ll then go through each step in the process.

LISTING 30.2 PERFORMING BIND AND NAME RESOLUTION OPERATIONS USING THE NAMING SERVICE

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

import com.visigenic.vbroker.services.CosNaming.*;

public class NameServiceDemo {

private NameComponent[] _nameGraph = null;

private NamingContext _root = null;

private BOA _boa = null;

public NameServiceDemo() { ORB orb = ORB.init();

_boa = orb.BOA_init();

try{ _root = NamingContextHelper.bind(orb); } catch( Exception e ) { }

createNameGraph();

bindObject();

resolveObject();

} /**

* The createNameGraph() method creates a new * name graph that is then used in binding * and resolution.

*/

private final void createNameGraph() { _nameGraph = new NameComponent[3];

_nameGraph[0]

= new NameComponent("great-grandparent category",

"string");

_nameGraph[1]

= new NameComponent("grandparent category",

"string");

_nameGraph[2]

= new NameComponent("parent category", "string");

} /**

* The bindObject() method creates a new * NameServiceObject object and binds it

* to the name graph created by the createNameGraph() * method.

*/

private final void bindObject() { NamingContext root = _root;

NameServiceObjectI object = new NameServiceObject();

object.sData("some information");

_boa.obj_is_ready(object);

int iLength = _nameGraph.length-1;

// iterate through the name graph, binding // each context on its own

NameComponent[] componentHolder = new NameComponent[1];

for(int i=0; i<iLength; i++) {

componentHolder[0] == _nameGraph[i];

try{ root =

root.bind_new_context(componentHolder); } catch( Exception e ) { } } // for

// bind the target object to the last // item in the name graph

componentHolder[0] == _nameGraph[iLength];

try{ root.bind(componentHolder, object); } catch( Exception e ) {

System.out.println("exception at object bind:

"+e);

}

System.out.println("bound object, data is:

"+object.sData());

} /**

* The resolveObject() method using the name * graph created by the createNameGraph() method * to discover the bound NameServiceObject object.

*/

private final void resolveObject() {

try{ // resolve using the last entry in the name graph NameComponent[] componentHolder == new

NameComponent[1];

componentHolder[0] == _nameGraph[2];

org.omg.CORBA.Object resolvedObject = _root.resolve(_nameGraph);

// narrow the result into a NameServiceObject object

NameServiceObjectI object =

NameServiceObjectIHelper.narrow(resolvedObject);

System.out.println("resolved object, data is: "+

object.sData());

}

catch( Exception e ) {

System.out.println("exception at object resolve:

"+e);

} }

public static void main(String[] args)) {

NameServiceDemo demo = new NameServiceDemo();

} }

The first task undertaken by the NameServiceDemo class is the creation of the name graph itself. This step, represented in the createNameGraph() method, simply creates an array of NameComponent objects. The first item in the array is the top-most parent, and the last item in the array is the bottommost parent.

After the creation of the name graph itself, the next task undertaken is to bind an object to the graph previously created. In this example, the bound object is an instance of the NameServiceObjectI class. This class, described using IDL in Listing 30.3, is a simple class that has a single string attribute.

LISTING 30.3 THE NameServiceObjectI INTERFACE

interface NameServiceObjectI { attribute string sData;

};

The process of binding the object to the name graph takes place in the bindObject() method and is accomplished in two steps. First, we iterate through the name graph, calling the bind_new_context() method to individually bind each NameComponent object to a new context. We then call the bind() method to bind the last

NameComponent object to the NameServiceObjectI object itself.

Once we’ve bound the target object to the name graph, that same name graph can be used to resolve the object. The last method invoked in this example, resolveObject() , uses the resolve() method to resolve the previously bound object.

Now that you fully understand the introduction to the Naming Service, you’re ready to run the sample application. If you’ve not already installed the Inprise ORB and Naming Service component (an option during the install), do so now. This software is included on the enclosed CD-ROM. With the proper software installed, first compile the IDL by entering

idl2java NameServiceObjectI.idl

and then compile the Java code using your Java compiler. Now start the ORB using the following command:

osagent

Start the Naming Service using this command:

vbj -DORBservices=CosNaming

com.visigenic.vbroker.services.CosNaming.ExtFactory ExampleServiceName example_log

The command to start the Naming Service takes two parameters. The first, ExampleServiceName , indicates the unique name associated with this Naming Service instance; the second, example_log, indicates the name of the file to be used as a log.

In this example, the same class performs all binding and resolving in sequence right after each other, but this does not have to be the case. The bindObject() and

resolveObject() methods could be pulled out of the NameServiceDemo class and placed individually in their own classes. To fully test your understanding of the name service, you might want to actually separate the methods yourself and then run them on different machines.

Một phần của tài liệu sams java distributed objects (Trang 537 - 540)

Tải bản đầy đủ (PDF)

(693 trang)