Write the First Corba
Program in J# |
Create a IDL script first |
Write the IDL Script: (server.idl) |
module demo
{
module hello
{
interface GoodDay
{
string hello_simple();
wstring hello_wide( in wstring msg );
};
};
};
|
Complete the implementation of the corba function calls |
Write the Operation Implementation (GoodDayImpl.java) |
package demo.hello;
import org.omg.CORBA.*;
public class GoodDayImpl
extends GoodDayPOA
{
private String location;
public GoodDayImpl( String location )
{
this.location = location;
}
public String hello_simple()
{
return "Hello World, from " + location;
}
public String hello_wide(String wide_msg)
{
System.out.println("The message is: " + wide_msg );
return "Hello W?rld, from ? 1 2 3 0 *&^%$#@!@";
}
}
|
Create a corba server |
Write the server program (Server.java) |
package demo.hello;
import java.io.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.CosNaming.*;
public class Server
{
public static void main(String[] args)
{
try
{
//init ORB
ORB orb = ORB.init( args, null );
//init POA
POA poa = POAHelper.narrow( orb.resolve_initial_references( "RootPOA" ));
poa.the_POAManager().activate();
// create a GoodDay object
GoodDayImpl goodDayImpl = new GoodDayImpl( "Somewhere" );
// create the object reference
org.omg.CORBA.Object obj = poa.servant_to_reference( goodDayImpl );
if ( args.length > 0 )
{
PrintWriter pw =
new PrintWriter( new FileWriter( args[ 0 ] ));
// print stringified object reference to file
pw.println( orb.object_to_string( obj ));
pw.flush();
pw.close();
}
else
{
// register server with naming context
NamingContextExt nc = NamingContextExtHelper.narrow(
orb.resolve_initial_references("NameService"));
nc.bind( nc.to_name("HelloServer.service"), obj);
}
// wait for requests
orb.run();
}
catch( Exception e )
{
System.out.println( e );
}
}
}
|
Create a corba client |
Write the client program: (Client.js) |
package demo.hello;
import java.io.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
public class Client
{
public static void main( String args[] )
{
try
{
// initialize the ORB.
ORB orb = ORB.init( args, null );
GoodDay goodDay;
if ( args.length > 0 )
{
File f = new File( args[ 0 ] );
// check if file exists
if ( ! f.exists() )
{
System.out.println("File " + args[0] + " does not exist.");
System.exit( -1 );
}
// check if args[0] points to a directory
if ( f.isDirectory() )
{
System.out.println("File " + args[0] + " is a directory.");
System.exit( -1 );
}
BufferedReader br = new BufferedReader( new FileReader( f ));
// get object reference from command-line argument file
org.omg.CORBA.Object obj = orb.string_to_object( br.readLine() );
br.close();
// and narrow it to HelloWorld.GoodDay
// if this fails, a BAD_PARAM will be thrown
goodDay = GoodDayHelper.narrow( obj );
}
else
{
// get hold of the naming service
NamingContextExt nc =
NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
goodDay = GoodDayHelper.narrow(nc.resolve(nc.to_name("HelloServer.service")));
}
// invoke the operation and print the result
System.out.println( goodDay.hello_simple() );
// invoke the operation again and print the wide string result
System.out.println( "wide string: " +
goodDay.hello_wide( "Hello W?rld, from ??1 2 3 0 *&^%$#@!@"));
}
catch( Exception ex )
{
System.err.println( ex );
}
}
}
|
How to test |
|
Suppose you are using Win32 platform and have installed .Net Framework v1.1 and J# SDK
already.
1. create a working directory
2. copy the <tomorb_home>\config\jsharp\tomorb_config.xml into the
working directory and edit the name service reference as
follow:
ORBInitRef.NameService=file://c:/NS_Ref
3. Put all the source codes into this directory
4. Make sure to copy the runtime libraries from the
distribution directory (dist\net-1.1\bin)
copy <tomorb home>\dist\net-1.1\js-bin\TomORB*.* .
or
fire the following command to register strongname libraries into GAC
<tomorb_home>\config\install-js-net-library.bat
5. Compile the Idl file (server.idl) type the following command
to compile the IDL script into VB.Net:
<tomorb_home>\dist\net-1.1\js-bin\idl2c.exe -lang JS -I . -d Generated server.idl
or
<tomorb_home>\dist\net-1.1\bin\idl2c.exe -lang JS -I . -d Generated server.idl
6. Compile the source files
vjc -target:exe -out:hello-server.exe Server.java GoodDayImpl.java /recurse:Generated\*.java
/r:TomORB_Based4js,TomORB4js,TomORB_CosNaming4js,System,System.Xml vjc
-target:exe -out:hello-client.exe Client.java GoodDayImpl.java
/recurse:Generated\*.java
/r:TomORB_Based4js,TomORB4js,TomORB_CosNaming4js,System,System.Xml
7. Start name service<tomorb_home>\dist\net-1.1\js-bin\namesvc
c:\NS_Ref run server program in one console with the following
command:
hello-server.exe
8. run client program in another console to test against the server program
Compile and test the programs by running the NAnt script
|