2 //=============================================================================
6 * In this example of using Persistent IDs.
8 * @author Irfan Pyarali
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/SString.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "tao/PortableServer/PortableServer.h"
20 public virtual POA_test
23 test_i (CORBA::ORB_ptr orb_ptr
,
24 PortableServer::POA_ptr poa
);
28 //FUZZ: disable check_for_lack_ACE_OS
29 ///FUZZ: enable check_for_lack_ACE_OS
32 test_ptr
create_POA ();
36 PortableServer::POA_ptr
_default_POA ();
40 PortableServer::POA_var poa_
;
41 PortableServer::POA_var child_poa_
;
42 PortableServer::ObjectId_var oid_
;
45 test_i::test_i (CORBA::ORB_ptr orb
,
46 PortableServer::POA_ptr poa
)
47 : orb_ (CORBA::ORB::_duplicate (orb
)),
48 poa_ (PortableServer::POA::_duplicate (poa
))
55 CORBA::Object_var obj
=
56 this->orb_
->resolve_initial_references ("POACurrent");
58 PortableServer::Current_var current
=
59 PortableServer::Current::_narrow (obj
.in ());
61 PortableServer::POA_var poa
=
64 CORBA::String_var poa_name
=
68 "Method invoked on servant in POA = %C\n",
75 this->orb_
->shutdown (false);
78 PortableServer::POA_ptr
79 test_i::_default_POA ()
81 return PortableServer::POA::_duplicate (this->poa_
.in ());
87 CORBA::PolicyList
policies (2);
91 this->poa_
->create_id_assignment_policy
92 (this->oid_
.ptr () == 0 ? PortableServer::SYSTEM_ID
:
93 PortableServer::USER_ID
);
96 this->poa_
->create_lifespan_policy (PortableServer::PERSISTENT
);
98 PortableServer::POAManager_var poa_manager
=
99 this->poa_
->the_POAManager ();
101 ACE_CString name
= "childPOA";
103 this->poa_
->create_POA (name
.c_str (),
107 // Destroy the policies
108 for (CORBA::ULong i
= 0;
109 i
< policies
.length ();
112 policies
[i
]->destroy ();
116 new test_i (this->orb_
.in (),
117 this->child_poa_
.in ());
119 PortableServer::ServantBase_var
safe_servant (servant
);
121 if (this->oid_
.ptr () == 0)
124 this->child_poa_
->activate_object (servant
);
128 this->child_poa_
->activate_object_with_id (this->oid_
.in (),
132 CORBA::Object_var object
= this->child_poa_
->id_to_reference (this->oid_
.in ());
134 test_var test
= test::_narrow (object
.in ());
136 return test
._retn ();
140 test_i::destroy_POA ()
142 this->child_poa_
->destroy (1, 0);
145 const ACE_TCHAR
*ior_file
= ACE_TEXT ("ior");
148 parse_args (int argc
, ACE_TCHAR
**argv
)
150 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
153 while ((c
= get_opts ()) != -1)
157 ior_file
= get_opts
.opt_arg ();
162 ACE_ERROR_RETURN ((LM_ERROR
,
174 write_ior_to_file (const char *ior
)
177 ACE_OS::fopen (ior_file
, "w");
179 if (output_file
== 0)
180 ACE_ERROR_RETURN ((LM_ERROR
, "Cannot open output files for writing IOR: %s\n",
186 result
= ACE_OS::fprintf (output_file
,
189 if (result
!= ACE_OS::strlen (ior
))
190 ACE_ERROR_RETURN ((LM_ERROR
,
191 "ACE_OS::fprintf failed while writing %C to %s\n",
196 ACE_OS::fclose (output_file
);
202 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
207 CORBA::ORB_init (argc
, argv
);
209 int result
= parse_args (argc
, argv
);
213 CORBA::Object_var obj
=
214 orb
->resolve_initial_references ("RootPOA");
216 PortableServer::POA_var root_poa
=
217 PortableServer::POA::_narrow (obj
.in ());
219 PortableServer::POAManager_var poa_manager
=
220 root_poa
->the_POAManager ();
222 test_i
servant (orb
.in (),
225 PortableServer::ObjectId_var id
=
226 root_poa
->activate_object (&servant
);
228 CORBA::Object_var object
= root_poa
->id_to_reference (id
.in ());
231 test::_narrow (object
.in ());
233 CORBA::String_var ior
=
234 orb
->object_to_string (test
.in ());
237 write_ior_to_file (ior
.in ());
238 if (write_result
!= 0)
241 poa_manager
->activate ();
247 catch (const CORBA::Exception
& ex
)
249 ex
._tao_print_exception ("Exception caught");