Orbacus
是
IONA
提供的开源
COBRA
实现,但其相关技术文档去很不详尽,而且还有错误,以下是以举例的方式说明了如何使用其提供的名字服务。
1
启动名字服务
java -Dooc.orb.oa.endpoint="iiop --port 9998" -classpath OBNaming.jar;OB.jar com.ooc.CosNaming.Server
其中通过
ooc.orb.oa.endpoint="iiop --port 9998"
设置服务的端口
2
服务器段代码:
import
helloorb.Hello;
import
helloorb.Hello_Impl;
import
org.omg.CosNaming.NameComponent;
import
org.omg.CosNaming.NamingContextExt;
import
org.omg.CosNaming.NamingContextExtHelper;
import
com.
ooc
.CORBA.ORB;
import
com.ooc.OBPortableServer.POA;
import
com.ooc.OBPortableServer.POAHelper;
import
com.ooc.OBPortableServer.POAManager;
public
class
ByNamingServer {
public
static
void
main(String[] args) {
java.util.Properties props = System.getProperties();
props.put(
"org.omg.CORBA.ORBClass"
,
"com.ooc.CORBA.ORB"
);
props.put(
"org.omg.CORBA.ORBSingletonClass"
,
"com.ooc.CORBA.ORBSingleton"
);
org.omg.CORBA.ORB orb =
null
;
try
{
orb = ORB.init(args, props);
org.omg.CORBA.Object poaObj =
null
;
try
{
poaObj = orb.resolve_initial_references(
"RootPOA"
);
}
catch
(org.omg.CORBA.ORBPackage.InvalidName ex) {
throw
new
RuntimeException();
}
POA rootPOA = POAHelper.narrow(poaObj);
org.omg.PortableServer.POAManager manager = rootPOA
.the_POAManager();
org.omg.CORBA.Object obj =
null
;
try
{
String nameService=
"corbaloc::127.0.0.1:9998/NameService"
;
obj = orb.string_to_object(nameService);
}
catch
(Exception ex) {
ex.printStackTrace();
throw
new
RuntimeException();
}
if
(obj ==
null
) {
throw
new
RuntimeException();
}
NamingContextExt nc =
null
;
try
{
Hello_Impl helloImpl =
new
Hello_Impl();
Hello hello = helloImpl._this(orb);
NameComponent[] a2Name =
new
NameComponent[1];
a2Name[0] =
new
NameComponent();
a2Name[0].
id
=
"HelloServer"
;
a2Name[0].
kind
=
""
;
nc = NamingContextExtHelper.narrow(obj);
nc.rebind(a2Name, hello);
manager.activate();
}
catch
(Exception ex) {
ex.printStackTrace();
throw
new
RuntimeException();
}
}
catch
(Exception e1) {
e1.printStackTrace();
}
}
}
3
客户端代码
import
org.omg.CosNaming.NameComponent;
import
org.omg.CosNaming.NamingContextExt;
import
org.omg.CosNaming.NamingContextExtHelper;
import
helloorb.Hello;
import
helloorb.HelloHelper;
public
class
ByNamingClient {
public
static
void
main(String args[]) {
java.util.Properties props = System.getProperties();
props.put(
"org.omg.CORBA.ORBClass"
,
"com.ooc.CORBA.ORB"
);
props.put(
"org.omg.CORBA.ORBSingletonClass"
,
"com.ooc.CORBA.ORBSingleton"
);
int
status = 0;
org.omg.CORBA.ORB orb =
null
;
try
{
orb = org.omg.CORBA.ORB.init(args, props);
status = run(orb);
}
catch
(Exception ex) {
ex.printStackTrace();
status = 1;
}
if
(orb !=
null
) {
try
{
orb.destroy();
}
catch
(Exception ex) {
ex.printStackTrace();
status = 1;
}
}
System.exit(status);
}
static
int
run(org.omg.CORBA.ORB orb) {
org.omg.CORBA.Object obj =
null
;
/* try {
String refFile = "Hello.ref";
java.io.BufferedReader in = new java.io.BufferedReader(
new java.io.FileReader(refFile));
String ref = in.readLine();
obj = orb.string_to_object(ref);
} catch (java.io.IOException ex) {
ex.printStackTrace();
return 1;
}*/
try
{
obj = orb.string_to_object(
"corbaloc::127.0.0.1:9998/NameService"
);
NamingContextExt nc = NamingContextExtHelper.narrow(obj);
/* NameComponent[] a2Name = new NameComponent[1];
a2Name[0] = new NameComponent();
a2Name[0].id = "HelloServer";
a2Name[0].kind = "";*/
Hello hello = HelloHelper.narrow(nc.resolve_str(
"HelloServer"
));
hello.say_hello();
}
catch
(Exception e){
e.printStackTrace();
}
return
0;
}
}