CORBA Example Hello
Prepared by Douglas Harris
doug@mscs.mu.edu
Hello.idl
// file Hello.idl
module HelloApp{
interface Hello{
string sayHello();
};
};
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class HelloClient
{
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("Hello", "");
NameComponent path[] = {nc};
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
// call the Hello server object and print results
String Hello = helloRef.sayHello();
System.out.println(Hello);
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
/*
* File: ./HelloApp/_HelloStub.java
* From: Hello.idl
* Date: Sat Jan 10 20:18:06 1998
* By: idltojava Java IDL 1.2 Nov 12 1997 12:23:47
*/
package HelloApp;
public class _HelloStub
extends org.omg.CORBA.portable.ObjectImpl
implements HelloApp.Hello {
public _HelloStub(org.omg.CORBA.portable.Delegate d) {
super();
_set_delegate(d);
}
private static final String _type_ids[] = {
"IDL:HelloApp/Hello:1.0"
};
public String[] _ids() { return (String[]) _type_ids.clone(); }
// IDL operations
// Implementation of ::HelloApp::Hello::sayHello
public String sayHello()
{
org.omg.CORBA.Request r = _request("sayHello");
r.set_return_type(org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
r.invoke();
String __result;
__result = r.return_value().extract_string();
return __result;
}
};
/*
* File: ./HelloApp/Hello.java
* From: Hello.idl
* Date: Sat Jan 10 20:18:06 1998
* By: idltojava Java IDL 1.2 Nov 12 1997 12:23:47
*/
package HelloApp;
public interface Hello
extends org.omg.CORBA.Object {
String sayHello()
;
}
/*
* File: ./HelloApp/_HelloImplBase.java
* From: Hello.idl
* Date: Sat Jan 10 20:18:06 1998
* By: idltojava Java IDL 1.2 Nov 12 1997 12:23:47
*/
package HelloApp;
public abstract class _HelloImplBase extends org.omg.CORBA.DynamicImplementation implements HelloApp.Hello {
// Constructor
public _HelloImplBase() {
super();
}
// Type strings for this class and its superclases
private static final String _type_ids[] = {
"IDL:HelloApp/Hello:1.0"
};
public String[] _ids() { return (String[]) _type_ids.clone(); }
private static java.util.Dictionary _methods = new java.util.Hashtable();
static {
_methods.put("sayHello", new java.lang.Integer(0));
}
// DSI Dispatch call
public void invoke(org.omg.CORBA.ServerRequest r) {
switch (((java.lang.Integer) _methods.get(r.op_name())).intValue()) {
case 0: // HelloApp.Hello.sayHello
{
org.omg.CORBA.NVList _list = _orb().create_list(0);
r.params(_list);
String ___result;
___result = this.sayHello();
org.omg.CORBA.Any __result = _orb().create_any();
__result.insert_string(___result);
r.result(__result);
}
break;
default:
throw new org.omg.CORBA.BAD_OPERATION(0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
}
}
}
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
class HelloServant extends _HelloImplBase
{
public String sayHello()
{
return "\nHello world !!\n";
}
}
public class HelloServer {
public static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create servant and register it with the ORB
HelloServant helloRef = new HelloServant();
orb.connect(helloRef);
// get the 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("Hello", "");
NameComponent path[] = {nc};
ncRef.rebind(path, helloRef);
// 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);
}
}
}