import java.util.Properties;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
/**
*
* @author chenyi
*/
public class OSPORBHelper {
private ORB orb;
private POA rootpoa;
private NamingContextExt ncRef;
private String clientName;
private String listenerEndPoint = null;
private String JacOrbHomeLib = null;
private String JacORB_NS = null;
private POA persistentPOA = null;
private boolean orb_init = false;
/**
*
* @param props
* @return int
*/
public int init(Properties props) {
try {
orb = ORB.init(new String[0], props);
} catch (Exception e) {
return -1;
}
try{
resolve_root_poa();
}catch(Exception e){
return -2;
}
try{
resolve_naming_service();
}catch(Exception e){
return -3;
}
orb_init = true;
return 0;
}
/**
* 获取POA
*/
private void resolve_root_poa() throws Exception {
rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
/* create policies */
org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[3];
policies[0] = rootpoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
policies[1] = rootpoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
policies[2] = rootpoa.create_servant_retention_policy(ServantRetentionPolicyValue.RETAIN);
/* create POA */
persistentPOA = rootpoa.create_POA("PersistentPOA", rootpoa.the_POAManager(), policies);
// 激活该对象,否则还处于“保持状态”无法处理任何请求
rootpoa.the_POAManager().activate();
policies[0].destroy();
policies[1].destroy();
policies[2].destroy();
policies = null;
}
/**
* 获得NameServices的一个引用
* 客户端与服务端都需要再获取根名称索引后才能启动
*/
private void resolve_naming_service() throws Exception {
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext,
ncRef = NamingContextExtHelper.narrow(objRef);
}
private void init_orb() {
}
public ORB getOrb() {
return orb;
}
public POA getRootpoa() {
return rootpoa;
}
public NamingContextExt getNcRef() {
return ncRef;
}
public String getClientName() {
return clientName;
}
public String getListenerEndPoint() {
return listenerEndPoint;
}
public String getJacOrbHomeLib() {
return JacOrbHomeLib;
}
public String getJacORB_NS() {
return JacORB_NS;
}
public POA getPersistentPOA() {
return persistentPOA;
}
public void clearRootPOA(){
this.rootpoa = null;
}
/**
* 查看orb是否初始化
* @return boolean
*/
public synchronized boolean getOrbInit() {
return orb_init;
}
public synchronized void setOrbInit(boolean b){
this.orb_init = b;
}
}
一主类中,ORB属性设置如下,并初始化ORB:
props = new Properties();
props.put("java.endorsed.dirs", "E:/JacORB/lib");
props.put("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
props.put("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
props.put("ORBInitRef.NameService", "corbaloc::192.168.100.221:5060/NameService");
props.put("jacorb.user_imr", "on");
props.put("jacorb.use_imr_endpoint", "on");
props.put("jacorb.implname", "TEST");
props.put("ORBDottedDecimalAddresses", 1);
props.put("OAAddress", "iiop://192.168.100.100:10025");
// start initialize ORB
OSPORBHelper orbHelper = new OSPORBHelper();
int ret = orbHelper.init(props);
关闭ORB的方法如下:
public void exit() {
if (orbHelper.getNcRef() != null) {
try {
NameComponent nc = new NameComponent(clientName, OSP_PM_DATAHANDLER);
NameComponent path[] = new NameComponent[]{nc};
orbHelper.getNcRef().unbind(path);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
NameComponent nc = new NameComponent(clientName, OSP_ADMIN_HANDLER);
NameComponent path[] = new NameComponent[]{nc};
orbHelper.getNcRef().unbind(path);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
orbHelper.getOrb().shutdown(false);
orbHelper.getOrb().destroy();
orbHelper.clearRootPOA();
} catch (Exception ex) {
ex.printStackTrace();
}
}
orbHelper.setOrbInit(false);
}
按照这个配置就可以使用JacORB将客户端的地址绑定为本机的192.168.100.100,端口10025。如果机器IP不一样可以修改对应的OAAddress后面的字符串。
ORBDottedDecimalAddresses属性为1是使用IPv4。jacorb.user_imr打开就是使用持久化Corba对象,同时需要设置jacorb.implname
这样ORB就初始化好了。
注册命名服务和一般的瞬时Corba对象有所不同,ospIPCproxyImpl是实现IDL 转的 Corba接口的类,代码就不提供了
ospIPCproxyImpl m_ipcproxy = new ospIPCproxyImpl();
// get object reference from the servant
// there use persistent poa and active the object using id.
orbhelper.getPersistentPOA().activate_object_with_id(name.getBytes(), m_ipcproxy);
obj = orbhelper.getPersistentPOA().servant_to_reference(m_ipcproxy);
//name component, type is "objectType" and name is "TEST"
NameComponent nc = new NameComponent("TEST", "objectType");
NameComponent path[] = new NameComponent[]{nc};
// bind
orbhelper.getNcRef().bind(path, obj);
System.out.println("注册成功");