CORBA-Example
A simple CORBA implementation using Java
Echo.idl
module EchoApp{
interface Echo{
string echoString();
};
};
EchoServer.java
import EchoApp.EchoPOA;
public class EchoServer extends EchoPOA {
@Override
public String echoString() {
return "Hello World This is sital!!!!!!!";
}
}
Server.java
import EchoApp.Echo;
import EchoApp.EchoHelper;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
public class Server {
public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// create servant
EchoServer server = new EchoServer();
// get object reference from the servant
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(server);
Echo href = EchoHelper.narrow(ref);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
NameComponent path[] = ncRef.to_name( "ECHO-SERVER" );
ncRef.rebind(path, href);
System.out.println("Server ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("Exiting ...");
}
}
Client.java
import EchoApp.Echo;
import EchoApp.EchoHelper;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
public class Client {
public static void main(String args[]) {
try {
// create and initialize the ORB
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
Echo href = EchoHelper.narrow(ncRef.resolve_str("ECHO-SERVER"));
String hello = href.echoString();
System.out.println(hello);
} catch (InvalidName invalidName) {
invalidName.printStackTrace();
} catch (CannotProceed cannotProceed) {
cannotProceed.printStackTrace();
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName invalidName) {
invalidName.printStackTrace();
} catch (NotFound notFound) {
notFound.printStackTrace();
}
}
}
Instructions
1. Write the IDL file
The IDL file defines the interface that will be used by the client and server for communicating and passing objects.
When the IDL file gets compiled, it will produce a number of files, known as the stub and skeleton:
- The stub is used by the client to communicate with the server
- The skeleton is used by the server to communicate with the client
- The stub and skeleton communicate with the ORB server to facilitate the remote procedure call
The module in the IDL file will correspond to the package and directory in which the Java code will be generated
Echo.idl
module EchoApp {
interface Echo {
string echoString();
};
};
2. Generate the stub and skeleton code
There is an idlj program that comes with the JDK for generating the stub and skeleton code in Java IN COMMAND PROMPT "CMD" open 3 command
“D:\CORBA\src>"C:\Program
Files\Java\jdk1.8.0_261\bin\idlj" -fall Echo.idl”
OR
idlj –fall Echo.idl
The following files are generated by the idlj program:
- _EchoStub.java
- Echo.java
- EchoHelper.java
- EchoHolder.java
- EchoOperations.java
- EchoPOA.java
3. Write the server code
The server program will inherit from the EchoPOA class that was generated as part of the idlj program.
The EchoPOA class implements the EchoOperations interface
- This interface contains the methods we defined in the Echo.idl file, but standardized to Java.
We create an EchoServer.java class that extends the abstract EchoPoa class and then implement the methods contained in it
We create a main method in Server.java to communicate with the object request broker (ORB), registering the server with the ORB so clients are able to find it.
4. Write the client code
The client program Client.java needs to get a reference to the ORB then resolve the name of the server object it would like to invoke.
- This is ECHO-SERVER in this case
After getting an instance of a Server object from the server, it can invoke methods on it just like a method within its own JVM.
5. Compile the code
Compile the stub and skeleton from the directory that contains the IDL file.
Windows
javac EchoApp\*.java
Linux
javac EchoApp/*.java
Generate a JAR file from the compiled stub and skeleton.
Windows
jar cvf echoapp.jar EchoApp\*.class
Linux
jar cvf echoapp.jar EchoApp/*.class
Compile the server and client classes
Windows
javac -classpath .;echoapp.jar Server.java EchoServer.java Client.java
Linux
javac -classpath .:echoapp.jar Server.java EchoServer.java Client.java
6. Running the application
Start the ORB server
orbd -ORBInitialPort 1050 -ORBInitialHost localhost
Start the server application
java Server -ORBInitialPort 1050 -ORBInitialHost localhost
Start the client application
java Client -ORBInitialPort 1050 -ORBInitialHost localhost
If everything was compiled correctly the output should be:
Hello World!!!!!!!