Borland(R) Enterprise Server VisiBroker(R) デベロッパーズガイド

[目次][索引][前へ][次へ]

14.15.1 名前のバインド

BankネーミングのサンプルではAccountManagerインタフェースを使用してAccountをオープンしたりアカウントの残高を問い合わせたりします。次に示すServerクラスは,名前をオブジェクトリファレンスにバインドするためのネーミングサービスの使い方を説明します。サーバは,ネーミングサーバのルートコンテキストにIORをバインド登録し,これは次にクライアントが検索します。

このサンプルでは,次の方法を理解できるようになります。

  1. ネーミングサービスのルートコンテキストへのリファレンスを取得するための,VisiBroker ORBインスタンスのresolve_initial_referencesメソッドの使用方法(サンプルでは,NameServiceのデフォルト名でネーミングサービスを起動しなければなりません)
  2. NamingContextExtHelperクラスのnarrowメソッド使用による,ルートコンテキストのリファレンスへのキャスト方法
  3. AccountManagerImplオブジェクトのPOAおよびサーバントの生成方法
  4. 最後にNamingContextインタフェースのbindメソッドを使用して,「BankManager」という名前をAccountManagerImplオブジェクトのオブジェクトリファレンスにバインドする方法

コードサンプル14-10 Server.c(C++)
 
#include "CosNaming_c.hh"
#include "BankImpl.h"
 
// USE_STD_NS is a define setup by VisiBroker to use the std namespace
USE_STD_NS
 
int main(int argc, char* const* argv) {
   try {
      // Initialize the ORB.
      CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
 
      // get a reference to the root POA
      PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(
                   orb->resolve_initial_references("RootPOA"));
 
      // get a reference to the Naming Service root_context
      CosNaming::NamingContext_var rootContext =
                  CosNaming::NamingContext::_narrow(
                  orb->resolve_initial_references("NameService"));
 
    CORBA::PolicyList policies;
    policies.length(1);
 
    policies[(CORBA::ULong)0] =
         rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);
 
      // get the POA Manager
      PortableServer::POAManager_var poa_manager =
                   rootPOA->the_POAManager();
 
      // Create myPOA with the right policies
      PortableServer::POA_var myPOA =
         rootPOA->create_POA("bank_agent_poa", poa_manager, policies);
 
      // Create the servant
      AccountManagerImpl managerServant;
 
      // Decide on the ID for the servant
      PortableServer::ObjectId_var managerId =
      PortableServer::string_to_ObjectId("BankManager");
 
      // Activate the servant with the ID on myPOA
      myPOA->activate_object_with_id(managerId, &managerServant);
 
      // Activate the POA Manager
      poa_manager->activate();
 
      CORBA::Object_var reference =
      myPOA->servant_to_reference(&managerServant);
 
      // Associate the bank manager with the name at the root context
      CosNaming::Name name;
      name.length(1);
      name[0].id = (const char *) "BankManager";
      name[0].kind = (const char *) "";
      rootContext->rebind(name, reference);
 
      cout << reference << " is ready" << endl;
 
      // Wait for incoming requests
      orb->run();
  }
      catch(const CORBA::Exception& e) {
      cerr << e << endl;
      return 1;
  }
 
  return 0;
}
 

コードサンプル14-11 Server.java(Java)
 
import org.omg.PortableServer.*;
import org.omg.CosNaming.*;
 
public class Server {
   public static void main(String[ ] args){
      try {
         // Initialize the ORB.
         org.omg.CORBA.ORB orb =
                          org.omg.CORBA.ORB.init(args,null);
         // get a reference to the rootPOA
         POA rootPOA = POAHelper.narrow(
                 orb.resolve_initial_references("RootPOA"));
         // get a reference to the Naming Service root 
         // context
         org.omg.CORBA.Object rootObj =
                 orb.resolve_initial_references("NameService");
         NamingContextExt root = 
                     NamingContextExtHelper.narrow(rootObj);
 
         // Create policies for our persistent POA
         org.omg.CORBA.Policy[ ] policies = {
             rootPOA.create_lifespan_policy(
                             LifespanPolicyValue.PERSISTENT)
         };
         // Create myPOA with the right policies
         POA myPOA = rootPOA.create_POA(
                  "bank_agent_poa", rootPOA.the_POAManager(),
                  policies );
         // Create the servant
         AccountManagerImpl managerServant =
                                 new AccountManagerImpl();
         // Decide on the ID for the servant
         byte[ ] managerId = "BankManager".getBytes();
         // Activate the servant with the ID on myPOA
         myPOA.activate_object_with_id(managerId,
                                        managerServant);
 
         // Activate the POA manager
         rootPOA.the_POAManager().activate();
 
         // Associate the bank manager with the name at
         // the root context Note that casting is needed
         // as a workaround for a JDK 1.1.x bug.
         ((NamingContext)root).bind(
               root.to_name("BankManager"),
               myPOA.servant_to_reference(managerServant));
 
         System.out.println(
                  myPOA.servant_to_reference(managerServant)
                  + " is ready.");
         // Wait for incoming requests
         orb.run();
      }catch (Exception e){
         e.printStackTrace();
      }
   }
}