Central to a CORBA system is the Object Request Broker, which implements the request to remote objects. Language neutrality is achieved through the use of an interface definition language (IDL) - to define the methods an object provides to the ORB. Language mappings are used to convert the IDL file into language specific class files. CORBA offers many sophisticated features.
Setting up a CORBA System in Java SDK 1.4
1. Write the IDL for the objects.
2. Use IDL compile tool to generate stubs and skeletons.
3. Write the class that implements the interfaces of the IDL file.
4. Write a server to connect objects to the ORB; then leave the server running.
5. Write the client code to access the remote CORBA object via the ORB using a name server to find it.
6. Invoke the methods using the remote object's stub.
The IDL file for HelloWorld – A Java implementation
module itc594{
struct Person{
string firstName;
string lastName;
short age;
string address;
};
struct Time{
string time;
};
struct Name{
string name;
};
typedef sequence< Person > PersonSeq;
interface HelloWorld{
string sayHello(in string inName);
Time whatsTheTime(in string country);
Person whoAreYou();
PersonSeq whoIsThere();
void addPerson(in Person aPerson);
};
};
IDL compilers are for certain languages. For example: Java SDK 1.4 comes with idlj.bat which creates all the CORBA support files necessary. The IDL types are mapped to their Java equivalents. For example: a struct type maps to a java data structure object. A sequence maps to an array of structs.
Creating the Object Implementation
Two approaches are suggested, either:
1. Inheritance by extend the POA class; or
2. Delegation using a TIE mechanism. This is used when we want the object to extend something else to save our hierarchy tree.
Creating the CORBA server – Connecting objects to the ORB
The steps taken in getting HelloWorldServer up and running are:
Step 1) Compile the IDL file.
C:\idlj -falltie HelloWorld.idl
Step 2) Implement HelloWorldOperations.Java and add concrete methods.
Step 3) Create a server class with a main method:
Make an ORB.
Create an object.
Obtain the root POA.
– Use the HelloWorldPOATie class to delegate to the object implementation.
Add it to the name service.
Keep the ORB running.
The Object Implementation code
A sample of the method implementation for the HelloWorldImpl class. Note the use of the Person struct from the IDL. Person becomes a basic object type after IDL compilation.
public Person whoAreYou(){
return (Person)people.get(0);
}
public Person[] whoIsThere(){
Person[] peopleArray = new Person[people.size()];
for(int index =0; index<people.size(); index++){
peopleArray[index] = (Person) people.get(index);
}
return peopleArray;
}
public void addPerson(Person aPerson){
people.add(aPerson);
}
The server code
try{
// create the ORB!
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
HelloWorldImpl helloWorldImpl = new HelloWorldImpl("Barry White");
POA rootPOA = POAHelper.
narrow(orb.resolve_initial_references("RootPOA"));
rootPOA.the_POAManager().activate();
HelloWorldPOATie tie = new HelloWorldPOATie(helloWorldImpl,
rootPOA);
HelloWorld helloWorldRef = tie._this(orb);
// create a nameSpace for the HelloWorld Object
NamingContext root_context = NamingContextHelper.narrow(
orb.resolve_initial_references("NameService"));
NameComponent[] helloName1 =
{ new NameComponent( "helloWorld", "" ) };
root_context.rebind( helloName1, helloWorldRef );
Thread.currentThread().join();
}
catch (Exception e) {
e.printStackTrace();
}
The Client Code
try{
ORB orb = ORB.init(args, null);
// Resolve the NameService to find the object
NamingContext nameService = NamingContextHelper
.narrow(orb.resolve_initial_references("NameService"));
// resolve the Object Reference in Naming
NameComponent[] helloName = { new NameComponent( "helloWorld", "" ) };
HelloWorld helloWorldImpl = HelloWorldHelper.
narrow(nameService.resolve(helloName));
//call a method on the Stub
System.out.println(“Say Hello Object: " + helloWorldImpl.sayHello());
}
Catch(Exception e){
e.printStackTrace();
}
Now to get it all running…and a well-earned rest!
1. Compile all classes!
2. Start the Java ORB bootstrap name service
c:\start tnameserv -ORBInitialHost localhost -ORBInitialPort 900
3. Start the server and tell it where the ORB is running
C:\start java HelloWorldServer -ORBInitialHost localhost
-ORBInitialPort 900
-ORBInitialPort 900
4. Start the Client and tell it where the ORB is running also
C:\java HelloWorldClient -ORBInitialHost localhost
-ORBInitialPort 900
-ORBInitialPort 900
0 comments:
Post a Comment