Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_1395_Regression / server.cpp
blob5873276dd90def5e98b92c1f3c2f6c9e368b197c
1 #include "ace/Get_Opt.h"
2 #include "Test_i.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "orbsvcs/Shutdown_Utilities.h"
6 const ACE_TCHAR *ior_output_file = 0;
8 class Service_Shutdown_Functor : public Shutdown_Functor
10 public:
11 Service_Shutdown_Functor (CORBA::ORB_ptr orb)
12 : orb_(CORBA::ORB::_duplicate (orb))
16 void operator() (int which_signal)
18 ACE_DEBUG ((LM_DEBUG,
19 "shutting down on signal %d\n", which_signal));
20 (void) this->orb_->shutdown ();
23 private:
24 CORBA::ORB_var orb_;
27 int
28 parse_args (int argc, ACE_TCHAR *argv[])
30 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
31 int c;
33 while ((c = get_opts ()) != -1)
34 switch (c)
36 case 'o':
37 ior_output_file = get_opts.opt_arg ();
38 break;
39 case '?':
40 default:
41 ACE_ERROR_RETURN ((LM_ERROR,
42 "SERVER (%P): usage: %s "
43 "-o <iorfile>"
44 "\n",
45 argv [0]),
46 -1);
48 // Indicates successful parsing of the command line
49 return 0;
52 int
53 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
56 try
58 CORBA::ORB_var orb =
59 CORBA::ORB_init (argc, argv);
61 Service_Shutdown_Functor killer (orb.in ());
62 Service_Shutdown kill_contractor (killer);
64 CORBA::Object_var poa_object =
65 orb->resolve_initial_references ("RootPOA");
67 if (CORBA::is_nil (poa_object.in ()))
68 ACE_ERROR_RETURN ((LM_ERROR,
69 "SERVER (%P): Unable to initialize the POA.\n"),
70 1);
72 PortableServer::POA_var root_poa =
73 PortableServer::POA::_narrow (poa_object.in ());
75 PortableServer::POAManager_var poa_manager =
76 root_poa->the_POAManager ();
78 if (parse_args (argc, argv) != 0)
79 return 1;
81 Test_i server_impl (orb.in ());
83 PortableServer::ObjectId_var tmp =
84 root_poa->activate_object (&server_impl);
87 CORBA::Object_var server = server_impl._this();
89 CORBA::String_var ior =
90 orb->object_to_string (server.in ());
92 // If the ior_output_file exists, output the ior to it
93 if (ior_output_file != 0)
95 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
96 if (output_file == 0)
97 ACE_ERROR_RETURN ((LM_ERROR,
98 "SERVER (%P): Cannot open output file "
99 "for writing IOR: %s",
100 ior_output_file),
102 ACE_OS::fprintf (output_file, "%s", ior.in ());
103 ACE_OS::fclose (output_file);
105 ACE_DEBUG ((LM_DEBUG,
106 "SERVER (%P): Activated as file://%s\n",
107 ior_output_file));
109 else
111 ACE_DEBUG ((LM_DEBUG,
112 "SERVER (%P): Activated as <%s>\n",
113 ior.in ()));
116 poa_manager->activate();
118 orb->run ();
120 root_poa->destroy (1, 1);
122 catch (const CORBA::Exception& ex)
124 ex._tao_print_exception ("SERVER (%P): Caught exception:");
125 return 1;
128 return 0;