xref: /aoo41x/main/stoc/test/testsmgr_cpnt.cxx (revision 647a425c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26 #include <rtl/alloc.h>
27 #include <osl/security.h>
28 #include <osl/thread.h>
29 #include <osl/mutex.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31 #include <cppuhelper/weak.hxx>
32 #include <uno/mapping.hxx>
33 
34 
35 #include <cppuhelper/factory.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 #include <cppuhelper/implbase1.hxx>
38 #include <registry/registry.hxx>
39 
40 #include <com/sun/star/registry/XSimpleRegistry.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/registry/XImplementationRegistration.hpp>
44 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
47 #include <com/sun/star/container/XEnumerationAccess.hpp>
48 #include <com/sun/star/container/XEnumeration.hpp>
49 
50 #if defined ( UNX )
51 #include <limits.h>
52 #define _MAX_PATH PATH_MAX
53 #endif
54 
55 
56 
57 #if OSL_DEBUG_LEVEL > 0
58 #define TEST_ENSHURE(c, m)   OSL_ENSURE(c, m)
59 #else
60 #define TEST_ENSHURE(c, m)   OSL_VERIFY(c)
61 #endif
62 
63 
64 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
65 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
66 
67 
68 using namespace com::sun::star::uno;
69 using namespace com::sun::star::registry;
70 using namespace com::sun::star::lang;
71 using namespace com::sun::star::container;
72 using namespace osl;
73 using namespace rtl;
74 using namespace cppu;
75 
76 
getProcessServiceManager()77 Reference<XMultiServiceFactory> getProcessServiceManager()
78 {
79 	Reference<XMultiServiceFactory > s_x;
80 	if (! s_x.is())
81 	{
82 		MutexGuard aGuard( Mutex::getGlobalMutex() );
83 		if (! s_x.is())
84 			s_x = createRegistryServiceFactory( OUString::createFromAscii( "stoctest.rdb" ), sal_False );
85 	}
86 	return s_x;
87 }
88 
createRegistryServiceManager(const OUString & registryName)89 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
90 {
91 	return createRegistryServiceFactory( registryName );
92 }
93 
94 
95 /**********************************
96 * The service, that is used to test the Service manager
97 *
98 *
99 *
100 *************************************/
101 static sal_uInt32 nInstanceCount = 0;
102 class Test_Manager_Impl : public WeakImplHelper1< XServiceInfo >
103 {
104 public:
Test_Manager_Impl()105 	Test_Manager_Impl(){ nInstanceCount++;}
106 	~Test_Manager_Impl();
107 
108 	// XServiceInfo
109 	OUString 					SAL_CALL getImplementationName() throw();
110     sal_Bool 					SAL_CALL supportsService(const OUString& ServiceName) throw();
111     Sequence< OUString > 		SAL_CALL getSupportedServiceNames(void) throw();
112     static Sequence< OUString >	SAL_CALL getSupportedServiceNames_Static(void) throw();
113 
114 private:
115 //	static XIdlClassRef		getStaticIdlClass();
116 };
117 
~Test_Manager_Impl()118 Test_Manager_Impl::~Test_Manager_Impl()
119 {
120 	nInstanceCount--;
121 }
122 
123 
124 // alt, wird von der neuen Mimic nicht mehr gebraucht
Test_Manager_Impl_CreateInstance_Impl()125 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
126 {
127 	return (OWeakObject *)new Test_Manager_Impl();
128 }
129 
130 
131 //*************************************************************************
132 // Test_Manager_Impl_CreateInstance()
133 //
Test_Manager_Impl_CreateInstance(const Reference<XMultiServiceFactory> &)134 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
135 	const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
136 {
137 	Reference < XInterface >  xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
138 
139 	return xService;
140 }
141 
142 
143 //*************************************************************************
144 // Test_Manager_Impl::getImplementationName
145 //
getImplementationName()146 OUString Test_Manager_Impl::getImplementationName() throw()
147 {
148 	return OUString::createFromAscii(IMPLEMENTATION_NAME);
149 }
150 
151 //*************************************************************************
152 // Test_Manager_Impl::supportsService
153 //
supportsService(const OUString & ServiceName)154 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
155 {
156 	Sequence< OUString > aSNL = getSupportedServiceNames();
157 	const OUString * pArray = aSNL.getConstArray();
158 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
159 		if( pArray[i] == ServiceName )
160 			return sal_True;
161 	return sal_False;
162 }
163 
164 //*************************************************************************
165 // Test_Manager_Impl::getSupportedServiceNames
166 //
getSupportedServiceNames(void)167 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames(void) throw ()
168 {
169 	return getSupportedServiceNames_Static();
170 }
171 
172 //*************************************************************************
173 // Test_Manager_Impl::getSupportedServiceNames_Static
174 //
getSupportedServiceNames_Static(void)175 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static(void) throw ()
176 {
177 	Sequence< OUString > aSNS( 2 );
178 	aSNS.getArray()[0] = OUString::createFromAscii(SERVICE_NAME);
179 	aSNS.getArray()[1] = OUString::createFromAscii("com.sun.star.bridge.Bridge");
180 	return aSNS;
181 }
182 
183 
184 
185 
186 /****
187 *
188 *
189 *  This routine performs the test of the process service manager ( getProcessServiceManager is called )
190 *
191 *
192 *
193 ****/
194 
195 #include <stdio.h>
196 
test_ServiceManager()197 extern "C" void SAL_CALL test_ServiceManager()
198 {
199 #if ! defined SAL_DLLPREFIX
200 #define SAL_DLLPREFIX ""
201 #endif
202  	OUString atUModule2 = OUString(
203         RTL_CONSTASCII_USTRINGPARAM(
204             SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ) );
205 
206 	// expand shared library name
207 	OString  atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
208 
209 	// get the process servicemanager
210 	Reference <XMultiServiceFactory>  xSMgr = getProcessServiceManager();
211 
212 	TEST_ENSHURE( xSMgr.is() , "query on XServiceManager failed" );
213 
214 	Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
215 	TEST_ENSHURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
216 	Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString::createFromAscii("com.sun.star.registry.SimpleRegistry")));
217 	TEST_ENSHURE( xEnum.is() , "createContentEnumeration failed" );
218 	sal_Int32 nLen = 0;
219 	while( xEnum->hasMoreElements() )
220 	{
221 		nLen++;
222 		xEnum->nextElement();
223 	}
224 	TEST_ENSHURE( nLen == 1, "more than one implementation for SimpleRegistry" );
225 
226 	Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
227 	TEST_ENSHURE( xImplEnum.is() , "query on XEnumeration failed" );
228 	xEnum = Reference<XEnumeration >(xImplEnum->createEnumeration());
229 	TEST_ENSHURE( xEnum.is() , "createEnumeration failed" );
230 	nLen = 0;
231 	while( xEnum->hasMoreElements() )
232 	{
233 		nLen++;
234 		Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
235         OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
236         ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
237 	}
238 	TEST_ENSHURE( nLen == 8, "more than 6 factories" );
239 
240 	// try to get an instance for a unknown service
241 	TEST_ENSHURE( !xSMgr->createInstance(OUString::createFromAscii("bla.blup.Q")).is(), "unknown service provider found" );
242 
243 	//
244 	// First test : register service via the internal function of the component itself
245 	//
246 	{
247 		Reference< XImplementationRegistration >
248 			xInst( xSMgr->createInstance(OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration")), UNO_QUERY );
249 		TEST_ENSHURE( xInst.is(), "no ImplementationRegistration" );
250 
251 		try	{
252 			// register the services via writeComponentRegInfo (see at end of this file)
253 			xInst->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
254 		}
255 		catch( CannotRegisterImplementationException e ) {
256 			TEST_ENSHURE( 0, "register implementation failed" );
257 		}
258 
259 		// getImplementations() check
260  		Sequence<OUString> seqImpl = xInst->getImplementations(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2);
261 		TEST_ENSHURE( seqImpl.getLength() == 1, "count of implementantions is wrong" );
262 		TEST_ENSHURE( seqImpl.getConstArray()[0] == OUString::createFromAscii("com.sun.star.DummyService.V10"), "implementation name is not equal" );
263 
264 
265 		// tests, if a service provider can be instantiated.
266 
267 		Reference< XInterface > xIFace(xSMgr->createInstance(OUString::createFromAscii("com.sun.star.ts.TestManagerImpl")));
268 		TEST_ENSHURE( xIFace.is(), "loadable service not found" );
269 
270 		// remove the service
271 		TEST_ENSHURE( 	xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()),
272 						"revoke implementation failed" );
273 	}
274 
275 
276 //	Reference < XSimpleRegistry > xSReg(  xSMgr->createInstance(OUString::createFromAscii("com::sun::star.uno.repos.SimpleRegistry")), UNO_QUERY );
277 //	TEST_ENSHURE( xSReg.is() , "Simple registry couldn't be instantiated" );
278 
279 //	xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_True);
280 //	xSReg->close();
281 
282 	// laut dbo : C-API muss nicht mehr unterstuezt werden
283 	//TEST_ENSHURE( registerExternService(atModule, "dummy.reg"), "install failed" );
284 	//TEST_ENSHURE( deregisterExternService(atModule, "dummy.reg"), "deinstall failed" );
285 
286 
287 //	UNO_INTERFACE(XMultiServiceFactory) xUnoSMgr = {0,0};
288 //	smart2uno(xSMgr, xUnoSMgr);
289 
290 //	TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy.reg"), "install failed" );
291 //	TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, atModule2, "dummy.reg"), "deinstall failed" );
292 
293 //	TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy2.reg"), "install failed" );
294 
295 //TODO : Java loader test
296 //	String testUrl(getTestJarUrl());
297 //	TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.Java", testUrl, "dummy.reg"), "install failed" );
298 //	TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, testUrl, "dummy.reg"), "deinstall failed" );
299 
300 //	if (!UNO_isNull((UNO_Ifc*)&xUnoSMgr))
301 //		xUnoSMgr.m_pVmt->release(xUnoSMgr.m_pCtx);
302 
303 //	xSReg->open(OUString::createFromAscii("dummy.reg"), sal_True, sal_False);
304 //	TEST_ENSHURE(!xSReg->getRootKey()->openKey(OUString::createFromAscii("/SERVICES/com::sun::star/ts/TestManagerImpl/URL")).is(),
305 //				"deinstall failed" );
306 
307 //	xSReg->close();
308 
309 //	xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_False);
310 //	xSReg->destroy();
311 //	xSReg->open(OUString::createFromAscii("dummy2.reg"), sal_False, sal_False);
312 //	xSReg->destroy();
313 
314 
315 	// Test initialisieren
316 /*
317 	XServiceProviderRef xSiSP1 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
318 	XServiceProviderRef xSiSP11 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
319 	XServiceProviderRef xSiSP2 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl2"), Test_Manager_Impl_getReflection );
320 */
321 /*
322 	// second test
323 	// create XServiceProvider via createSingleFactory and write them directly into the registry
324 	// For this is needed a sequence of supported servicenames and a createComponent function pointer
325 	{
326 		Reference< XServiceProvider > xSiSP1(createSingleFactory(
327 			xSMgr,
328 			OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1),
329 			Test_Manager_Impl_CreateInstance,
330 			Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
331 		Reference< XServiceProvider > xSiSP11(createSingleFactory(
332 			xSMgr,
333 			OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"),
334 			Test_Manager_Impl_CreateInstance,
335 			Test_Manager_Impl::getSupportedServiceNames_Static() ),UNO_QUERY);
336 		Reference< XServiceProvider > xSiSP2(createSingleFactory(
337 			xSMgr,
338 			L"com::sun::star.usr.Test_Manager_Impl2",
339 			Test_Manager_Impl_CreateInstance,
340 			Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
341 
342 		// put the service providers into the registry
343 		xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP1 );
344 		xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP11 );
345 		xReg->registerServiceProvider( L"com::sun::star.test.TestManager2", xSiSP2 );
346 
347 		// TestManager1
348 		Reference< XServiceProvider > xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager1");
349 		Reference< XSingleServiceFactory > xFact( xProv, UNO_QUERY );
350 		TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager1 not found" );
351 
352 		Reference< XInterface > xTest1 = xFact->createInstance();
353 		TEST_ENSHURE( nInstanceCount == 1, "wrong service instanciated" );
354 
355 		// TestManager2
356 		xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager2");
357 		xFact = Reference < XSingleServiceFactory > ( xProv , UNO_QUERY );
358 		TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager2 not found" );
359 
360 		Reference < XInterface > xTest2 = xFact->createInstance();
361 		TEST_ENSHURE( nInstanceCount == 2, "wrong service instanciated" );
362 
363 		xTest1 = Reference< XInterface >();
364 		xTest2 = Reference< XInterface >();
365 		TEST_ENSHURE( nInstanceCount == 0, "wrong service deleted" );
366 
367 		Reference< XEnumeration > xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
368 		TEST_ENSHURE( xEnum.is() , "no provider enumeration" );
369 
370 		sal_Int32 nSPTestManagerImplLen2 = 0;
371 
372 		while (xEnum.is() && xEnum->hasMoreElements())
373 		{
374 			nSPTestManagerImplLen2++;
375 			xEnum->nextElement();
376 
377 		}
378 		TEST_ENSHURE( nSPTestManagerImplLen2 == 2, "queryServiceProviders() wrong length" );
379 
380 		sal_Int32 nCount = 0;
381 		xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
382 		while (xEnum->hasMoreElements())
383 		{
384 			Reference< XServiceProvider > xProv;
385 			xEnum->nextElement() >>= xProv;
386 			if (xProv == xSiSP1 || xProv == xSiSP11)
387 				nCount++;
388 		}
389 
390 		TEST_ENSHURE( nCount == 2 , "not all com::sun::star.testimpl.TestManagerImpl registered" );
391 */
392 /*
393 	{
394 		Reference< XMultiServiceFactory > xTestManager(createRegistryServiceManager(L"testmanager.rdb"));
395 		TEST_ENSHURE( xTestManager.is(), "create Test ServiceManager failed!" );
396 	}
397 */
398 	Reference<XComponent> xComp(xSMgr, UNO_QUERY);
399 	xComp->dispose();
400 
401 	xComp.clear();
402 	xSMgr.clear();
403 }
404 
405 
406 
407 extern "C"
408 {
409 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)410 void SAL_CALL component_getImplementationEnvironment(
411 	const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
412 {
413 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
414 }
415 //==================================================================================================
component_writeInfo(void *,void * pRegistryKey)416 sal_Bool SAL_CALL component_writeInfo(
417 	void * /*pServiceManager*/, void * pRegistryKey )
418 {
419 	if (pRegistryKey)
420 	{
421 		try
422 		{
423 			Reference< XRegistryKey > xNewKey(
424 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
425 					OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLEMENTATION_NAME "/UNO/SERVICES") ) ) );
426 
427 			const Sequence< OUString > & rSNL =
428 				Test_Manager_Impl::getSupportedServiceNames_Static();
429 			const OUString * pArray = rSNL.getConstArray();
430 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
431 				xNewKey->createKey( pArray[nPos] );
432 
433 			return sal_True;
434 		}
435 		catch (InvalidRegistryException &)
436 		{
437 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
438 		}
439 	}
440 	return sal_False;
441 }
442 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)443 void * SAL_CALL component_getFactory(
444 	const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
445 {
446 	void * pRet = 0;
447 
448 	if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
449 	{
450 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
451 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
452 			OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLEMENTATION_NAME) ),
453 			Test_Manager_Impl_CreateInstance,
454 			Test_Manager_Impl::getSupportedServiceNames_Static() ) );
455 
456 		if (xFactory.is())
457 		{
458 			xFactory->acquire();
459 			pRet = xFactory.get();
460 		}
461 	}
462 
463 	return pRet;
464 }
465 }
466 
467 
468 
469