クライアントアプリケーションが CORBA オブジェクト上のオペレーションを呼び出すには、そのオブジェクトへの参照が必要です。この参照はさまざまな方法で取得できます。たとえば、ORB.resolve_initial_references() を呼び出す方法や、ネームサービス等の別の CORBA オブジェクトを使う方法があります。ここまでのレッスンでは、この両方の方法を使って初期オブジェクト参照を取得しました。
しかし、分散環境で利用できるネームサービスがないことがよくあります。 この場合は、CORBA クライアントは「文字列化された」オブジェクト参照を使って最初のオブジェクトを検索します。
このレッスンでは、 サーバの起動手順の一部として文字列化されたオブジェクト参照を作成する方法、およびクライアントが参照を取得して実際のオブジェクト参照として使うために文字列化を解除する方法を学びます。
このレッスンの手順は次のとおりです。
完成したソースコードを見るには、HelloServer.java と HelloClient.java をクリックしてください。
文字列化されたオブジェクト参照の作成
オブジェクト参照をクライアントから利用できるようにするには、サーバがその参照を作成して、クライアントからアクセスできる場所に保存する必要があります。このレッスンでは、参照をテキストファイルの形式で作成してディスクに保存します。
import java.io.*; // needed for output to the file system.
import org.omg.CosNaming.*; // not needed for stringified version import org.omg.CosNaming.NamingContextPackage.*; // remove from code
      // Get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext ncRef = NamingContextHelper.narrow(objRef);
      
      // Bind the object reference in naming
      NameComponent nc = new NameComponent("Hello", " ");
      NameComponent path[] = {nc};
      ncRef.rebind(path, helloRef);
      String ior = orb.object_to_string(helloRef);
      String filename = System.getProperty("user.home")+
            System.getProperty("file.separator")+"HelloIOR";
      FileOutputStream fos = new FileOutputStream(filename);
      PrintStream ps = new PrintStream(fos);
      ps.print(ior);
      ps.close();
Windows ユーザの方は、このマニュアルのパスのスラッシュ (/) をバックスラッシュ (\) に置き換えてください。
import java.io.*; // needed for input from the file system.
import org.omg.CosNaming;* // not needed for stringified version
      // Get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext ncRef = NamingContextHelper.narrow(objRef);
      
      // Resolve the object reference in naming
      NameComponent nc = new NameComponent("Hello", " ");
      NameComponent path[] = {nc};
      Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
      String filename = System.getProperty("user.home")+
            System.getProperty("file.separator")+"HelloIOR";
      FileInputStream fis = new FileInputStream(filename);
      DataInputStream dis = new DataInputStream(fis);
      String ior = dis.readLine();
これで、HelloClient アプリケーションは、文字列化されたオブジェクト参照の入っている String オブジェクトを取得しました。
オブジェクト参照の文字列化解除
ior 内のオブジェクト参照を文字列化解除するには、標準 ORB メソッドを呼び出します。
      org.omg.CORBA.Object obj = orb.string_to_object(ior);
最後に、CORBA オブジェクトを適切な型にナロー変換して、クライアントから呼び出せるようにします。
      Hello helloRef = HelloHelper.narrow(obj);
これ以外のクライアントコードは、前のレッスンと同じなので変更しません。
文字列化版の Hello World をコンパイルして実行する手順は、ネームサービス版とほとんど同じです。
Hello World の設定
プロジェクトディレクトリは次のようになります。
string
 |-HelloServer.java
 |-HelloClient.java
 |-HelloApp
    |-_HelloImplBase.class
    |-_HelloStub.class
    |-Hello.class
    |-HelloHelper.class
    |-HelloHolder.class
javac *.java
このレッスンで作ったサーバプログラムが確実に実行されるよう、前のレッスンで実行した Hello サーバとネームサーバのすべてのプロセスが停止していることを確認します。
java HelloServer -ORBInitialPort 1050 &
java HelloClient -ORBInitialPort 1050
コマンド行に、次の文字列が表示されます。
Hello world!!
前のレッスン | チュートリアルのホーム | HelloClient.java | HelloServer.java
| ホーム |