1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/alloc.h>
21 #include <osl/security.h>
22 #include <osl/thread.h>
23 #include <osl/mutex.hxx>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <uno/mapping.hxx>
30 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <registry/registry.hxx>
35 #include <com/sun/star/registry/XSimpleRegistry.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/registry/XImplementationRegistration.hpp>
39 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
42 #include <com/sun/star/container/XEnumerationAccess.hpp>
43 #include <com/sun/star/container/XEnumeration.hpp>
47 #define _MAX_PATH PATH_MAX
51 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
52 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
55 using namespace css::uno
;
56 using namespace css::registry
;
57 using namespace css::lang
;
58 using namespace css::container
;
64 Reference
<XMultiServiceFactory
> getProcessServiceManager()
66 Reference
<XMultiServiceFactory
> s_x
;
69 MutexGuard
aGuard( Mutex::getGlobalMutex() );
71 s_x
= createRegistryServiceFactory( OUString("stoctest.rdb"), sal_False
);
76 Reference
< XMultiServiceFactory
> createRegistryServiceManager( const OUString
& registryName
)
78 return createRegistryServiceFactory( registryName
);
82 /**********************************
83 * The service, that is used to test the Service manager
87 *************************************/
88 static sal_uInt32 nInstanceCount
= 0;
89 class Test_Manager_Impl
: public WeakImplHelper1
< XServiceInfo
>
92 Test_Manager_Impl(){ nInstanceCount
++;}
96 OUString SAL_CALL
getImplementationName() throw();
97 sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw();
98 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw();
99 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_Static() throw();
102 // static XIdlClassRef getStaticIdlClass();
105 Test_Manager_Impl::~Test_Manager_Impl()
111 // old, is no longer needed by the new Mimic
112 Reference
< XInterface
> SAL_CALL
Test_Manager_Impl_CreateInstance_Impl()
114 return (OWeakObject
*)new Test_Manager_Impl();
119 // Test_Manager_Impl_CreateInstance()
121 Reference
< XInterface
> SAL_CALL
Test_Manager_Impl_CreateInstance(
122 const Reference
< XMultiServiceFactory
> & /*rSMgr*/ ) throw (Exception
)
124 Reference
< XInterface
> xService
= (XWeak
*)(OWeakObject
*)new Test_Manager_Impl( );
131 // Test_Manager_Impl::getImplementationName
133 OUString
Test_Manager_Impl::getImplementationName() throw()
135 return OUString(IMPLEMENTATION_NAME
);
138 // Test_Manager_Impl::supportsService
139 sal_Bool
Test_Manager_Impl::supportsService( const OUString
& ServiceName
) throw()
141 return cppu::supportsService(this, ServiceName
);
145 // Test_Manager_Impl::getSupportedServiceNames
147 Sequence
< OUString
> Test_Manager_Impl::getSupportedServiceNames() throw ()
149 return getSupportedServiceNames_Static();
153 // Test_Manager_Impl::getSupportedServiceNames_Static
155 Sequence
< OUString
> Test_Manager_Impl::getSupportedServiceNames_Static() throw ()
157 Sequence
< OUString
> aSNS( 2 );
158 aSNS
.getArray()[0] = SERVICE_NAME
;
159 aSNS
.getArray()[1] = "com.sun.star.bridge.Bridge";
169 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
177 extern "C" void SAL_CALL
test_ServiceManager()
179 #if ! defined SAL_DLLPREFIX
180 #define SAL_DLLPREFIX ""
182 OUString atUModule2
= SAL_DLLPREFIX
"testsmgr_component" SAL_DLLEXTENSION
;
184 // expand shared library name
185 OString
atModule2( OUStringToOString(atUModule2
, RTL_TEXTENCODING_ASCII_US
) );
187 // get the process servicemanager
188 Reference
<XMultiServiceFactory
> xSMgr
= getProcessServiceManager();
190 OSL_ENSURE( xSMgr
.is() , "query on XServiceManager failed" );
192 Reference
<XContentEnumerationAccess
> xContEnum(xSMgr
, UNO_QUERY
);
193 OSL_ENSURE( xContEnum
.is() , "query on XContentEnumerationAccess failed" );
194 Reference
<XEnumeration
> xEnum(xContEnum
->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
195 OSL_ENSURE( xEnum
.is() , "createContentEnumeration failed" );
197 while( xEnum
->hasMoreElements() )
200 xEnum
->nextElement();
202 OSL_ENSURE( nLen
== 1, "more than one implementation for SimpleRegistry" );
204 Reference
<XEnumerationAccess
> xImplEnum(xSMgr
, UNO_QUERY
);
205 OSL_ENSURE( xImplEnum
.is() , "query on XEnumeration failed" );
206 xEnum
= Reference
<XEnumeration
>(xImplEnum
->createEnumeration());
207 OSL_ENSURE( xEnum
.is() , "createEnumeration failed" );
209 while( xEnum
->hasMoreElements() )
212 Reference
< XServiceInfo
> sf( xEnum
->nextElement(), UNO_QUERY
);
213 OString
str( OUStringToOString( sf
->getImplementationName(), RTL_TEXTENCODING_ASCII_US
) );
214 ::fprintf( stderr
, "> implementation name: %s\n", str
.getStr() );
216 OSL_ENSURE( nLen
== 8, "more than 6 factories" );
218 // try to get an instance for a unknown service
219 OSL_VERIFY( !xSMgr
->createInstance("bla.blup.Q").is() );
222 // First test : register service via the internal function of the component itself
225 Reference
< XImplementationRegistration
>
226 xInst( xSMgr
->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY
);
227 OSL_ENSURE( xInst
.is(), "no ImplementationRegistration" );
230 // register the services via writeComponentRegInfo (see at end of this file)
231 xInst
->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2
, Reference
< XSimpleRegistry
>() );
233 catch(const CannotRegisterImplementationException
&) {
234 OSL_ENSURE( 0, "register implementation failed" );
237 // getImplementations() check
238 Sequence
<OUString
> seqImpl
= xInst
->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2
);
239 OSL_ENSURE( seqImpl
.getLength() == 1, "count of implementantions is wrong" );
240 OSL_ENSURE( seqImpl
.getConstArray()[0] == "com.sun.star.DummyService.V10", "implementation name is not equal" );
243 // tests, if a service provider can be instantiated.
245 Reference
< XInterface
> xIFace(xSMgr
->createInstance("com.sun.star.ts.TestManagerImpl"));
246 OSL_ENSURE( xIFace
.is(), "loadable service not found" );
248 // remove the service
249 OSL_VERIFY( xInst
->revokeImplementation(atUModule2
, Reference
< XSimpleRegistry
> ()) );
252 Reference
<XComponent
> xComp(xSMgr
, UNO_QUERY
);
264 sal_Bool SAL_CALL
component_writeInfo(
265 void * /*pServiceManager*/, void * pRegistryKey
)
271 Reference
< XRegistryKey
> xNewKey(
272 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
273 OUString( "/" IMPLEMENTATION_NAME
"/UNO/SERVICES" ) ) );
275 const Sequence
< OUString
> & rSNL
=
276 Test_Manager_Impl::getSupportedServiceNames_Static();
277 const OUString
* pArray
= rSNL
.getConstArray();
278 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
279 xNewKey
->createKey( pArray
[nPos
] );
283 catch (InvalidRegistryException
&)
285 OSL_FAIL( "### InvalidRegistryException!" );
291 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
292 const sal_Char
* pImplName
, void * pServiceManager
, void * /*pRegistryKey*/ )
296 if (rtl_str_compare( pImplName
, IMPLEMENTATION_NAME
) == 0)
298 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
299 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
300 OUString( IMPLEMENTATION_NAME
),
301 Test_Manager_Impl_CreateInstance
,
302 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
307 pRet
= xFactory
.get();
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */