免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2424 | 回复: 0
打印 上一主题 下一主题

CORBA入门 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-19 15:42 |只看该作者 |倒序浏览

               
一、使用到的软件
JacORB 2.3
Eclipse SDK 3.3
JDK5 1.5.0_11
ORB Studio 7.7.7
使用的系统为Windows XP SP2。版本号可能不一定要完全按照上面,但是如果你配置成功不了,不妨就按上面来弄。
二、为eclipse安装corba开发插件。
ORB Studio是开发corba的插件,用于eclipse。
安装其实很简单,把ORBStudio_7.7.7.jar文件拷贝到eclipse的plugins目录。
三、安装JacORB,生成jaco.bat
(1)安装
解压JacORB-2.3.0-bin.zip到D:\JavaTools,JacORB的根目录为JacORB-2.3.0。
(2)生成jaco.bat,这个脚本用来启动服务端和客户端程序。
进入D:\JavaTools\JacORB-2.3.0,执行下列的命令
set ANT_HOME=E:\apache-ant-1.7.0
set CLASSPATH=%ANT_HOME%\lib\ant.jar;
set PATH=%PATH%;%ANT_HOME%\bin;
ant jaco
就可以生成jaco.bat,自己编写一个也可以,只要改掉里面的路径。
######################################################
@echo off
rem call java interpreter
java -Djava.endorsed.dirs=D:\JavaTools\JacORB-2.3.0/lib -Djacorb.home=D:\JavaTools\JacORB-2.3.0 -Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB -
Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton -classpath %CLASSPATH% %*
######################################################
四、在eclipse里面配置ORB Studio和JacORB。
在eclipse的菜单栏中,选【windows】->[preferences]->[ORB Studio]->[IDL Compiler],选中JacORB。
然后展开[IDL Compiler],设置JacORB的IDL Command和Command Options。
默认参数是:
IDL Command:java
Command Options:-cp "/Tools/JacORB/lib/idl.jar;/Tools/JacORB/lib/logkit-1.2.jar" org.jacorb.idl.parser -d %D% %F%
第二个参数要设置成正确的路径。
-cp "D:\JavaTools\JacORB-2.3.0\lib/idl.jar;D:\JavaTools\JacORB-2.3.0\lib/logkit-1.2.jar" org.jacorb.idl.parser -d %D% %F%
五、corba程序示例
在D:\JavaTools\JacORB-2.3.0\demo取一个简单的例子hello
以下操作在eclipse中进行。
1、建立一个IDL文件,如demo.idl
module demo
{
    module hello {
        interface GoodDay {
            string hello_latin1();
            wstring hello_chinese();           
        };
    };
};
2、右键点击demo.idl文件,选[ORB Menu]->[Compile],会生成demo.hello包,里面包含7个自动生成的文件
_GoodDayStub.java
GoodDay.java
GoodDayHelper.java
GoodDayHolder.java
GoodDayOperations.java
GoodDayPOA.java
GoodDayPOATie.java
3、实现demo.idl文件hello模块中GoodDay接口,手动创建GoodDayImpl.java文件
package demo.hello;
public class GoodDayImpl extends GoodDayPOA {
public String hello_chinese() {
  // TODO Auto-generated method stub
  return "你好, 世界";
}
public String hello_latin1() {
  // TODO Auto-generated method stub
  return "Hello, World";
}
}
4、创建Server和Client(服务器端和客户端)程序
Server.java
//////////////////////////////////////////////////////////////////////
package demo.hello;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
public class Server {
public static void main(String[] args) {
  try {
   ORB orb = ORB.init(args, null); // 初始化 ORB
   
   POA poa = POAHelper.narrow(orb
     .resolve_initial_references("RootPOA")); // 初始化 POA
   poa.the_POAManager().activate();
   // 创建一个 GoodDay 对象
   GoodDayImpl goodDayImpl = new GoodDayImpl();
   // 创建 GoodDay 对象的引用
   org.omg.CORBA.Object obj = poa.servant_to_reference(goodDayImpl);
   // 使用 naming service
   NamingContextExt nc = NamingContextExtHelper.narrow(orb
     .resolve_initial_references("NameService"));
   
   nc.bind(nc.to_name("hello.goodDay"), obj); // 绑定对象
   orb.run();
  } catch (Exception e) {
   e.printStackTrace();
  }
}
}
Client.java
//////////////////////////////////////////////////////////////////////
package demo.hello;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
public class Client {
public static void main(String[] args) {
  try{
   GoodDay goodDay;
   
   ORB orb = ORB.init(args, null); // 初始化 ORB
   
   // 使用 naming service
   NamingContextExt nc = NamingContextExtHelper.narrow(orb
     .resolve_initial_references("NameService"));
   org.omg.CORBA.Object obj = nc.resolve(nc.to_name("hello.goodDay")); //解析对象
   
   goodDay = GoodDayHelper.narrow(obj); //转换
   
   // GoodDay 接口调用
   System.out.println(goodDay.hello_latin1());
   System.out.println(goodDay.hello_chinese());
  }catch(Exception e) {
   e.printStackTrace();
  }
}
}
5、运行程序
(1)运行JacORB的ns
先把jacorb_properties.template文件另存为jacorb.properties,且把下面两行
#ORBInitRef.NameService=file:/c:/NS_Ref
ORBInitRef.NameService=http://www.x.y.z/~user/NS_Ref
改为
ORBInitRef.NameService=file:/c:/NS_Ref
#ORBInitRef.NameService=http://www.x.y.z/~user/NS_Ref
注意:
ORBInitRef.NameService=file:/d:/NS_Ref
设成D盘或E盘死活不行。
先设置环境变量
======================================================
set JacORB_HOME=E:\JacORB_2.3.0_beta2
set CLASSPATH=.;%JacORB_HOME%\lib\idl.jar;%JacORB_HOME%\lib\jacorb.jar;%JacORB_HOME%\lib\logkit-1.2.jar;
set PATH=%PATH%;%JacORB_HOME%\bin;
======================================================
ns
(2)运行Server和Client
进入代码目录demo的上级目录(例如:cd E:\workspace\CorbaTest\bin),在运行之前设置环境变量,然后运行下面的代码
jaco demo.hello.Server
jaco demo.hello.Client
启动NS
E:\JacORB_2.3.0_beta2>ns
[jacorb.orb.print_ver] INFO :
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        JacORB V 2.3-beta-2, www.jacorb.org
        (C) The JacORB project 14-Oct-2006
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[jacorb.orb] INFO : Property "jacorb.hashtable_class" is set to: java.util.Hasht
able
[org.jacorb.orb.codes] WARN : Warning - unknown codeset (GBK) - defaulting to IS
O-8859-1
[jacorb.orb.intercept] INFO : InterceptorManager started with 0 Server Intercept
ors, 0 Client Interceptors and 1 IOR Interceptors
[jacorb.orb.singleton] INFO : created ORBSingleton
[jacorb.naming] INFO : NS up
[jacorb.orb] INFO : ORB run
启动Server:
E:\workspace\CorbaTest\bin>jaco demo.hello.Server
[jacorb.orb.print_ver] INFO :
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        JacORB V 2.3-beta-2, www.jacorb.org
        (C) The JacORB project 14-Oct-2006
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[jacorb.orb] INFO : Property "jacorb.hashtable_class" is set to: java.util.Hash
able
[org.jacorb.orb.codes] WARN : Warning - unknown codeset (GBK) - defaulting to I
O-8859-1
[jacorb.orb.intercept] INFO : InterceptorManager started with 0 Server Intercep
ors, 0 Client Interceptors and 1 IOR Interceptors
[jacorb.poa] INFO : oid:
00 15 2B 05 4C 21 35 24 02 1F 30                            ..+.L!5$..0
object is activated
[jacorb.poa] INFO : Using server ID (1842945734) for transient POA
[jacorb.orb.singleton] INFO : created ORBSingleton
[jacorb.orb.giop] INFO : ClientConnectionManager: created new ClientGIOPConnect
on to 10.0.18.30:4008 (c68c3)
[jacorb.orb.iiop] INFO : Connected to 10.0.18.30:4008 from local port 4026
[jacorb.orb] INFO : ORB run
启动Client:
E:\workspace\CorbaTest\bin>jaco demo.hello.Client
[jacorb.orb.print_ver] INFO :
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        JacORB V 2.3-beta-2, www.jacorb.org
        (C) The JacORB project 14-Oct-2006
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[jacorb.orb] INFO : Property "jacorb.hashtable_class" is set to: java.util.Hash
able
[org.jacorb.orb.codes] WARN : Warning - unknown codeset (GBK) - defaulting to I
O-8859-1
[jacorb.orb.intercept] INFO : InterceptorManager started with 0 Server Intercep
ors, 0 Client Interceptors and 1 IOR Interceptors
[jacorb.orb.singleton] INFO : created ORBSingleton
[jacorb.orb.giop] INFO : ClientConnectionManager: created new ClientGIOPConnect
on to 10.0.18.30:4008 (872380)
[jacorb.orb.iiop] INFO : Connected to 10.0.18.30:4008 from local port 4041
[jacorb.orb.giop] INFO : ClientConnectionManager: created new ClientGIOPConnect
on to 10.0.18.30:4025 (f11404)
[jacorb.orb.iiop] INFO : Connected to 10.0.18.30:4025 from local port 4042
hello
你好 !
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40588/showart_499709.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP