第五章 组件化开发方法 一.题目分析 本次题目基于CORBA模型以及ORB机制进行CORBA组件编程,在CS模式下进行开发。 二.CORBA模型分析
三.组件实现 题目1.Java版CORBA程序1——HelloWorld 实现详解: 1 编写IDL接口HelloWorld.idl: module sample{ interface HelloWorld{ wstring sayHello(); }; }; 2编译IDL接口: 编译结果生成sample包,生成下述文件 _HelloWorldStub.java HelloWorld.java HelloWorldHelper.java HelloWorldHolder.java HelloWorldOperations.java HelloWorldPOA.java 3编译服务端程序:HelloWorldServer.java import sample.; import org.omg.CosNaming.; import org.omg.CosNaming.NamingContextPackage.; import org.omg.CORBA.; import org.omg.CORBA.portable.; import org.omg.PortableServer.; class HelloWorldServant extends HelloWorldPOA{ //对象实现类 public String sayHello(){ return “\nHello World!+软件工程1702班+黄加诚\n”; } } public class HelloWorldServer{ //服务程序 public static void main(String args[]){ try{ //初始化ORB ORB orb = ORB.init(args, null); //取根POA的引用 org.omg.CORBA.Object poaobj = orb.resolve_initial_references (“RootPOA”); org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj); org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager(); //创建伺服对象 HelloWorldServant objRef = new HelloWorldServant(); HelloWorld obj = objRef._this(orb); //绑定命名服务 NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references(“NameService”)); NameComponent nc = new NameComponent(“Hello”, “”); NameComponent path[] = {nc}; ncRef.rebind(path, obj); //激活POA管理器 manager.activate(); //等待处理客户程序的请求 System.out.println(“HelloWorld is running!”); orb.run(); }catch (Exception e) { System.err.println(“ERROR: " + e); e.printStackTrace(System.out); } } } 4编译客户端程序: HelloWorldClient.java import sample.; import org.omg.CosNaming.; import org.omg.CORBA.; public class HelloWorldClient { public static void main(String args[]) { try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object objRef = orb.resolve_initial_references(“NameService”); NamingContext ncRef = NamingContextHelper.narrow(objRef); NameComponent nc = new NameComponent(“Hello”,""); NameComponent path[] = {nc}; HelloWorld helloWorld = HelloWorldHelper.narrow(ncRef.resolve(path)); String hello = helloWorld.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } } 题目2.JAVA版CORBA程序2——Counter 实现详解: 1 IDL接口counter.idl: module CounterApp{ interface Counter{ readonly attribute long value; void inc(); void dec(); }; }; 2编译IDL接口: 编译结果生成CounterApp包,生成下述文件 _CounterStub.java Counter.java CounterHelper.java CounterHolder.java CounterOperations.java CounterPOA.java 3 编译对象实现代码:CounterImpl.java import CounterApp.; public class CounterImpl extends CounterPOA { private int count; public CounterImpl(){ count = 0; } public void inc(){ count++; } public void dec(){ count - -; } public int value(){ return count; } } 4编译服务端程序: Server.java import CounterApp.; import java.io.; import org.omg.CosNaming.; import org.omg.CosNaming.NamingContextPackage.; import org.omg.CORBA.; import org.omg.CORBA.portable.; import org.omg.PortableServer.; public class Server { public static void main(String[] args){ try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object poaobj = orb.resolve_initial_references (“RootPOA”); org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj); org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager(); CounterImpl c_impl = new CounterImpl(); Counter c = c_impl._this(orb); NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references(“NameService”)); NameComponent nc = new NameComponent(“Count”, “”); NameComponent path[] = {nc}; ncRef.rebind(path, c); FileOutputStream file = new FileOutputStream(“Counter.ref”); PrintWriter writer = new PrintWriter(file); String ref = orb.object_to_string©; writer.println(ref); writer.flush(); file.close(); System.out.println(“Server started.”+" Stop:Ctrl-c"); rootPOA.the_POAManager().activate(); orb.run(); }catch(IOException ex){ System.out.println(“File error:”+ex.getMessage()); System.exit(2); }catch(Exception ex){ System.out.println("Exception: "+ex.getMessage()); System.exit(1); } } } 5编译客户端程序: Client.java import CounterApp.; import java.util.; import java.io.; import org.omg.CORBA.; import org.omg.CosNaming.; public class Client { public static void main(String[] args){ try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object obj = orb.resolve_initial_references(“NameService”); NamingContext ncRef = NamingContextHelper.narrow(obj); NameComponent nc = new NameComponent(“Count”,”"); NameComponent path[] = {nc}; String ref = null; try{ Scanner reader = new Scanner(new File(“Counter.ref”)); ref = reader.nextLine(); }catch(IOException ex){ System.out.println("File error: "+ex.getMessage()); System.exit(2); } obj = orb.string_to_object(ref); if(obj == null){ System.out.println(“Invalid IOR”); System.exit(4); } Counter c = null; try{ c = CounterHelper.narrow(obj); }catch(BAD_PARAM ex){ System.out.println(“Narrowing failed”); System.exit(3); } int inp = -1; do{ System.out.print(“Counter value: “+c.value()+”\nAction(+/-/e)?”); System.out.flush(); do{ try{ inp = System.in.read(); }catch(IOException ioe){} }while(inp != ‘+’ && inp != ‘-’ && inp != ‘e’); if(inp == ‘+’) c.inc(); else if(inp == ‘-’) c.dec(); }while(inp != ‘e’); }catch(Exception ex){ System.out.println("Exception: "+ex.getMessage()); System.exit(1); } } }
四.测试、调试及运行结果 题目一: 启动名字服务器:X:\corba >tnameserv -ORBInitialPort 1050 启动服务端程序:X:\corba >java HelloWorldServer -ORBInitialPort 1050 输出:HelloWorld is running 启动客户端程序:X:\corba >java HelloWorldClient -ORBInitialPort 1050 输出:
题目二: 5 运行 启动名字服务器:X:\corba >tnameserv -ORBInitialPort 1050 启动服务端程序:X:\corba >java Server -ORBInitialPort 1050 输出:Server started. Stop: Ctrl-c 启动客户端程序:X:\corba >java Client -ORBInitialPort 1050 进行一些加减以及退出操作: 五.经验归纳 与传统的客户/服务器模式(称为双重客户/服务器模式)不同,CORBA是一种多重客户/服务器结构,更确切地说,是一种三重客户/服务器模式。双重客户/服务器模式存在的问题是两者耦合太紧,它们之间采用一种私有协议通信,服务器的改变将影响到客户端。多重客户/服务器与此不同,两者之间的通信不能直接进行,而需要通过中间的一种叫做代理的方式进行。在CORBA中这种代理就是ORB,通过它,客户和服务器不再关心通信问题,它们只需关心功能上的实现。其实,CORBA就是一种中间件技术。