Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_2248_Regression / server.cpp
blobb029a16325a9fbfdd70b6e0a54d80ccfa93a7100
1 #include "Server_ORBInitializer.h"
2 #include "Server_Request_Interceptor.h"
3 #include "test_i.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_stdio.h"
6 #include "orbsvcs/FaultTolerance/FT_Service_Activate.h"
7 #include "tao/ORBInitializer_Registry.h"
9 const ACE_TCHAR *ior_output_file = 0;
10 const ACE_TCHAR *merged_iorstr = 0;
11 const ACE_TCHAR *key = 0;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:k:f:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_output_file = get_opts.opt_arg ();
24 break;
25 case 'k':
26 key = get_opts.opt_arg ();
27 break;
28 case 'f':
29 merged_iorstr = get_opts.opt_arg ();
30 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s "
36 "-o <iorfile>"
37 "-k <key> "
38 "-f merged_iorstr"
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
47 int
48 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
52 try
54 Server_ORBInitializer *temp_initializer = 0;
55 ACE_NEW_RETURN (temp_initializer,
56 Server_ORBInitializer,
57 -1); // No exceptions yet!
58 PortableInterceptor::ORBInitializer_var orb_initializer =
59 temp_initializer;
61 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
63 CORBA::ORB_var orb =
64 CORBA::ORB_init (argc, argv);
66 // We do the command line parsing first
67 if (parse_args (argc, argv) != 0)
68 return -1;
69 CORBA::Object_var poa_object =
70 orb->resolve_initial_references("RootPOA");
72 if (CORBA::is_nil (poa_object.in ()))
73 ACE_ERROR_RETURN ((LM_ERROR,
74 " (%P|%t) Unable to initialize the POA.\n"),
75 -1);
77 PortableServer::POA_var root_poa =
78 PortableServer::POA::_narrow (poa_object.in ());
80 PortableServer::POAManager_var poa_manager =
81 root_poa->the_POAManager ();
83 CORBA::PolicyList policies;
84 policies.length (3);
85 policies[0] = root_poa->create_id_assignment_policy (
86 PortableServer::USER_ID);
87 policies[1] = root_poa->create_implicit_activation_policy (
88 PortableServer::NO_IMPLICIT_ACTIVATION);
89 policies[2] = root_poa->create_lifespan_policy (
90 PortableServer::PERSISTENT);
92 PortableServer::POA_var poa = root_poa->create_POA (
93 "PERS_POA", poa_manager.in (), policies);
95 for (CORBA::ULong i = 0; i < policies.length (); ++i)
97 policies[i]->destroy ();
100 // Instantiate the LCD_Display implementation class
101 Simple_Server_i display_impl (orb.in (), ACE_TEXT_ALWAYS_CHAR(key));
102 PortableServer::ObjectId_var id =
103 PortableServer::string_to_ObjectId ("IOGR_OID");
105 poa->activate_object_with_id (id.in(), &display_impl);
107 CORBA::Object_var server =
108 poa->id_to_reference (id.in ());
110 CORBA::String_var ior =
111 orb->object_to_string (server.in ());
113 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
115 // If the ior_output_file exists, output the ior to it
116 if (ior_output_file != 0)
118 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
119 if (output_file == 0)
120 ACE_ERROR_RETURN ((LM_ERROR,
121 "Cannot open output file for writing IOR: %s",
122 ior_output_file),
123 -1);
124 ACE_OS::fprintf (output_file, "%s", ior.in ());
125 ACE_OS::fclose (output_file);
128 // Set the forward references in the server request interceptor.
129 PortableInterceptor::ServerRequestInterceptor_var
130 server_interceptor = temp_initializer->server_interceptor ();
132 Simple_ServerRequestInterceptor_var interceptor =
133 Simple_ServerRequestInterceptor::_narrow (
134 server_interceptor.in ());
136 if (CORBA::is_nil (interceptor.in ()))
137 ACE_ERROR_RETURN ((LM_ERROR,
138 "(%P|%t) Could not obtain reference to "
139 "server request interceptor.\n"),
140 -1);
142 interceptor->forward_reference (ACE_TEXT_ALWAYS_CHAR(merged_iorstr));
144 poa_manager->activate ();
146 orb->run ();
148 catch (const CORBA::Exception& ex)
150 ex._tao_print_exception ("Caught exception:");
151 return -1;
153 return 0;