2 //=============================================================================
6 * This example is very similar to the Explicit_Activation example
7 * except that the POAs are deleted once the object references have
8 * been created. After this, an adapter activator is install in the
9 * RootPOA to reactivate the POAs on demand.
11 * Similar to the Explicit_Activation, the client for this example
14 * @author Irfan Pyarali
16 //=============================================================================
19 #include "ace/Get_Opt.h"
21 #include "ace/OS_NS_stdio.h"
22 #include "ace/OS_NS_string.h"
23 #include "tao/PortableServer/AdapterActivatorC.h"
25 // This is to remove "inherits via dominance" warnings from MSVC.
26 // MSVC is being a little too paranoid.
27 #if defined (_MSC_VER)
28 # pragma warning (disable : 4250)
31 class reference_counted_test_i
:
35 /// Constructor - takes a POA and a value parameter
36 reference_counted_test_i (CORBA::ORB_ptr orb
,
37 PortableServer::POA_ptr poa
);
40 reference_counted_test_i::reference_counted_test_i (CORBA::ORB_ptr orb
,
41 PortableServer::POA_ptr poa
)
47 class Adapter_Activator
: public PortableServer::AdapterActivator
50 Adapter_Activator (PortableServer::POAManager_ptr poa_manager
,
53 CORBA::Boolean
unknown_adapter (PortableServer::POA_ptr parent
,
56 CORBA::PolicyList first_poa_policies_
;
57 CORBA::PolicyList second_poa_policies_
;
60 PortableServer::POAManager_var poa_manager_
;
64 Adapter_Activator::Adapter_Activator (PortableServer::POAManager_ptr poa_manager
,
66 : poa_manager_ (PortableServer::POAManager::_duplicate (poa_manager
)),
67 orb_ (CORBA::ORB::_duplicate (orb
))
72 Adapter_Activator::unknown_adapter (PortableServer::POA_ptr parent
,
75 if (ACE_OS::strcmp (name
, "firstPOA") == 0)
77 PortableServer::POA_var child
= parent
->create_POA (name
,
78 this->poa_manager_
.in (),
79 this->first_poa_policies_
);
81 // Creation of firstPOA is over. Destroy the Policy objects.
82 for (CORBA::ULong i
= 0;
83 i
< this->first_poa_policies_
.length ();
86 this->first_poa_policies_
[i
]->destroy ();
89 child
->the_activator (this);
91 reference_counted_test_i
*servant
=
92 new reference_counted_test_i (this->orb_
.in (),
95 child
->set_servant (servant
);
97 // This means that the ownership of <servant> now belongs to the
99 servant
->_remove_ref ();
101 // Finally everything is fine
104 else if (ACE_OS::strcmp (name
, "secondPOA") == 0)
106 PortableServer::POA_var child
= parent
->create_POA (name
,
107 this->poa_manager_
.in (),
108 this->second_poa_policies_
);
110 // Creation of secondPOA is over. Destroy the Policy objects.
111 for (CORBA::ULong i
= 0;
112 i
< this->second_poa_policies_
.length ();
115 this->second_poa_policies_
[i
]->destroy ();
118 reference_counted_test_i
*servant
=
119 new reference_counted_test_i (this->orb_
.in (),
122 PortableServer::ObjectId_var oid
=
123 PortableServer::string_to_ObjectId ("third test");
125 child
->activate_object_with_id (oid
.in (),
128 // This means that the ownership of <servant> now belongs to the
130 servant
->_remove_ref ();
132 // Finally everything is fine
142 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("ior");
145 parse_args (int argc
, ACE_TCHAR
**argv
)
147 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
150 while ((c
= get_opts ()) != -1)
154 ior_output_file
= get_opts
.opt_arg ();
159 ACE_ERROR_RETURN ((LM_ERROR
,
161 "[-f ior_output_file]"
167 // Indicates successful parsing of command line.
172 write_iors_to_file (const char *first_ior
,
173 const char *second_ior
,
174 const char *third_ior
)
176 char ior_output_file_1
[BUFSIZ
];
177 char ior_output_file_2
[BUFSIZ
];
178 char ior_output_file_3
[BUFSIZ
];
180 ACE_OS::sprintf (ior_output_file_1
, "%s_1", ACE_TEXT_ALWAYS_CHAR(ior_output_file
));
181 ACE_OS::sprintf (ior_output_file_2
, "%s_2", ACE_TEXT_ALWAYS_CHAR(ior_output_file
));
182 ACE_OS::sprintf (ior_output_file_3
, "%s_3", ACE_TEXT_ALWAYS_CHAR(ior_output_file
));
184 FILE *output_file_1
= ACE_OS::fopen (ior_output_file_1
, ACE_TEXT("w"));
185 FILE *output_file_2
= ACE_OS::fopen (ior_output_file_2
, ACE_TEXT("w"));
186 FILE *output_file_3
= ACE_OS::fopen (ior_output_file_3
, ACE_TEXT("w"));
188 if (output_file_1
== 0 ||
189 output_file_2
== 0 ||
191 ACE_ERROR_RETURN ((LM_ERROR
, "Cannot open output files for writing IORs: %C, %C %C\n",
199 result
= ACE_OS::fprintf (output_file_1
,
202 if (result
!= ACE_OS::strlen (first_ior
))
203 ACE_ERROR_RETURN ((LM_ERROR
,
204 "ACE_OS::fprintf failed while writing %C to %s\n",
209 result
= ACE_OS::fprintf (output_file_2
,
212 if (result
!= ACE_OS::strlen (second_ior
))
213 ACE_ERROR_RETURN ((LM_ERROR
,
214 "ACE_OS::fprintf failed while writing %C to %s\n",
219 result
= ACE_OS::fprintf (output_file_3
,
222 if (result
!= ACE_OS::strlen (third_ior
))
223 ACE_ERROR_RETURN ((LM_ERROR
,
224 "ACE_OS::fprintf failed while writing %C to %s\n",
229 ACE_OS::fclose (output_file_1
);
230 ACE_OS::fclose (output_file_2
);
231 ACE_OS::fclose (output_file_3
);
237 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
241 // Initialize the ORB first.
242 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
244 int result
= parse_args (argc
, argv
);
248 // Obtain the RootPOA.
249 CORBA::Object_var obj
=
250 orb
->resolve_initial_references ("RootPOA");
252 // Get the POA_var object from Object_var.
253 PortableServer::POA_var root_poa
=
254 PortableServer::POA::_narrow (obj
.in ());
256 // Get the POAManager of the RootPOA.
257 PortableServer::POAManager_var poa_manager
=
258 root_poa
->the_POAManager ();
260 Adapter_Activator
adapter_activator (poa_manager
.in (),
263 root_poa
->the_activator (&adapter_activator
);
265 PortableServer::POA_var first_poa
;
266 PortableServer::POA_var second_poa
;
269 // Policies for the firstPOA to be created.
270 CORBA::PolicyList
&policies
= adapter_activator
.first_poa_policies_
;
273 // Id Assignment Policy
275 root_poa
->create_id_assignment_policy (PortableServer::SYSTEM_ID
);
279 root_poa
->create_lifespan_policy (PortableServer::PERSISTENT
);
281 // Request Processing policy
283 root_poa
->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT
);
287 root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
289 // Create the firstPOA under the RootPOA.
290 first_poa
= root_poa
->create_POA ("firstPOA",
296 // Policies for the secondPOA to be created.
297 CORBA::PolicyList
&policies
= adapter_activator
.second_poa_policies_
;
300 // Id Assignment Policy
302 root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
306 root_poa
->create_lifespan_policy (PortableServer::PERSISTENT
);
308 // Create the secondPOA under the firstPOA.
309 second_poa
= first_poa
->create_POA ("secondPOA",
315 reference_counted_test_i
first_servant (orb
.in (),
318 PortableServer::ObjectId_var first_oid
=
319 root_poa
->activate_object (&first_servant
);
321 // Get Object Reference for the first_servant object.
322 test_var first_test
= first_servant
._this ();
324 CORBA::Object_var second_test
=
325 first_poa
->create_reference ("IDL:test:1.0");
327 PortableServer::ObjectId_var third_oid
=
328 PortableServer::string_to_ObjectId ("third test");
330 CORBA::Object_var third_test
=
331 second_poa
->create_reference_with_id (third_oid
.in (),
334 // Stringyfy all the object references and print them out.
335 CORBA::String_var first_ior
=
336 orb
->object_to_string (first_test
.in ());
338 CORBA::String_var second_ior
=
339 orb
->object_to_string (second_test
.in ());
341 CORBA::String_var third_ior
=
342 orb
->object_to_string (third_test
.in ());
344 ACE_DEBUG ((LM_DEBUG
,
350 int write_result
= write_iors_to_file (first_ior
.in (),
353 if (write_result
!= 0)
356 first_poa
->destroy (1,
359 poa_manager
->activate ();
365 catch (const CORBA::Exception
& ex
)
367 ex
._tao_print_exception ("Exception caught");