=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / ORB_Local_Config / Service_Dependency / Test.cpp
blobf97cd985f0a495ddc6504fa007c62236f468c167
1 #include "tao/Codeset_Descriptor_Base.h"
2 #include "tao/Codeset_Manager_Factory_Base.h"
3 #include "tao/Codeset_Manager.h"
4 #include "tao/ORBInitializer_Registry_Adapter.h"
6 #include "ace/ARGV.h"
7 #include "ace/Dynamic_Service.h"
8 #include "ace/Dynamic_Service_Dependency.h"
9 #include "ace/Thread_Manager.h"
10 #include "ace/Intrusive_Auto_Ptr.h"
12 #include "Service_Configuration_Per_ORB.h"
14 // Return the name of the service configuration file, based on the current ACE
15 // support for wide characters and unicode
16 /// Borrowing this from the $ACE_ROOT/test/Service_Config_Test
17 /// The various config files have the same effect of loading 5
18 /// new service objects.
20 const ACE_TCHAR*
21 file_Service_Config_Test ()
23 #if defined (ACE_USES_WCHAR)
24 // When using full Unicode support, use the version of the Service
25 // Configurator file appropriate to the platform.
26 // For example, Windows Unicode uses UTF-16.
28 // iconv(1) found on Linux, for example, can
29 // be used to convert between encodings.
31 // Byte ordering is also an issue, so we should be
32 // generating this file on-the-fly from the UTF-8 encoded
33 // file by using functions like iconv(1) or iconv(3).
34 # if defined (ACE_WIN32)
35 static const ACE_TCHAR svc_conf[] =
36 ACE_TEXT ("Service_Config_Test.UTF-16")
37 ACE_TEXT (ACE_DEFAULT_SVC_CONF_EXT);
38 # else
39 static const ACE_TCHAR svc_conf[] =
40 ACE_TEXT ("Service_Config_Test.WCHAR_T")
41 ACE_TEXT (ACE_DEFAULT_SVC_CONF_EXT);
42 # endif /* ACE_WIN32 */
43 #else
44 // ASCII (UTF-8) encoded Service Configurator file.
45 static const ACE_TCHAR svc_conf[] =
46 ACE_TEXT ("Service_Config_Test")
47 ACE_TEXT (ACE_DEFAULT_SVC_CONF_EXT);
48 #endif /* ACE_USES_WCHAR */
50 return svc_conf;
54 // @brief Loading dynamic services in a local repository
56 int
57 testOpenDynamicServices (int , ACE_TCHAR *[])
59 ACE_TRACE ("testOpenDynamicServices");
61 ACE_ARGV new_argv;
63 // Process the Service Configurator directives in this test's
64 if (false == (new_argv.add (ACE_TEXT ("bogus")) != -1
65 && new_argv.add (ACE_TEXT ("-f")) != -1
66 && new_argv.add (file_Service_Config_Test ()) != -1))
68 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error: Failed to build an argv\n")));
69 return -1;
72 // We need this scope to make sure that the destructor for the
73 // <ACE_Service_Config> gets called.
74 ACE_Service_Gestalt_Test daemon(10);
76 if (daemon.open (new_argv.argc (),
77 new_argv.argv ()) == -1 && errno == ENOENT)
79 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error: Unable to open service config\n")));
80 return -1;
83 ACE_DEBUG ((LM_DEBUG,
84 "\tdaemon.services_count () -> %d\n",
85 daemon.services_count ()));
87 if (5 != daemon.services_count ())
89 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error: Expected %d service, but found %d instead\n"), 5, daemon.services_count ()));
90 return -1;
93 // Since the loaded services start their own threads, wait until all
94 // of them are done to avoid pulling the rug under their feet.
95 ACE_Thread_Manager::instance ()->wait ();
96 return 0;
102 // @brief Try loading the ORBInitializer_Registry locally
105 testORBInitializer_Registry (int , ACE_TCHAR *[])
107 ACE_TRACE ("testORBInitializer_Registry");
109 // for global service registrations
110 ACE_Intrusive_Auto_Ptr<ACE_Service_Gestalt_Test> glob (new ACE_Service_Gestalt_Test ());
112 ACE_Intrusive_Auto_Ptr<ACE_Service_Gestalt_Test> one (new ACE_Service_Gestalt_Test (10));
114 size_t glob_size = glob->services_count ();
115 size_t loca_size = one->services_count ();
117 // It is expected to be empty at this point since it is not using
118 // the global repo
119 if (loca_size != 0)
121 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Expected empty repository\n")));
122 return -1;
125 // Lookup it up.
126 TAO::ORBInitializer_Registry_Adapter* oir =
127 ACE_Dynamic_Service<TAO::ORBInitializer_Registry_Adapter>::instance
128 (one.get (), "ORBInitializer_Registry");
130 #if defined (TAO_AS_STATIC_LIBS)
131 if ((oir == 0))
133 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Expected to find staticaly linked ORBInitializer_Registry\n")));
134 return -1;
136 #else
137 if (oir != 0)
139 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Not expected to find ORBInitializer_Registry\n")));
140 return -1;
142 #endif
145 #if !defined (TAO_AS_STATIC_LIBS)
146 // In case we build shared, try to load the PI Client library, in a
147 // static build we just can't do this, so don't try it, lower layers
148 // output an error then.
149 if (oir == 0)
151 one->process_directive (
152 ACE_DYNAMIC_VERSIONED_SERVICE_DIRECTIVE("ORBInitializer_Registry",
153 "TAO_PI",
154 TAO_VERSION,
155 "_make_ORBInitializer_Registry",
156 ""));
157 oir =
158 ACE_Dynamic_Service<TAO::ORBInitializer_Registry_Adapter>::instance
159 (one.get (), "ORBInitializer_Registry");
161 if (oir == 0)
163 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Expected to find ORBInitializer_Registry\n")));
164 return -1;
166 #endif
169 if (glob_size != glob->services_count ())
171 ACE_ERROR ((LM_ERROR,
172 "Expected %d local static service registrations, found %d\n",
173 glob_size,
174 glob->services_count ()));
175 return -1;
178 // The local repository must have asquired also the static services
179 // registered within the dynamic service we just loaded. As of this
180 // writing, loading ORBInitializer_Registry causes the registration of
181 // four other (static) services. The PolicyFactory_Loader,
182 // ClientRequestInterceptor_Adapter_Factory and PICurrent_Loader are
183 // registred explicitely, while CodecFactory_Loader - indirectly.
185 if (loca_size == one->services_count ())
187 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Expected to find additional services present\n")));
188 return -1;
191 ACE_DEBUG ((LM_DEBUG,
192 "(%P|%t) try global dynamic service"
193 " on ORBInitializer_Registry ...\n"));
195 // Try to instantiate the dynamic service from the global repository ...
196 TAO::ORBInitializer_Registry_Adapter* oir1 =
197 ACE_Dynamic_Service<TAO::ORBInitializer_Registry_Adapter>::instance
198 ("ORBInitializer_Registry");
201 // Right! It should not have been global.
202 if (oir1 != 0)
203 ACE_ERROR_RETURN((LM_ERROR,
204 ACE_TEXT ("Unexpected to find ORBInitializer_Registry\n")),
205 -1);
207 ACE_DEBUG ((LM_DEBUG,
208 "(%P|%t) try local dynamic service"
209 " on ORBInitializer_Registry ...\n"));
211 // Try to instantiate the dynamic service from the local repository ...
212 TAO::ORBInitializer_Registry_Adapter* oir2 =
213 ACE_Dynamic_Service<TAO::ORBInitializer_Registry_Adapter>::instance
214 (one.get (), "ORBInitializer_Registry");
216 // Right! That's local.
217 if (oir2 == 0)
218 ACE_ERROR_RETURN((LM_ERROR,
219 ACE_TEXT ("Expected to find ORBInitializer_Registry locally\n")),
220 -1);
222 ACE_DEBUG ((LM_DEBUG,
223 "(%P|%t) Explicitely initialize ORBInitializer_Registry"
224 " (although Dynamic_Service already did it) ...\n"));
226 // ... initialize, but note that without the guard there is nothing to tell
227 // the service which gestalt must be used. If init() does static service
228 // registrations, those may end up in the wrong (global) gestalt and will
229 // be in memory, which may not be mapped by finalization time!
231 ACE_Service_Config_Guard guard (one.get ());
232 oir2->init (0,0);
235 ACE_DEBUG ((LM_DEBUG,
236 "(%P|%t) Try dynamic_service on a "
237 "dependent static service (CodecFactory_Loader) ...\n"));
239 // ... and also one of the dependent static services
240 if (0 == ACE_Dynamic_Service <ACE_Service_Object>::instance
241 (one.get (), "CodecFactory_Loader"))
242 ACE_ERROR_RETURN((LM_ERROR,
243 ACE_TEXT ("Error: Expected to find CodecFactory_Loader locally\n")),
244 -1);
247 ACE_DEBUG ((LM_DEBUG,
248 "(%P|%t) Explicitely dynamic_service PolicyFactory_Loader"
249 " (although ORBInitializer_Registry already did it) ...\n"));
251 one->process_directive
252 (ACE_DYNAMIC_VERSIONED_SERVICE_DIRECTIVE("PolicyFactory_Loader",
253 "TAO_PI",
254 TAO_VERSION,
255 "_make_TAO_PolicyFactory_Loader",
256 ""));
259 if (0 == ACE_Dynamic_Service <ACE_Service_Object>::instance
260 (one.get (), "PolicyFactory_Loader"))
261 ACE_ERROR_RETURN((LM_ERROR,
262 ACE_TEXT ("Error: Expected to find PolicyFactory_Loader locally\n")),
263 -1);
266 ACE_DEBUG ((LM_DEBUG,
267 "(%P|%t) We're done testing.\n"));
268 return 0;
272 // @brief ...
276 testServiceDependency (int , ACE_TCHAR *[])
278 ACE_TRACE ("testServiceDependency");
280 ACE_DEBUG ((LM_DEBUG, "sizeof (ACE_DLL) == %d\n", sizeof (ACE_DLL)));
281 ACE_DEBUG ((LM_DEBUG, "sizeof (ACE_Dynamic_Service_Dependency) == %d\n",
282 sizeof (ACE_Dynamic_Service_Dependency)));
284 TAO_Codeset_Manager *codeset_manager = 0;
285 ACE_Dynamic_Service_Dependency *pdep = 0;
288 // Start a block to limit the lifespan of a gestalt
289 ACE_Intrusive_Auto_Ptr<ACE_Service_Gestalt_Test> one (new ACE_Service_Gestalt_Test (10));
291 int result = one->process_directive
292 (ACE_DYNAMIC_VERSIONED_SERVICE_DIRECTIVE("TAO_Codeset",
293 "TAO_Codeset",
294 TAO_VERSION,
295 "_make_TAO_Codeset_Manager_Factory",
296 ""));
297 if (result != 0)
298 ACE_ERROR_RETURN((LM_ERROR,
299 ACE_TEXT ("Expected to register TAO_Codeset successfuly\n")),
300 -1);
302 TAO_Codeset_Manager_Factory_Base *factory =
303 ACE_Dynamic_Service<TAO_Codeset_Manager_Factory_Base>::instance
304 (one.get (), "TAO_Codeset");
306 if (factory == 0)
307 ACE_ERROR_RETURN((LM_ERROR,
308 ACE_TEXT ("Expected to obtain an instance successfuly\n")),
309 -1);
311 codeset_manager = factory->create ();
312 if (codeset_manager == 0)
314 ACE_DEBUG ((LM_DEBUG, "Creating dependency ...\n"));
316 // [1]
318 // Stating that a thing depends on that dynamic service. Why?
319 // Read on ...
321 pdep = new ACE_Dynamic_Service_Dependency (one.get (), ACE_TEXT ("TAO_Codeset"));
323 // This would ordinarily cause the dynamic services to get
324 // unloaded and their DLL's unmapped ...
327 // ... therefore the following code would crash miserably because it
328 // needs the ~TAO_Codeset_Manager()'s code, which is in the
329 // (unlodaed) DLL's text segment ...
330 delete codeset_manager;
332 // ... unless of course we used the magic dependency statement,
333 // above - [1]
334 delete pdep;
336 return 0;
341 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
343 return
344 testServiceDependency (argc, argv)
345 && testOpenDynamicServices (argc, argv)
346 && testORBInitializer_Registry(argc, argv);