4.5.1 サーバプログラム

このファイルは,サンプルのbankでサーバ側のServerクラスをインプリメントします。コードサンプル4-4はC++のサーバ側プログラムの例です。コードサンプル4-5はJavaのサーバ側プログラムの例です。サーバプログラムは次のように動作します。

コードサンプル4-4 Server.Cプログラム(C++)

#include "BankImpl.h"
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 rootPOA
     PortableServer::POA_var rootPOA =
           PortableServer::POA::_narrow(
           orb->resolve_initial_references("RootPOA"));

     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();

     cout << myPOA->servant_to_reference(&managerServant) <<
           "is ready" << endl;

     // Wait for incoming requests
     orb->run();
  } catch(const CORBA::Exception& e) {
     cerr << e << endl;
     return 1;
  }
  return 0;
}

コードサンプル4-5 Server.javaプログラム(Java)

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"));
        // 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();
        System.out.println(
               myPOA.servant_to_reference(managerServant) +
                                              "is ready.");
        // Wait for incoming requests
        orb.run();
     } catch (Exception e) {
        e.printStackTrace();
     }
  }
}