Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Adapter_Activator / server.cpp
blob7fcbd0d07f09abbbd5473537a276e669528105b4
2 //=============================================================================
3 /**
4 * @file server.cpp
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
12 * is Generic_Servant.
14 * @author Irfan Pyarali
16 //=============================================================================
19 #include "ace/Get_Opt.h"
20 #include "test_i.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)
29 #endif /* _MSC_VER */
31 class reference_counted_test_i :
32 public virtual test_i
34 public:
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)
42 : test_i (orb,
43 poa)
47 class Adapter_Activator : public PortableServer::AdapterActivator
49 public:
50 Adapter_Activator (PortableServer::POAManager_ptr poa_manager,
51 CORBA::ORB_ptr orb);
53 CORBA::Boolean unknown_adapter (PortableServer::POA_ptr parent,
54 const char *name);
56 CORBA::PolicyList first_poa_policies_;
57 CORBA::PolicyList second_poa_policies_;
59 private:
60 PortableServer::POAManager_var poa_manager_;
61 CORBA::ORB_var orb_;
64 Adapter_Activator::Adapter_Activator (PortableServer::POAManager_ptr poa_manager,
65 CORBA::ORB_ptr orb)
66 : poa_manager_ (PortableServer::POAManager::_duplicate (poa_manager)),
67 orb_ (CORBA::ORB::_duplicate (orb))
71 CORBA::Boolean
72 Adapter_Activator::unknown_adapter (PortableServer::POA_ptr parent,
73 const char *name)
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 ();
84 ++i)
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 (),
93 child.in ());
95 child->set_servant (servant);
97 // This means that the ownership of <servant> now belongs to the
98 // POA.
99 servant->_remove_ref ();
101 // Finally everything is fine
102 return 1;
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 ();
113 ++i)
115 this->second_poa_policies_[i]->destroy ();
118 reference_counted_test_i *servant =
119 new reference_counted_test_i (this->orb_.in (),
120 child.in ());
122 PortableServer::ObjectId_var oid =
123 PortableServer::string_to_ObjectId ("third test");
125 child->activate_object_with_id (oid.in (),
126 servant);
128 // This means that the ownership of <servant> now belongs to the
129 // POA.
130 servant->_remove_ref ();
132 // Finally everything is fine
133 return 1;
135 else
137 // Unknown POA.
138 return 0;
142 const ACE_TCHAR *ior_output_file = ACE_TEXT ("ior");
144 static int
145 parse_args (int argc, ACE_TCHAR **argv)
147 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
148 int c;
150 while ((c = get_opts ()) != -1)
151 switch (c)
153 case 'f':
154 ior_output_file = get_opts.opt_arg ();
155 break;
157 case '?':
158 default:
159 ACE_ERROR_RETURN ((LM_ERROR,
160 "usage: %s "
161 "[-f ior_output_file]"
162 "\n",
163 argv [0]),
164 -1);
167 // Indicates successful parsing of command line.
168 return 0;
171 static int
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 ||
190 output_file_3 == 0)
191 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IORs: %C, %C %C\n",
192 ior_output_file_1,
193 ior_output_file_2,
194 ior_output_file_3),
195 -1);
197 u_int result = 0;
199 result = ACE_OS::fprintf (output_file_1,
200 "%s",
201 first_ior);
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",
205 first_ior,
206 ior_output_file_1),
207 -1);
209 result = ACE_OS::fprintf (output_file_2,
210 "%s",
211 second_ior);
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",
215 second_ior,
216 ior_output_file_2),
217 -1);
219 result = ACE_OS::fprintf (output_file_3,
220 "%s",
221 third_ior);
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",
225 third_ior,
226 ior_output_file_3),
227 -1);
229 ACE_OS::fclose (output_file_1);
230 ACE_OS::fclose (output_file_2);
231 ACE_OS::fclose (output_file_3);
233 return 0;
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);
245 if (result != 0)
246 return result;
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 (),
261 orb.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_;
271 policies.length (4);
273 // Id Assignment Policy
274 policies[0] =
275 root_poa->create_id_assignment_policy (PortableServer::SYSTEM_ID);
277 // Lifespan policy
278 policies[1] =
279 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
281 // Request Processing policy
282 policies[2] =
283 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
285 // Id Uniqueness
286 policies[3] =
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",
291 poa_manager.in (),
292 policies);
296 // Policies for the secondPOA to be created.
297 CORBA::PolicyList &policies = adapter_activator.second_poa_policies_;
298 policies.length (2);
300 // Id Assignment Policy
301 policies[0] =
302 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
304 // Lifespan policy
305 policies[1] =
306 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
308 // Create the secondPOA under the firstPOA.
309 second_poa = first_poa->create_POA ("secondPOA",
310 poa_manager.in (),
311 policies);
314 // Create a servant.
315 reference_counted_test_i first_servant (orb.in (),
316 root_poa.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 (),
332 "IDL:test:1.0");
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,
345 "%C\n%C\n%C\n",
346 first_ior.in (),
347 second_ior.in (),
348 third_ior.in ()));
350 int write_result = write_iors_to_file (first_ior.in (),
351 second_ior.in (),
352 third_ior.in ());
353 if (write_result != 0)
354 return write_result;
356 first_poa->destroy (1,
359 poa_manager->activate ();
361 orb->run ();
363 orb->destroy ();
365 catch (const CORBA::Exception& ex)
367 ex._tao_print_exception ("Exception caught");
368 return -1;
371 return 0;