Orbacus名字服務(wù)的使用
Orbacus 是 IONA 提供的開源 COBRA 實現(xiàn),但其相關(guān)技術(shù)文檔去很不詳盡,而且還有錯誤,以下是以舉例的方式說明了如何使用其提供的名字服務(wù)。
1 啟動名字服務(wù)
java -Dooc.orb.oa.endpoint="iiop --port 9998" -classpath OBNaming.jar;OB.jar com.ooc.CosNaming.Server
其中通過 ooc.orb.oa.endpoint="iiop --port 9998" 設(shè)置服務(wù)的端口
2 服務(wù)器段代碼:
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;
??? }
}
posted on 2006-11-19 22:02 超越巔峰 閱讀(1766) 評論(3) 編輯 收藏 所屬分類: CORBA