1 Hello IDLインタフェース
Hello WorldアプリケーションのIDLを次に示します。インタフェースHelloには1つのメソッドsayHelloがあり、パラメータを採用せず、文字列を返します。IDLはオブジェクト実装とそのクライアントの間のコントラクトとして機能します。
// hello.idl
module helloWorld
{
interface Hello
{
| string sayHello();
};
};
idl2java -compile -ds gensrc -d ../../lib hello.idlidl2javaコンパイラは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
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は自動的に接続します。
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();
| }
}
}
継承ベースのスケルトンを利用すると、ランタイムに作成されるオブジェクト数が少なくなります。主な欠点は、他に使用する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. |