16.6 サンプルプログラム

ここでは,アカウントを生成して(再び)オープンするための単純なAccountManagerインタフェースを含む,IRの簡単なサンプルを示します。コードはexamples¥vbe¥irディレクトリ内にあります。初期化時にAccountManagerインプリメンテーションは,管理されたアカウントインタフェースのIR定義と接続します。

これは,特定のAccountインプリメンテーションがすでにインプリメントした追加オペレーションをクライアントに提供します。ここでクライアントはわかっている(IDLに記述されている)すべてのオペレーションにアクセスでき,さらに,ほかのオペレーションをサポートするIRを検証し,それを呼び出せます。サンプルでは,IR定義オブジェクトの管理方法と,C++およびJavaのIRを使用したリモートオブジェクトの検査の方法を示します。

このプログラムをテストするには,次の条件が成立している必要があります。

コードサンプル16-3 IR内のインタフェースのオペレーションと属性の検索(C++)

/* PrintIR.C */
#ifndef _VIS_INCLUDE_IR
#define _VIS_INCLUDE_IR
#endif

#include "corba.h"
#include "strvar.h"

int main(int argc, char *argv[ ]) {
  try {
     if (argc != 2) {
     cout << "Usage: PrintIR idlName" << endl;
     exit(1);
     }
     CORBA::String_var idlName = (const char *)argv[1];

     CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
     CORBA::Repository_var rep = CORBA::Repository::_bind();

     CORBA::Contained_var contained = rep->lookup(idlName);
     CORBA::InterfaceDef_var intDef =
        CORBA::InterfaceDef::_narrow(contained);
     if (intDef != CORBA::InterfaceDef::_nil()) {
     CORBA::InterfaceDef::FullInterfaceDescription_var fullDesc =
           intDef->describe_interface();
     cout << "Operations:" << endl;
     for(CORBA::ULong i = 0;
           i < fullDesc->operations.length(); i++)
        cout << " " << fullDesc->operations[i].name << endl;
        cout << "Attributes:" << endl;
        for(i = 0; i < fullDesc->attributes.length(); i++)
           cout << " " << fullDesc->attributes[i].name
                 << endl;
     }else
        cout << "idlName is not an interface: "
              << idlName << endl;

  } catch (const CORBA::Exception& excep) {
     cerr << "Exception occurred ..." << endl;
     cerr << excep << endl;
     exit(1);
  }
  return 0;
}

コードサンプル16-4 IR内のインタフェースのオペレーションと属性の検索(Java)

// Client.java
import org.omg.CORBA.InterfaceDef;
import org.omg.CORBA.InterfaceDefHelper;
import org.omg.CORBA.Request;
import java.util.Random;

public class Client {
  public static void main(String[ ] args) {
     try {
        // Initialize the ORB.
        org.omg.CORBA.ORB orb =
                         org.omg.CORBA.ORB.init(args,null);
        // Get the manager Id
        byte[ ] managerId = "BankManager".getBytes();
        // Locate an account manager. Give the full POA name
        // and the servant ID.
        Bank.AccountManager manager =
           Bank.AccountManagerHelper.bind(orb,
                                 "/bank_ir_poa", managerId);
        // use args[0] as the account name, or a default.
        String name = args.length > 0 ?
                                   args[0] : "Jack B. Quick";
        // Request the account manager to open a named
        // account.
        Bank.Account account = manager.open(name);
        // Get the balance of the account.
        float balance = account.balance();
        // Print out the balance.
        System.out.println("The balance in " + name +
                               "'s account is $" + balance);
        // Calculate and set a new balance
        balance = args.length > 1 ?
           Float.parseFloat(args[1]):
           Math.abs(new Random().nextInt()) % 100000 / 100f;
        account.balance(balance);
        // Get the balance description if it is possible
        // and print it
        String desc = getDescription(account);
        System.out.println("Balance description:¥n" + desc);
     }catch (org.omg.CORBA.SystemException e) {
        System.err.println("System exception caught:" + e);
     }catch (Exception e) {
        System.err.println("Unexpected exception caught:");
        e.printStackTrace();
     }
  }

  static String getDescription(Bank.Account account) {
     // Get the interface repository definition for
     // this interface
     InterfaceDef accountDef = InterfaceDefHelper.
        narrow(account._get_interface_def());
     // Check if this *particular* implementation supports
     // "describe" operation
     if (accountDef.lookup("describe") != null) {
        // We cannot use the static skeleton's method here
        // because at the time of its creation this method
        // was not present in the IDL's version of the
        // Account interface. Use DII instead.
        Request request = account._request("describe");
        request.result().value().insert_string("");
        request.invoke();
        return request.result().value().extract_string();
     }else {
        return "<no description>";
     }
  }
}