Sei sulla pagina 1di 4

For this assignment, you will create and run an IDL interface using the provided example.

Applications Needed: Java JDK software and IDLTOJAVA compiler. The JDK has a built in
ORB and API which enables CORBA distributed object interaction. The IDLTOJAVA compiler
converts IDL-to-Java mapping in order to convert IDL interface definitions to Java methods,
classes, and interfaces that can be used to implement the server and client code.
To get the latest version of the IDL-to-Java compiler, download the latest version of
the Java™ Platform, Standard Edition (Java SE). When Java SE is installed, idlj will be
located in the bin directory.

Step 1: Create an IDL Interface


In this step you will map the IDL to Java in order to define the contract between the server and
client for your application. The code for this is language independent and will not look like your
traditional Java code. Create a new IDL file using your downloaded IDLTOJAVA compiler.
Name the file, “Liberty.idl” and enter the following code:
module LibertyApp
{
interface Liberty
{
String libertyU();
};
};
This Liberty U application will only have a single operation. This is all the code that you will
need for this step.

Step 2: Mapping Liberty.idl to Java


In this step you will create the required java files through the IDLTOJAVA tool. Open up your
command line prompt. Change the directory to the location of your Liberty.idl file. Enter the
compiler command:
idltojava Liberty.idl
You will see that a directory called LibertyApp has been created and it will contain 5 files. Open
up Liberty.java. I will contain these lines of code:
/* Liberty.java as generated by idltojava */
package LibertyApp;
public interface Liberty
extends org.omg.CORBA.Object {
String libertyU();
}

Step 3: Create LibertyClient.java


Copy and paste the code into LibertyClient.java
import LibertyApp.*; // The package containing your stubs.
import org.omg.CosNaming.*; // LibertyClient will use the naming service.
import org.omg.CORBA.*; // All CORBA applications need these classes.

public class LibertyClient

Page 1 of 4
{
public static void main(String args[])
{
try{

// create and initialize the ORB


ORB orb = ORB.init(args, null);

// get the root naming context


org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);

// resolve the object reference in naming


NameComponent nc = new NameComponent("Liberty", "");
NameComponent path[] = {nc};
Liberty libertyRef = LibertyHelper.narrow(ncRef.resolve(path));

// call the Liberty server object and print results


String liberty = libertyRef.libertyU();
System.out.println(liberty);

} catch(Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
}
}

Save and close LibertyClient.java

Step 4: Create a Liberty U Server


Create a file called LibertyServer.java
Copy and paste this code into your file:
// included are all packages imported in order to make this app work
import LibertyApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class LibertyServer


{
public static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);

Page 2 of 4
// create the servant and register it with the ORB
LibertyServant libertyRef = new LibertyServant();
orb.connect(libertyRef);

// get root naming context


org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);

// bind the object reference in naming


NameComponent nc = new NameComponent("Liberty", "");
NameComponent path[] = {nc};
ncRef.rebind(path, libertyRef);

// wait for invocations from clients


java.lang.Object sync = new java.lang.Object();
synchronized(sync){
sync.wait();
}
} catch(Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}
}

class LibertyServant extends _LibertyImplBase


{
public String libertyU()
{
return "\nLiberty U!!\n";
}
}

Step 5: Compile and run LibertyU Application


Compile both LibertyClient.java and LibertyServer.java. You should see the following classes
created once you do: LibertyClient.class, LibertyServer.class and LibertyServant.class.
Run the application from the command prompt. Start the Java IDL name server with the
following command:
tnameserv –ORBInitialPort nameserverport
Open up a second command prompt and start the Liberty server with the following command:
java LibertyServer –ORBInitialHost nameserverhost
-ORBInitialPort nameserverport
Open up a third command prompt and run the Liberty application client with the the following
command:

Page 3 of 4
java LibertyClient –ORBInitialHost nameserverhost
-ORBInitialPort nameserverport
The client will return the string from the server to the command line: Liberty U!!
Turn off tnameserv and LibertyServer processes after the client application returns successfully.

Deliverables: Take a screen shot of the “Liberty U!!” command prompt output and save it to the
java project folder. Compress all of the files within your java project and upload it to the final
project assignment link.

Page 4 of 4

Potrebbero piacerti anche