IDLを使用したHello World

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

1 Hello IDLインタフェース

Hello WorldアプリケーションのIDLを次に示します。インタフェースHelloには1つのメソッドsayHelloがあり、パラメータを採用せず、文字列を返します。IDLはオブジェクト実装とそのクライアントの間のコントラクトとして機能します。
// hello.idl
module helloWorld
{
    interface Hello
    {
    |   string sayHello();
    };
};

2 Javaバインドの生成

idl2javaコンパイラをhello.idlファイル上で実行して、前のIDLからJavaバインドを生成します。
idl2java -compile -ds gensrc -d ../../lib hello.idl
idl2javaコンパイラはgensrcディレクトリに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
_HelloImplBase      // inheritance based Skeleton
_HelloTie           // delegation based Skeleton
_HelloStub          // Stub

3 Helloの実装

サーバントは_HelloImplBaseクラスを拡張し、sayHelloメソッドを実装します。
package helloWorld;
                                                                           
public class HelloImpl extends _HelloImplBase
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}
_HelloImplBaseを拡張することにより、サーバントはorg.omg.CORBA.Objectインタフェースも実装します。サーバントをオブジェクト参照としても使用できます。

4 Hello Worldサーバ

サーバはORBを作成し、CORBAオブジェクトを作成し、その文字列化されたオブジェクト参照を書き出し、呼び出しを待機します。
package helloWorld;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant and register with ORB
    |   |   Hello hello = new HelloImpl();
    |   |   orb.connect(hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(hello);
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
orb.connect(hello)ステートメントはオプションです。未接続のサーバント/objrefをマーシャルするか、文字列化する場合、ORBは自動的に接続します。

5 Hello Worldクライアント

クライアントはORBを作成し、Helloオブジェクトの文字列化されたオブジェクト参照を読み込み、Helloタイプに絞込み、それに対してsayHelloメソッドを呼び出します。
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アプローチと呼ばれることもあります。

継承ベースのスケルトンを利用すると、ランタイムに作成されるオブジェクト数が少なくなります。主な欠点は、他に使用する1つの実装継承を使い果たしてしまうことです。

6.1 Helloの実装

サーバントはHelloOperationsインタフェースを実装します。
package helloWorld;
                                                                           
public class HelloImpl2 implements HelloOperations
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}

6.2 Hello Worldサーバ

前に示した通り、サーバはORBを作成し、CORBAオブジェクトを作成し、その文字列化されたオブジェクト参照を書き出し、呼び出しを待ちます。

ここでは、サーバントの作成手順が前とは異なります。TIEオブジェクトは実際に_HelloImplBaseを拡張します。

package helloWorld;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloServer2
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a TIEd servant and register with ORB
    |   |   Hello hello = new _HelloTie(new HelloImpl2());
    |   |   orb.connect(hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(hello);
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc.All rights reserved.