Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / DLL_ORB / Test_Client_Module.cpp
blob4fbd18caa77e6479b81da08b2b5c11957b70456e
1 // -*- C++ -*-
2 #include "Test_Client_Module.h"
3 #include "tao/TAO_Singleton_Manager.h"
4 #include "tao/StringSeqC.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/Argv_Type_Converter.h"
9 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'k':
21 ior = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: -k <ior>\n"),
28 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int
36 Test_Client_Module::init (int argc, ACE_TCHAR *argv[])
38 // -----------------------------------------------------------------
39 // Pre-ORB initialization steps necessary for proper DLL ORB
40 // support.
41 // -----------------------------------------------------------------
42 // Make sure TAO's singleton manager is initialized, and set to not
43 // register itself with the ACE_Object_Manager since it is under the
44 // control of the Service Configurator. If we register with the
45 // ACE_Object_Manager, then the ACE_Object_Manager will still hold
46 // (dangling) references to instances of objects created by this
47 // module and destroyed by this object when it is dynamically
48 // unloaded.
49 int register_with_object_manager = 0;
51 if (TAO_Singleton_Manager::instance ()->init (
52 register_with_object_manager) == -1)
53 ACE_ERROR_RETURN ((LM_ERROR,
54 "Test_Client_Module::init -- ORB pre-initialization "
55 "failed."),
56 -1); // No exceptions available yet, so return
57 // an error status.
59 // -----------------------------------------------------------------
60 // Boilerplate CORBA/TAO client-side ORB initialization code.
61 // -----------------------------------------------------------------
62 try
64 // Prepend a "dummy" program name argument to the Service
65 // Configurator argument vector.
66 int new_argc = argc + 1;
68 CORBA::StringSeq new_argv (new_argc);
69 new_argv.length (new_argc);
71 // Prevent the ORB from opening the Service Configurator file
72 // again since the Service Configurator file is already in the
73 // process of being opened.
74 new_argv[0] = CORBA::string_dup ("dummy");
76 // Copy the remaining arguments into the new argument vector.
77 for (int i = new_argc - argc, j = 0;
78 j < argc;
79 ++i, ++j)
80 new_argv[i] = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(argv[j]));
82 // Initialize the ORB.
83 this->orb_ = CORBA::ORB_init (new_argc,
84 new_argv.get_buffer (),
85 "CLIENT");
87 if (CORBA::is_nil (this->orb_.in ()))
88 return -1;
90 ACE_Argv_Type_Converter converter (new_argc, new_argv.get_buffer ());
92 if (::parse_args (new_argc, converter.get_TCHAR_argv ()) != 0)
93 return 1;
95 CORBA::Object_var obj =
96 this->orb_->string_to_object (ior);
98 this->test_ =
99 Test::_narrow (obj.in ());
101 if (CORBA::is_nil (this->test_.in ()))
103 ACE_ERROR_RETURN ((LM_DEBUG,
104 ACE_TEXT ("Nil Test reference <%s>\n"),
105 ior),
109 catch (const CORBA::Exception& ex)
111 ex._tao_print_exception (ACE_TEXT ("Test_Client_Module::init"));
113 return -1;
116 #if defined (ACE_HAS_THREADS)
118 // Become an Active Object so that the ORB will execute in a
119 // separate thread.
120 return this->activate ();
122 #else
124 return 0;
126 #endif /* ACE_HAS_THREADS */
130 Test_Client_Module::fini ()
132 ACE_DEBUG ((LM_INFO,
133 "Client is being finalized.\n"));
135 // ------------------------------------------------------------
136 // Pre-Test_Client_Module termination steps.
137 // ------------------------------------------------------------
138 // Explicitly clean up singletons and other objects created by TAO
139 // before unloading this module.
140 if (TAO_Singleton_Manager::instance ()->fini () == -1)
141 ACE_ERROR_RETURN ((LM_ERROR,
142 "Test_Client_Module::fini -- ORB pre-termination "
143 "failed."),
144 -1);
146 return 0;
150 Test_Client_Module::svc ()
154 // Invoke an operation on the Test object.
155 this->test_->invoke_me ();
157 /// Shutdown the remote ORB.
158 this->test_->shutdown ();
160 // Make sure the ORB is destroyed here - before the thread
161 // exits, because it may be holding global resources, owned by
162 // the Object Manager (thru its core, which is in turn owned by
163 // the ORB table; which is owned by the Object Manager).
164 // Otherwise the Object Manager will have clobbered them by the
165 // time it gets to destroy the ORB Table, which calls our
166 // fini(). Had we destroyed the ORB in our fini(), its core
167 // fininalization would have required access to those already
168 // deleted resources.
169 if (!CORBA::is_nil (this->orb_.in ()))
171 this->orb_->destroy ();
174 // This is a bit of a hack. The ORB Core's lifetime is tied to the
175 // lifetime of an object reference. We need to wipe out all object
176 // references before we call fini() on the TAO_Singleton_Manager.
178 // Note that this is only necessary if the default resource factory
179 // is used, i.e. one isn't explicitly loaded prior to initializing
180 // the ORB.
181 (void) this->test_.out ();
183 catch (const CORBA::Exception& ex)
185 ex._tao_print_exception (ACE_TEXT ("Test_Client_Module::svc"));
186 return -1;
189 return 0;
193 ACE_STATIC_SVC_DEFINE (Test_Client_Module,
194 ACE_TEXT ("Client_Module"),
195 ACE_SVC_OBJ_T,
196 &ACE_SVC_NAME (Test_Client_Module),
197 ACE_Service_Type::DELETE_THIS
198 | ACE_Service_Type::DELETE_OBJ,
201 ACE_FACTORY_DEFINE (Test_Client_Module, Test_Client_Module)