summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/LoadBalancing/HashReplicaFactory.cpp
blob: 15063062c9bac5691dfbc323951961f28bab2ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// -*- C++ -*-
//
// $Id$

#include "HashReplicaFactory.h"

ACE_RCSID (LB_test, HashReplicaFactory, "$Id$")

CORBA::Object_ptr
HashReplicaFactory::create_object (
    const char *type_id,
    const TAO_LoadBalancing::Criteria &the_criteria,
    TAO_LoadBalancing::GenericFactory::FactoryCreationId_out
      factory_creation_id,
    ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   TAO_LoadBalancing::NoFactory,
                   TAO_LoadBalancing::ObjectNotCreated,
                   TAO_LoadBalancing::InvalidCriteria,
                   TAO_LoadBalancing::InvalidProperty,
                   TAO_LoadBalancing::CannotMeetCriteria))
{
  // This factory is only designed to create HashReplica objects.
  // However, a GenericFactory need not be limited to being able to
  // only create one type of object.

  // Since this factory is only capable of creating HashReplica
  // objects, throw an exception if the type_id does not match that of
  // the HashReplica.
  if (ACE_OS::strcmp (type_id,
                      this->replica_type_id_) != 0)
    ACE_THROW_RETURN (TAO_LoadBalancing::NoFactory (the_location,
                                                    type_id),
                      CORBA::Object::_nil ());

  this->parse_criteria (criteria,
                        ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::Object::_nil ());

  HashReplica_i *hash_replica_servant = 0;
  ACE_NEW_THROW_EX (hash_replica_servant,
                    HashReplica_i,
                    CORBA::NO_MEMORY ());
  ACE_CHECK_RETURN (CORBA::Object::_nil ());

  // Transfer ownership of the servant to the POA (HashReplica_i
  // inherits from PortableServer::RefCountServantBase).
  PortableServer::ServantBase_var tmp = hash_replica_servant;

  // Activate the HashReplica and obtain a reference to it.
  this->hash_replica_ =
    hash_replica_servant->_this (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::Object::_nil ());

  factory_creation_id = ...FILL IN CREATION ID...;

  return HashReplica::_duplicate (this->hash_replica_.in ());
}

void
HashReplicaFactory::delete_object (
    const TAO_LoadBalancing::GenericFactory::FactoryCreationId
      &factory_creation_id,
    ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   TAO_LoadBalancer::ObjectNotFound))
{
  if (factory_creation_id != this->factory_creation_id_)
    ACE_THROW (TAO_LoadBalancer::ObjectNotFound ());

  // Get the POA used when activating the HashReplica
  PortableServer::POA_var poa =
    this->_default_POA (ACE_TRY_ENV);
  ACE_CHECK;

  // Get the object ID associated with the HashReplica reference.
  PortableServer::ObjectId_var oid =
    poa->reference_to_id (this->hash_replica_.in (),
                          ACE_TRY_ENV);
  ACE_CHECK;

  // Now deactivate the HashReplica.
  poa->deactivate_object (oid.in (), ACE_TRY_ENV);
  ACE_CHECK;

  // Decrease the reference count on the HashReplica.
  this->hash_replica->_remove_ref ();
}

void
HashReplicaFactory::parse_criteria (
    const TAO_LoadBalancer::Criteria &criteria,
    CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   TAO_LoadBalancing::InvalidCriteria,
                   TAO_LoadBalancing::InvalidProperty,
                   TAO_LoadBalancing::CannotMeetCriteria))
{
  // We don't use any criteria!

  if (criteria.length () != 0)
    ACE_THROW (TAO_LoadBalancer::CannotMeetCriteria (criteria));
}