CORBA版HelloWorld
interface Hello{
string sayHello();
oneway void shutdown();
};
};
_HelloStub.java
HelloHolder.java
HelloHelper.java
Hello.java
HelloOperations.java
import HelloApp.HelloHelper;
import HelloApp.HelloPOA;
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;
class HelloImpl extends HelloPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
// implement sayHello() method
public String sayHello() {
return “\nHello world !!\n”;
}
// implement shutdown() method
public void shutdown() {
orb.shutdown(false);
}
}
public class HelloServer {
public static void main(String args[]) {
try {
//创建一个ORB实例
ORB orb = ORB.init(args, null);
//得到一个RootPOA的引用,并激活POAManager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references(“RootPOA”));
rootpoa.the_POAManager().activate();
// create servant and register it with the ORB
//创建一个HelloImpl实例(servant),并注册到ORB上
HelloImpl helloImpl = new HelloImpl();
helloImpl.setORB(orb);
//从服务中得到对象的引用
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
Hello href = HelloHelper.narrow(ref);
//得到一个根名称的上下文
org.omg.CORBA.Object objRef = orb.resolve_initial_references(“NameService”);
// Use NamingContextExt which is part of the Interoperable
// Naming Service (INS) specification.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
//在命名上下文中绑定这个对象
String name = “Hello”;
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, href);
System.out.println(“HelloServer ready and waiting …”);
//启动线程服务,等待客户端的调用
orb.run();
}catch (Exception e) {
System.err.println(“ERROR: “ + e);
e.printStackTrace(System.out);
}
System.out.println(“HelloServer Exiting …”);
}
}
import HelloApp.HelloHelper;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
public class HelloClient {
static Hello helloImpl;
public static void main(String args[]) {
try {
//创建一个ORB实例
ORB orb = ORB.init(args, null);
//获取根名称上下文
org.omg.CORBA.Object objRef = orb.resolve_initial_references(“NameService”);
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable naming Service.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
//从命名上下文中获取接口实现对象
String name = “Hello”;
helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));
//调用接口对象的方法
System.out.println(“Obtained a handle on server object: “ + helloImpl);
System.out.println(helloImpl.sayHello());
helloImpl.shutdown();
} catch (Exception e) {
System.out.println(“ERROR : “ + e);
e.printStackTrace(System.out);
}
}
HelloServer Exiting …
Hello world !!
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/240268