summaryrefslogtreecommitdiff
path: root/TAO/tao/Utils/ORB_Manager.cpp
blob: 4ab7af28e562430ef5ae26d11ab78828fd227abf (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// $Id$

// ============================================================================
//
// = LIBRARY
//    tao
//
// = AUTHOR
//    Sumedh Mungee <sumedh@cs.wustl.edu>
//
// ============================================================================

#include "tao/Utils/ORB_Manager.h"
#include "tao/Utils/PolicyList_Destroyer.h"

#include "tao/PortableServer/POAManagerC.h"
#include "tao/PortableServer/IdAssignmentPolicyC.h"
#include "tao/PortableServer/LifespanPolicyC.h"


#include "tao/ORBInitializer_Registry.h"

#include "ace/Log_Msg.h"

ACE_RCSID (PortableServer,
           ORB_Manager,
           "$Id$")


TAO_BEGIN_VERSIONED_NAMESPACE_DECL

// constructor
TAO_ORB_Manager::TAO_ORB_Manager (CORBA::ORB_ptr orb,
                                  PortableServer::POA_ptr poa,
                                  PortableServer::POAManager_ptr poa_manager)
  : orb_ (CORBA::ORB::_duplicate(orb)),
    poa_ (PortableServer::POA::_duplicate(poa)),
    poa_manager_ (PortableServer::POAManager::_duplicate(poa_manager))
{
}

int
TAO_ORB_Manager::init (int &argc,
                       char **argv,
                       const char *orb_name)
{
  if (CORBA::is_nil (this->orb_.in ()))
    {
      this->orb_ = CORBA::ORB_init (argc, argv, orb_name);
    }

  if (CORBA::is_nil (this->poa_.in ()))
    {
      // Get the POA from the ORB.
      CORBA::Object_var poa_object =
        this->orb_->resolve_initial_references (TAO_OBJID_ROOTPOA);

      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT (" (%P|%t) Unable to initialize the POA.\n")),
                          -1);

      // Get the POA object.
      this->poa_ = PortableServer::POA::_narrow (poa_object.in ());
    }

  if (CORBA::is_nil (this->poa_manager_.in ()))
    {
      // Get the POA_Manager.
      this->poa_manager_ = this->poa_->the_POAManager ();
    }

  return 0;
}

#if !defined (CORBA_E_MICRO)
int
TAO_ORB_Manager::init_child_poa (int& argc,
                                 char **argv,
                                 const char *poa_name,
                                 const char *orb_name)
{
  // Check to see if root poa has to be created.
  if (this->init (argc, argv, orb_name) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT (" (%P|%t) Error in init.\n")),
                      -1);

  // Create the default policies - user-supplied ID, and persistent
  // objects.
  TAO::Utils::PolicyList_Destroyer policies (2);
  policies.length (2);

  // Id Assignment policy
  policies[0] =
    this->poa_->create_id_assignment_policy (PortableServer::USER_ID);

  // Lifespan policy
  policies[1] =
    this->poa_->create_lifespan_policy (PortableServer::PERSISTENT);

  // We use a different POA, otherwise the user would have to change
  // the object key each time it invokes the server.
  this->child_poa_ =
    this->poa_->create_POA (poa_name, this->poa_manager_.in (), policies);

  return 0;
}
#endif /* CORBA_E_MICRO */

// Activate POA manager.

int
TAO_ORB_Manager::activate_poa_manager (void)
{
  this->poa_manager_->activate ();
  return 0;
}

// Activate servant in the POA.

char *
TAO_ORB_Manager::activate (PortableServer::Servant servant)
{
  PortableServer::ObjectId_var id = this->poa_->activate_object (servant);

  CORBA::Object_var obj = this->poa_->id_to_reference (id.in ());

  CORBA::String_var str = this->orb_->object_to_string (obj.in ());

  return str._retn ();
}

void
TAO_ORB_Manager::deactivate (const char *id)
{
  CORBA::Object_var object = this->orb_->string_to_object (id);

  PortableServer::ObjectId_var object_id =
    this->poa_->reference_to_id (object.in ());

  this->poa_->deactivate_object (object_id.in ());
}

#if !defined (CORBA_E_MICRO)
// Activate the object with the object_name under the child POA.
char *
TAO_ORB_Manager::activate_under_child_poa (const char *object_name,
                                           PortableServer::Servant servant)
{
  if (object_name == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("\n(%P|%t) TAO_ORB_Manager::register: ")
                       ACE_TEXT ("object_name is null!")),
                      0);

  PortableServer::ObjectId_var id =
    PortableServer::string_to_ObjectId (object_name);

  this->child_poa_->activate_object_with_id (id.in (), servant);

  CORBA::Object_var obj = this->child_poa_->id_to_reference (id.in ());

  CORBA::String_var str = this->orb_->object_to_string (obj.in ());

  return str._retn();
}

void
TAO_ORB_Manager::deactivate_under_child_poa (const char *id)
{
  CORBA::Object_var object = this->orb_->string_to_object (id);

  PortableServer::ObjectId_var object_id =
    this->child_poa_->reference_to_id (object.in ());

  this->child_poa_->deactivate_object (object_id.in ());
}
#endif /* CORBA_E_MICRO */

// Enter the ORB event loop.

int
TAO_ORB_Manager::run (ACE_Time_Value &tv)
{
  this->poa_manager_->activate ();

  this->orb_->run (tv);

  return 0;
}

int
TAO_ORB_Manager::fini (void)
{
  this->poa_->destroy (1, 1);

  this->child_poa_   = PortableServer::POA::_nil();
  this->poa_         = PortableServer::POA::_nil();
  this->poa_manager_ = PortableServer::POAManager::_nil();

  this->orb_->destroy ();

  this->orb_ = CORBA::ORB::_nil();

  return 0;
}

int
TAO_ORB_Manager::run (void)
{
  this->poa_manager_->activate ();

  this->orb_->run ();

  return 0;
}

// Return the corba orb reference.

CORBA::ORB_ptr
TAO_ORB_Manager::orb (void)
{
  return CORBA::ORB::_duplicate (this->orb_.in ());
}

// Return the root POA reference
PortableServer::POA_ptr
TAO_ORB_Manager::root_poa (void)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

// Return the child POA reference
PortableServer::POA_ptr
TAO_ORB_Manager::child_poa (void)
{
  return PortableServer::POA::_duplicate (this->child_poa_.in ());
}

PortableServer::POAManager_ptr
TAO_ORB_Manager::poa_manager (void)
{
  return PortableServer::POAManager::_duplicate (this->poa_manager_.in ());
}

// Destructor.

TAO_ORB_Manager::~TAO_ORB_Manager (void)
{
  try
    {
      if (!CORBA::is_nil (this->poa_.in ()))
        {
          this->poa_->destroy (1,1);
        }
      if (!CORBA::is_nil (this->orb_.in ()))
        {
          this->orb_->destroy ();
        }
    }
  catch (const ::CORBA::Exception&)
    {
      // ignore any exceptions..
    }
}

TAO_END_VERSIONED_NAMESPACE_DECL