13.3.1 あるインタフェースのすべてのインスタンスの検索

コードサンプル13-1および13-2は,all_instances()メソッドを使用してAccountインタフェースのすべてのインスタンスを探します。スマートエージェントへの問い合わせは,ORB::resolve_initial_references()メソッド(C++)またはORB.resolve_initial_references()メソッド(Java)に「LocationService」を渡し,そのメソッドが返したオブジェクトをObjLocation::Agent(C++)またはObjLocation.Agent(Java)にナロウすることによって行われることに注意してください。またAccountリポジトリIDのフォーマットはIDL:Bank/Account:1.0であることにも注意してください。

コードサンプル13-1 AccountManagerインタフェースの要求を満たすすべてのインスタンスの検索(C++)

#include "corba.h"
#include "locate_c.hh"

// USE_STD_NS is a define setup by VisiBroker to use the std
// namespace USE_STD_NS

int main(int argc, char** argv)
{
  try {
     // ORB initialization
     CORBA::ORB_var the_orb = CORBA::ORB_init(argc, argv);
     // Obtain a reference to the Location Service
     CORBA::Object_var obj = the_orb->
        resolve_initial_references("LocationService");
     if ( CORBA::is_nil(obj) ) {
        cout << "Unable to locate initial LocationService"
              << endl;
        return 0;
     }
     ObjLocation::Agent_var the_agent =
           ObjLocation::Agent::_narrow(obj);
     // Query the Location Service for all implementations of
     // the Account interface
     ObjLocation::ObjSeq_var accountRefs =
        the_agent->all_instances(
        "IDL:Bank/AccountManager:1.0");
     cout << "Obtained " << accountRefs->length()
           << "Account objects" << endl;
     for (CORBA::ULong i=0; i < accountRefs->length(); i++) {
        cout << "Stringified IOR for account #"<< i << ":"
              << endl;
        CORBA::String_var stringified_ior(
              the_orb->object_to_string(accountRefs[i]));
        cout << stringified_ior << endl;
        cout << endl;
     }
  } catch (const CORBA::Exception& e) {
     cout << "Caught exception: " << e << endl;
     return 0;
  }
  return 1;
}

コードサンプル13-2 AccountManagerインタフェースの要求を満たすすべてのインスタンスの検索(Java)

// AccountFinder.java
public class AccountFinder {
  public static void main(String[ ] args){
  try {
     // Initialize the ORB.
     org.omg.CORBA.ORB orb =
                         org.omg.CORBA.ORB.init(args,null);
     com.inprise.vbroker.ObjLocation.Agent the_agent = null;
     try {
        the_agent =
            com.inprise.vbroker.ObjLocation.AgentHelper.narrow(
            orb.resolve_initial_references("LocationService"));
     } catch (org.omg.CORBA.ORBPackage.InvalidName e){
        System.out.println(
           "Not able to resolve references " +
           "for LocationService");
        System.exit(1);
     } catch (Exception e){
        System.out.println(
           "Unable to locate LocationService!");
        System.out.println("Caught exception: " + e);
        System.exit(1);
     }
     org.omg.CORBA.Object[ ] accountRefs =
        the_agent.all_instances(
                     "IDL:Bank/AccountManager:1.0");
     System.out.println("Agent returned " +
        accountRefs.length  +
        "object references");
     for (int i=0; i <accountRefs.length; i++){
        System.out.println(
            "Stringified IOR for account #" + (i+1) + ":");
        System.out.println(orb.object_to_string(
                                          accountRefs[i]));
        System.out.println();
     }
  }catch (Exception e){
     System.out.println("Caught exception: " + e);
     System.exit(1);
  }
}
}