ここでは,この章で説明したコード全体を示します。コードサンプル6-16は,この章で説明したC++のコード全体を示します。コードサンプル6-17は,この章で説明したJavaのコード全体を示します。
// Server.C
#include "Bank_s.hh"
#include <math.h>
class Dictionary {
private:
struct Data {
const char* name;
void* value;
};
unsigned _count;
Data* _data;
public:
Dictionary() {
_count = 0;
}
void put(const char* name, void* value) {
Data* oldData = _data;
_data = new Data[_count +1];
for(unsigned i = 0; i < _count; i++) {
_data[i] = oldData[i];
}
_data[_count].name = strdup(name);
_data[_count].value = value;
_count++;
}
void* get(const char* name) {
for(unsigned i =0; i < _count; i++) {
if(!strcmp(name, _data[i].name)) {
return _data[i].value;
}
}
return 0;
}
};
class AccountImpl : public POA_Bank::Account {
private:
float _balance;
public:
AccountImpl(float balance) {
_balance = balance;
}
virtual float balance() {
return _balance;
}
};
class AccountManagerImpl : public POA_Bank::AccountManager {
private:
Dictionary _accounts;
public:
virtual Bank::Account_ptr open(const char* name) {
// Lookup the account in the account dictionary.
Bank::Account_ptr account =
(Bank::Account_ptr) _accounts.get(name);
if(account == Bank::Account::_nil()) {
// Make up the account's balance, between
// 0 and 1000 dollars.
float balance = abs(rand()) % 100000 / 100.0;
// Create the account implementation, given
// the balance.
AccountImpl *accountServant =
new AccountImpl(balance);
try {
// Activate it on the default POA which is
// rootPOA for this servant
PortableServer::POA_var rootPOA = _default_POA();
CORBA::Object_var obj =
rootPOA->servant_to_reference(accountServant);
account = Bank::Account::_narrow(obj);
} catch(const CORBA::Exception& e) {
cerr << "_narrow caught exception: " << e
<< endl;
}
//Print out the new account.
cout << "Created " << name << "'s account: "
<< account << endl;
// Save the account in the account dictionary.
_accounts.put(name, account);
}
// Return the account.
return Bank::Account::_duplicate(account);
}
};
int main(int argc, char* const* argv) {
try {
// Initialize the ORB.
CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
// get a reference to the rootPOA
CORBA::Object_var obj =
orb->resolve_initial_references("RootPOA");
// narrow the object reference to a POA reference
PortableServer::POA_var rootPOA =
PortableServer::POA::_narrow(obj);
CORBA::PolicyList policies;
policies.length(1);
policies[(CORBA::ULong)0] =
rootPOA->create_lifespan_policy(
PortableServer::PERSISTENT
);
// Create myPOA with the right policies
PortableServer::POAManager_var rootManager =
rootPOA->the_POAManager();
PortableServer::POA_var myPOA =
rootPOA->create_POA( "bank_agent_poa",
rootManager,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
rootPOA->the_POAManager()->activate();
cout << myPOA->servant_to_reference(&managerServant)
<< " is ready" << endl;
// Wait for incoming requests
orb->run();
} catch(const CORBA::Exception& e) {
cerr << e << endl;
}
}
// Server.java
import org.omg.PortableServer.*;
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();
}
}
}