2 //=============================================================================
6 * This program demonstrates and tests how to move between id,
7 * reference, and servants.
9 * @author Irfan Pyarali
11 //=============================================================================
15 #include "tao/ORB_Core.h"
16 #include "tao/Server_Strategy_Factory.h"
18 class test_i
: public POA_test
21 test_i (PortableServer::POA_ptr poa
)
22 : poa_ (PortableServer::POA::_duplicate (poa
))
26 PortableServer::POA_ptr
_default_POA ()
28 return PortableServer::POA::_duplicate (this->poa_
.in ());
32 PortableServer::POA_var poa_
;
36 create_poas (PortableServer::POA_ptr root_poa
,
37 PortableServer::LifespanPolicyValue lifespan_policy
,
38 PortableServer::POA_out first_poa
,
39 PortableServer::POA_out second_poa
,
40 PortableServer::POA_out third_poa
,
41 PortableServer::POA_out forth_poa
)
43 // Policies for the new POAs
44 CORBA::PolicyList
policies (3);
47 policies
[0] = root_poa
->create_lifespan_policy (lifespan_policy
);
49 policies
[1] = root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
51 policies
[2] = root_poa
->create_id_assignment_policy (PortableServer::SYSTEM_ID
);
53 // Creation of the firstPOA
54 ACE_CString name
= "firstPOA";
55 first_poa
= root_poa
->create_POA (name
.c_str (),
56 PortableServer::POAManager::_nil (),
59 policies
[1]->destroy ();
61 policies
[1] = root_poa
->create_id_uniqueness_policy (PortableServer::UNIQUE_ID
);
63 // Creation of the secondPOA
65 second_poa
= root_poa
->create_POA (name
.c_str (),
66 PortableServer::POAManager::_nil (),
69 policies
[2]->destroy ();
71 policies
[2] = root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
73 // Creation of the thirdPOA
75 third_poa
= root_poa
->create_POA (name
.c_str (),
76 PortableServer::POAManager::_nil (),
79 policies
[1]->destroy ();
81 policies
[1] = root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
83 // Creation of the forthPOA
85 forth_poa
= root_poa
->create_POA (name
.c_str (),
86 PortableServer::POAManager::_nil (),
89 // Creation of the new POAs over, so destroy the policies
90 for (CORBA::ULong i
= 0;
91 i
< policies
.length ();
94 policies
[i
]->destroy ();
99 test_poas (CORBA::ORB_ptr orb
,
100 PortableServer::POA_ptr root_poa
,
101 PortableServer::POA_ptr first_poa
,
102 PortableServer::POA_ptr second_poa
,
103 PortableServer::POA_ptr third_poa
,
104 PortableServer::POA_ptr forth_poa
,
105 int perform_deactivation_test
)
108 test_i
servant (root_poa
);
110 CORBA::Object_var obj
= root_poa
->create_reference ("IDL:test:1.0");
112 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
114 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
116 PortableServer::ObjectId_var id
= root_poa
->reference_to_id (obj
.in ());
118 root_poa
->activate_object_with_id (id
.in (),
121 obj
= root_poa
->id_to_reference (id
.in ());
123 string
= orb
->object_to_string (obj
.in ());
125 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
127 PortableServer::ServantBase_var servant_from_reference
=
128 root_poa
->reference_to_servant (obj
.in ());
130 PortableServer::ServantBase_var servant_from_id
=
131 root_poa
->id_to_servant (id
.in ());
133 if (servant_from_reference
.in () != servant_from_id
.in ()
134 || servant_from_reference
.in () != &servant
)
136 ACE_ERROR ((LM_ERROR
,
137 "Mismatched servant_from_reference, "
138 "servant_from_id and &servant\n"));
141 obj
= root_poa
->servant_to_reference (&servant
);
143 string
= orb
->object_to_string (obj
.in ());
145 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
147 obj
= servant
._this ();
149 string
= orb
->object_to_string (obj
.in ());
151 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
153 PortableServer::ObjectId_var id_from_servant
= root_poa
->servant_to_id (&servant
);
155 ACE_ASSERT (id_from_servant
.in () == id
.in ());
157 root_poa
->deactivate_object (id
.in ());
159 if (perform_deactivation_test
)
161 root_poa
->activate_object_with_id (id
.in (),
164 servant_from_reference
= root_poa
->reference_to_servant (obj
.in ());
166 ACE_ASSERT (servant_from_reference
.in () == &servant
);
168 root_poa
->deactivate_object (id
.in ());
173 test_i
servant (root_poa
);
175 PortableServer::ObjectId_var id
= root_poa
->activate_object (&servant
);
177 CORBA::Object_var obj
= root_poa
->id_to_reference (id
.in ());
179 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
181 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
183 obj
= root_poa
->create_reference_with_id (id
.in (),
186 string
= orb
->object_to_string (obj
.in ());
188 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
190 root_poa
->deactivate_object (id
.in ());
194 test_i
servant (first_poa
);
196 CORBA::Object_var obj
= first_poa
->create_reference ("IDL:test:1.0");
198 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
200 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
202 PortableServer::ObjectId_var id
= first_poa
->reference_to_id (obj
.in ());
204 first_poa
->activate_object_with_id (id
.in (),
207 obj
= first_poa
->id_to_reference (id
.in ());
209 string
= orb
->object_to_string (obj
.in ());
211 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
213 PortableServer::ServantBase_var servant_from_reference
=
214 first_poa
->reference_to_servant (obj
.in ());
216 PortableServer::ServantBase_var servant_from_id
=
217 first_poa
->id_to_servant (id
.in ());
219 if (servant_from_reference
.in () != servant_from_id
.in ()
220 || servant_from_reference
.in () != &servant
)
222 ACE_ERROR ((LM_ERROR
,
223 "Mismatched servant_from_reference, "
224 "servant_from_id and &servant\n"));
227 first_poa
->deactivate_object (id
.in ());
229 if (perform_deactivation_test
)
231 first_poa
->activate_object_with_id (id
.in (),
234 servant_from_reference
= first_poa
->reference_to_servant (obj
.in ());
236 ACE_ASSERT (servant_from_reference
.in () == &servant
);
238 first_poa
->deactivate_object (id
.in ());
243 test_i
servant (first_poa
);
245 PortableServer::ObjectId_var id
= first_poa
->activate_object (&servant
);
247 CORBA::Object_var obj
= first_poa
->id_to_reference (id
.in ());
249 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
251 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
253 obj
= first_poa
->create_reference_with_id (id
.in (),
256 string
= orb
->object_to_string (obj
.in ());
258 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
260 first_poa
->deactivate_object (id
.in ());
264 test_i
servant (second_poa
);
266 CORBA::Object_var obj
= second_poa
->create_reference ("IDL:test:1.0");
268 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
270 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
272 PortableServer::ObjectId_var id
= second_poa
->reference_to_id (obj
.in ());
274 second_poa
->activate_object_with_id (id
.in (),
277 obj
= second_poa
->id_to_reference (id
.in ());
279 string
= orb
->object_to_string (obj
.in ());
281 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
283 PortableServer::ServantBase_var servant_from_reference
=
284 second_poa
->reference_to_servant (obj
.in ());
286 PortableServer::ServantBase_var servant_from_id
=
287 second_poa
->id_to_servant (id
.in ());
289 if (servant_from_reference
.in () != servant_from_id
.in ()
290 || servant_from_reference
.in () != &servant
)
292 ACE_ERROR ((LM_ERROR
,
293 "Mismatched servant_from_reference, "
294 "servant_from_id and &servant\n"));
297 obj
= second_poa
->servant_to_reference (&servant
);
299 string
= orb
->object_to_string (obj
.in ());
301 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
303 obj
= servant
._this ();
305 string
= orb
->object_to_string (obj
.in ());
307 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
309 PortableServer::ObjectId_var id_from_servant
= second_poa
->servant_to_id (&servant
);
311 ACE_ASSERT (id_from_servant
.in () == id
.in ());
313 second_poa
->deactivate_object (id
.in ());
315 if (perform_deactivation_test
)
317 second_poa
->activate_object_with_id (id
.in (),
320 servant_from_reference
= second_poa
->reference_to_servant (obj
.in ());
322 ACE_ASSERT (servant_from_reference
.in () == &servant
);
324 second_poa
->deactivate_object (id
.in ());
329 test_i
servant (second_poa
);
331 PortableServer::ObjectId_var id
= second_poa
->activate_object (&servant
);
333 CORBA::Object_var obj
= second_poa
->id_to_reference (id
.in ());
335 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
337 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
339 obj
= second_poa
->create_reference_with_id (id
.in (),
342 string
= orb
->object_to_string (obj
.in ());
344 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
346 second_poa
->deactivate_object (id
.in ());
350 test_i
servant (third_poa
);
351 PortableServer::ObjectId_var id
= PortableServer::string_to_ObjectId ("hello");
353 CORBA::Object_var obj
= third_poa
->create_reference_with_id (id
.in (),
356 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
358 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
360 PortableServer::ObjectId_var id_from_reference
= third_poa
->reference_to_id (obj
.in ());
362 string
= PortableServer::ObjectId_to_string (id_from_reference
.in ());
363 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
365 ACE_ASSERT (id_from_reference
.in () == id
.in ());
367 third_poa
->activate_object_with_id (id
.in (),
370 obj
= third_poa
->id_to_reference (id
.in ());
372 string
= orb
->object_to_string (obj
.in ());
374 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
376 PortableServer::ServantBase_var servant_from_reference
=
377 third_poa
->reference_to_servant (obj
.in ());
379 PortableServer::ServantBase_var servant_from_id
=
380 third_poa
->id_to_servant (id
.in ());
382 if (servant_from_reference
.in () != servant_from_id
.in ()
383 || servant_from_reference
.in () != &servant
)
385 ACE_ERROR ((LM_ERROR
,
386 "Mismatched servant_from_reference, "
387 "servant_from_id and &servant\n"));
390 obj
= third_poa
->servant_to_reference (&servant
);
392 string
= orb
->object_to_string (obj
.in ());
394 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
396 obj
= servant
._this ();
398 string
= orb
->object_to_string (obj
.in ());
400 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
402 PortableServer::ObjectId_var id_from_servant
= third_poa
->servant_to_id (&servant
);
404 string
= PortableServer::ObjectId_to_string (id_from_servant
.in ());
405 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
407 ACE_ASSERT (id_from_servant
.in () == id
.in ());
409 third_poa
->deactivate_object (id
.in ());
411 if (perform_deactivation_test
)
413 third_poa
->activate_object_with_id (id
.in (),
416 servant_from_reference
= third_poa
->reference_to_servant (obj
.in ());
418 ACE_ASSERT (servant_from_reference
.in () == &servant
);
420 third_poa
->deactivate_object (id
.in ());
425 test_i
servant (forth_poa
);
426 PortableServer::ObjectId_var id
= PortableServer::string_to_ObjectId ("hello");
428 CORBA::Object_var obj
= forth_poa
->create_reference_with_id (id
.in (),
431 CORBA::String_var string
= orb
->object_to_string (obj
.in ());
433 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
435 PortableServer::ObjectId_var id_from_reference
= forth_poa
->reference_to_id (obj
.in ());
437 string
= PortableServer::ObjectId_to_string (id_from_reference
.in ());
438 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
440 ACE_ASSERT (id_from_reference
.in () == id
.in ());
442 forth_poa
->activate_object_with_id (id
.in (),
445 obj
= forth_poa
->id_to_reference (id
.in ());
447 string
= orb
->object_to_string (obj
.in ());
449 ACE_DEBUG ((LM_DEBUG
, "%C\n", string
.in ()));
451 PortableServer::ServantBase_var servant_from_reference
=
452 forth_poa
->reference_to_servant (obj
.in ());
454 PortableServer::ServantBase_var servant_from_id
=
455 forth_poa
->id_to_servant (id
.in ());
457 if (servant_from_reference
.in () != servant_from_id
.in ()
458 || servant_from_reference
.in () != &servant
)
460 ACE_ERROR ((LM_ERROR
,
461 "Mismatched servant_from_reference, "
462 "servant_from_id and &servant\n"));
465 forth_poa
->deactivate_object (id
.in ());
467 if (perform_deactivation_test
)
469 forth_poa
->activate_object_with_id (id
.in (),
472 servant_from_reference
= forth_poa
->reference_to_servant (obj
.in ());
474 ACE_ASSERT (servant_from_reference
.in () == &servant
);
476 forth_poa
->deactivate_object (id
.in ());
482 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
486 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
488 // Obtain the RootPOA.
489 CORBA::Object_var obj
=
490 orb
->resolve_initial_references ("RootPOA");
492 PortableServer::POA_var root_poa
= PortableServer::POA::_narrow (obj
.in ());
494 const TAO_Server_Strategy_Factory::Active_Object_Map_Creation_Parameters
&creation_parameters
495 = TAO_ORB_Core_instance ()->server_factory ()->active_object_map_creation_parameters ();
497 int perform_deactivation_test
= creation_parameters
.allow_reactivation_of_system_ids_
;
499 PortableServer::POA_var first_poa
;
500 PortableServer::POA_var second_poa
;
501 PortableServer::POA_var third_poa
;
502 PortableServer::POA_var forth_poa
;
504 create_poas (root_poa
.in (),
505 PortableServer::TRANSIENT
,
511 test_poas (orb
.in (),
517 perform_deactivation_test
);
519 first_poa
->destroy (1,
522 second_poa
->destroy (1,
525 third_poa
->destroy (1,
528 forth_poa
->destroy (1,
531 create_poas (root_poa
.in (),
532 PortableServer::PERSISTENT
,
538 test_poas (orb
.in (),
544 perform_deactivation_test
);
546 root_poa
->destroy (true, true);
550 catch (const CORBA::Exception
& ex
)
552 ex
._tao_print_exception ("Exception!");