Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / InterfaceRepo / Application_Test / server.cpp
blob1b8fe6b409f39340589961b614e58d8aab56ea9c
1 // -*- C++ -*-
2 #include "test_i.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "orbsvcs/Shutdown_Utilities.h"
5 #include "ace/Get_Opt.h"
9 static const ACE_TCHAR *ior_output_file = ACE_TEXT("iorfile");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-o <ior> "
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 class Service_Shutdown_Functor : public Shutdown_Functor
39 public:
40 Service_Shutdown_Functor (CORBA::ORB_ptr orb)
41 : orb_(CORBA::ORB::_duplicate (orb))
45 void operator() (int which_signal)
47 ACE_DEBUG ((LM_DEBUG,
48 "shutting down on signal %d\n", which_signal));
49 (void) this->orb_->shutdown ();
52 private:
53 CORBA::ORB_var orb_;
56 static int
57 write_ior_to_file (const char *ior)
59 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
61 if (output_file == 0)
63 ACE_ERROR_RETURN ((LM_ERROR,
64 "Cannot open output files for writing IOR: %s\n",
65 ior_output_file),
66 -1);
69 int result = ACE_OS::fprintf (output_file,
70 "%s",
71 ior);
72 if (result < 0)
74 ACE_ERROR_RETURN ((LM_ERROR,
75 "ACE_OS::fprintf failed while writing %s to %s\n",
76 ior,
77 ior_output_file),
78 -1);
81 ACE_OS::fclose (output_file);
83 return 0;
86 int
87 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
89 try
91 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
93 if (parse_args (argc, argv) != 0)
94 return 1;
96 Service_Shutdown_Functor killer (orb.in ());
97 Service_Shutdown kill_contractor (killer);
99 CORBA::Object_var poa_object =
100 orb->resolve_initial_references ("RootPOA");
102 PortableServer::POA_var root_poa =
103 PortableServer::POA::_narrow (poa_object.in ());
105 PortableServer::POAManager_var poa_manager =
106 root_poa->the_POAManager ();
108 poa_manager->activate ();
110 inventory_i servant_impl;
112 warehouse::inventory_var servant =
113 servant_impl._this ();
115 CORBA::String_var ior =
116 orb->object_to_string (servant.in ());
118 int write_result = write_ior_to_file (ior.in ());
120 if (write_result != 0)
122 ACE_ERROR_RETURN ((LM_ERROR,
123 "%p\n",
124 "write_ior_to_file"),
125 -1);
128 orb->run ();
130 root_poa->destroy (1,
133 orb->destroy ();
135 catch (const CORBA::Exception& ex)
137 ex._tao_print_exception ("server exception:");
138 return 1;
141 return 0;