Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Persistent_ID / server.cpp
blob06ba954cf45bef180698ba5b30fa0b6db6134164
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * In this example of using Persistent IDs.
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/SString.h"
15 #include "testS.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "tao/PortableServer/PortableServer.h"
19 class test_i :
20 public virtual POA_test
22 public:
23 test_i (CORBA::ORB_ptr orb_ptr,
24 PortableServer::POA_ptr poa);
26 void method ();
28 //FUZZ: disable check_for_lack_ACE_OS
29 ///FUZZ: enable check_for_lack_ACE_OS
30 void shutdown ();
32 test_ptr create_POA ();
34 void destroy_POA ();
36 PortableServer::POA_ptr _default_POA ();
38 protected:
39 CORBA::ORB_var orb_;
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))
52 void
53 test_i::method ()
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 =
62 current->get_POA ();
64 CORBA::String_var poa_name =
65 poa->the_name ();
67 ACE_DEBUG ((LM_DEBUG,
68 "Method invoked on servant in POA = %C\n",
69 poa_name.in ()));
72 void
73 test_i::shutdown ()
75 this->orb_->shutdown (false);
78 PortableServer::POA_ptr
79 test_i::_default_POA ()
81 return PortableServer::POA::_duplicate (this->poa_.in ());
84 test_ptr
85 test_i::create_POA ()
87 CORBA::PolicyList policies (2);
88 policies.length (2);
90 policies[0] =
91 this->poa_->create_id_assignment_policy
92 (this->oid_.ptr () == 0 ? PortableServer::SYSTEM_ID :
93 PortableServer::USER_ID);
95 policies[1] =
96 this->poa_->create_lifespan_policy (PortableServer::PERSISTENT);
98 PortableServer::POAManager_var poa_manager =
99 this->poa_->the_POAManager ();
101 ACE_CString name = "childPOA";
102 this->child_poa_ =
103 this->poa_->create_POA (name.c_str (),
104 poa_manager.in (),
105 policies);
107 // Destroy the policies
108 for (CORBA::ULong i = 0;
109 i < policies.length ();
110 ++i)
112 policies[i]->destroy ();
115 test_i *servant =
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)
123 this->oid_ =
124 this->child_poa_->activate_object (servant);
126 else
128 this->child_poa_->activate_object_with_id (this->oid_.in (),
129 servant);
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 ();
139 void
140 test_i::destroy_POA ()
142 this->child_poa_->destroy (1, 0);
145 const ACE_TCHAR *ior_file = ACE_TEXT ("ior");
147 static int
148 parse_args (int argc, ACE_TCHAR **argv)
150 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
151 int c;
153 while ((c = get_opts ()) != -1)
154 switch (c)
156 case 'f':
157 ior_file = get_opts.opt_arg ();
158 break;
160 case '?':
161 default:
162 ACE_ERROR_RETURN ((LM_ERROR,
163 "usage: %s "
164 "-f ior_file"
165 "\n",
166 argv [0]),
167 -1);
170 return 0;
173 static int
174 write_ior_to_file (const char *ior)
176 FILE *output_file =
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",
181 ior_file),
182 -1);
184 u_int result = 0;
186 result = ACE_OS::fprintf (output_file,
187 "%s",
188 ior);
189 if (result != ACE_OS::strlen (ior))
190 ACE_ERROR_RETURN ((LM_ERROR,
191 "ACE_OS::fprintf failed while writing %C to %s\n",
192 ior,
193 ior_file),
194 -1);
196 ACE_OS::fclose (output_file);
198 return 0;
202 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
206 CORBA::ORB_var orb =
207 CORBA::ORB_init (argc, argv);
209 int result = parse_args (argc, argv);
210 if (result != 0)
211 return result;
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 (),
223 root_poa.in ());
225 PortableServer::ObjectId_var id =
226 root_poa->activate_object (&servant);
228 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
230 test_var test =
231 test::_narrow (object.in ());
233 CORBA::String_var ior =
234 orb->object_to_string (test.in ());
236 int write_result =
237 write_ior_to_file (ior.in ());
238 if (write_result != 0)
239 return write_result;
241 poa_manager->activate ();
243 orb->run ();
245 orb->destroy ();
247 catch (const CORBA::Exception& ex)
249 ex._tao_print_exception ("Exception caught");
250 return -1;
253 return 0;