IDLおよびPOAを使用したHello World

この単純な例によって、POA一時的オブジェクトサポートを使用したORBでのJava CORBAプログラミングについて学習できます。IDLはオブジェクトインタフェースを定義するために使用されます。

1 Hello IDLインタフェース

前のHello WorldアプリケーションからのIDLを使用します。
// hello.idl
module helloWorld
{
    interface Hello
    {
    |   string sayHello();
    };
};

2 Javaバインドの生成

idl2javaコンパイラをhello.idlファイル上で実行して、上のIDLからJavaバインドを生成します。
idl2java -poa -compile -ds gensrc -d ../../lib hello.idl
idl2javaコンパイラは、gensrcディレクトリにPOAのJavaコードを生成し、生成されたJavaコードを../../libディレクトリにコンパイルします。次のクラスはhelloWorldJavaパッケージで作成されます。すべてのJavaクラス命はIDLインタフェースのHelloの名前から取得されます。
Hello               // Java interface corresponding to IDL interface
HelloOperations     // methods on the Java interface
HelloHelper         // helper methods for narrow, read, write
HelloHolder         // holder for out and inout parameters
HelloPOA            // inheritance based POA Skeleton
HelloPOATie         // delegation based POA Skeleton
_HelloStub          // Stub

3 Helloの実装

サーバントはHelloPOAクラスを拡張し、sayHelloメソッドを実装します。
package poaHello;
                                                                           
public class HelloImpl extends helloWorld.HelloPOA
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}
HelloPOAを拡張することにより、サーバントはorg.omg.PortableServer.Servantになります。また、org.omg.CORBA.Objectではないので、CORBAオブジェクトをパラメータとして使用するorb.object_to_stringのようなAPIを使用する前に、POA APIを使用してオブジェクト参照を作成する必要があります。

4 Hello Worldサーバ

サーバはORBを作成し、サーバントを作成し、ルートPOAを使用して、オブジェクト参照を作成し、それを文字列化して書き出し、呼び出し待ちをします。

ルートPOAはパラメータとして「RootPOA」でorb.resolve_initial_referencesを使用して、ORBから取得できるローカル性が制約されたオブジェクトです。

package poaHello;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Servant hello = new HelloImpl();
    |   |                                                                  
    |   |   // get the root POA
    |   |   POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(
    |   |       rootPOA.servant_to_reference(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // activate the Root POA
    |   |   rootPOA.the_POAManager().activate();
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
rootPOA.the_POAManaget().activateステートメントは任意です。orb.run()は自動的にルートPOAを有効にします。

5 Hello Worldクライアント

IDLインタフェースは変更されていないため、Hello Worldの例からクライアントを使用します。
package helloWorld;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloClient
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String helloIOR = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Hello hello = HelloHelper.narrow(orb.string_to_object(helloIOR));
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

6 TIEアプローチ

idl2javaコンパイラは、継承ベースのスケルトンだけではなく、委任ベースのスケルトンの両方を生成します。上では、継承ベースのスケルトンを使用してサーバントを作成する方法を説明しました。ここでは、委任ベースのスケルトンを使用します。TIEアプローチと呼ばれることもあります。

6.1 Helloの実装

HelloImpl2のサンプルからのhelloWorldを引き続き使用できます。
package helloWorld;
                                                                           
public class HelloImpl2 implements HelloOperations
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}

6.2 Hello Worldサーバ

ここでは、サーバントの作成手順が前とは異なります。TIEオブジェクトは実際にHelloPOAを拡張します。
package poaHello;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
                                                                           
import helloWorld.HelloImpl2;
import helloWorld.HelloPOATie;
                                                                           
public class helloServer2
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Servant hello = new HelloPOATie(new HelloImpl2());
    |   |                                                                  
    |   |   // get the root POA
    |   |   POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(
    |   |       rootPOA.servant_to_reference(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // activate the Root POA
    |   |   rootPOA.the_POAManager().activate();
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc.All rights reserved.