xref: /aoo41x/main/sal/test/unloading/samplelib1.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_sal.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <sal/types.h>
29cdf0e10cSrcweir #include <osl/time.h>
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <uno/environment.h>
32cdf0e10cSrcweir #include <cppu/macros.hxx>
33cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
34cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
35cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
36cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37cdf0e10cSrcweir #include <rtl/unload.h>
38cdf0e10cSrcweir #include <rtl/string.hxx>
39cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
42cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::com::sun::star::registry;
45cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46cdf0e10cSrcweir using namespace ::com::sun::star::lang;
47cdf0e10cSrcweir using namespace ::rtl;
48cdf0e10cSrcweir using namespace ::cppu;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #define IMPLNAME1 "com.sun.star.comp.sal.UnloadingTest1"
51cdf0e10cSrcweir #define SERVICENAME1 "com.sun.star.UnloadingTest1"
52cdf0e10cSrcweir #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest2"
53cdf0e10cSrcweir #define SERVICENAME2 "com.sun.star.UnloadingTest2"
54cdf0e10cSrcweir #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest3"
55cdf0e10cSrcweir #define SERVICENAME3 "com.sun.star.UnloadingTest3"
56cdf0e10cSrcweir #define IMPLNAME4 "com.sun.star.comp.sal.OneInstanceTest"
57cdf0e10cSrcweir #define SERVICENAME4 "com.sun.star.OneInstanceTest"
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir // Unloading Support ----------------------------------------------
61cdf0e10cSrcweir rtl_StandardModuleCount globalModuleCount= MODULE_COUNT_INIT;
62cdf0e10cSrcweir //rtl_StandardModuleCount globalModuleCount= { {rtl_moduleCount_acquire,rtl_moduleCount_release}, rtl_moduleCount_canUnload,0,{0,0}}; //, 0, {0, 0}};
63cdf0e10cSrcweir // Services -------------------------------------------------------
64cdf0e10cSrcweir class TestService: public WeakImplHelper1<XServiceInfo>
65cdf0e10cSrcweir {
66cdf0e10cSrcweir OUString m_implName;
67cdf0e10cSrcweir OUString m_serviceName;
68cdf0e10cSrcweir public:
69cdf0e10cSrcweir 	TestService( OUString implName, OUString serviceName);
70cdf0e10cSrcweir 	~TestService();
71cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName(  )  throw (RuntimeException);
72cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
73cdf0e10cSrcweir     virtual Sequence<OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
74cdf0e10cSrcweir };
75cdf0e10cSrcweir 
TestService(OUString implName,OUString serviceName)76cdf0e10cSrcweir TestService::TestService( OUString implName, OUString serviceName):
77cdf0e10cSrcweir 			m_implName(implName),m_serviceName(serviceName)
78cdf0e10cSrcweir {	// Library unloading support
79cdf0e10cSrcweir 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
~TestService()82cdf0e10cSrcweir TestService::~TestService()
83cdf0e10cSrcweir {	// Library unloading support
84cdf0e10cSrcweir 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
getImplementationName()87cdf0e10cSrcweir OUString SAL_CALL TestService::getImplementationName(  )  throw (RuntimeException)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	return m_implName;
90cdf0e10cSrcweir }
supportsService(const OUString & ServiceName)91cdf0e10cSrcweir sal_Bool SAL_CALL TestService::supportsService( const OUString& ServiceName ) throw (RuntimeException)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	return ServiceName.equals( m_serviceName);
94cdf0e10cSrcweir }
getSupportedServiceNames()95cdf0e10cSrcweir Sequence<OUString > SAL_CALL TestService::getSupportedServiceNames(  ) throw (RuntimeException)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	return Sequence<OUString>( &m_serviceName, 1);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir // Creator functions for Services -------------------------------------------------
test1_createInstance(const Reference<XMultiServiceFactory> & rSMgr)102cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test1_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
103cdf0e10cSrcweir 		throw (RuntimeException)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
106cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1)),
107cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)) )), UNO_QUERY);
108cdf0e10cSrcweir }
test2_createInstance(const Reference<XMultiServiceFactory> & rSMgr)109cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test2_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
110cdf0e10cSrcweir 		throw (RuntimeException)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
113cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2)),
114cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)) )), UNO_QUERY);
115cdf0e10cSrcweir }
test3_createInstance(const Reference<XMultiServiceFactory> & rSMgr)116cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test3_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
117cdf0e10cSrcweir 		throw (RuntimeException)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
120cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3)),
121cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)) )), UNO_QUERY);
122cdf0e10cSrcweir }
test4_createInstance(const Reference<XMultiServiceFactory> & rSMgr)123cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test4_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
124cdf0e10cSrcweir 		throw (RuntimeException)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
127cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4)),
128cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)) )), UNO_QUERY);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // Standard UNO library interface -------------------------------------------------
133cdf0e10cSrcweir extern "C" {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)134cdf0e10cSrcweir 	void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv){
135cdf0e10cSrcweir 		*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 
component_writeInfo(void * pServiceManager,void * pRegistryKey)138cdf0e10cSrcweir 	sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey) throw()
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir 		if (pRegistryKey)
141cdf0e10cSrcweir 		{
142cdf0e10cSrcweir 			try
143cdf0e10cSrcweir 			{
144cdf0e10cSrcweir 				Reference< XRegistryKey > xNewKey(
145cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
146cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME1 "/UNO/SERVICES" ) ) );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 				xNewKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)));
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 				xNewKey=
151cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
152cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME2 "/UNO/SERVICES" ) );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)));
155cdf0e10cSrcweir 				xNewKey=
156cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
157cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME3 "/UNO/SERVICES" )   );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)));
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 				xNewKey=
162cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
163cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME4 "/UNO/SERVICES" )   );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)));
166cdf0e10cSrcweir 				return sal_True;
167cdf0e10cSrcweir 			}
168cdf0e10cSrcweir 			catch (InvalidRegistryException &)
169cdf0e10cSrcweir 			{
170cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
171cdf0e10cSrcweir 			}
172cdf0e10cSrcweir 		}
173cdf0e10cSrcweir 		return sal_False;
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)176cdf0e10cSrcweir 	void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey) throw()
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 		void * pRet = 0;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 		OUString implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1) );
182cdf0e10cSrcweir 		OUString serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1) );
183cdf0e10cSrcweir 		OUString implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2) );
184cdf0e10cSrcweir 		OUString serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2) );
185cdf0e10cSrcweir 		OUString implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3) );
186cdf0e10cSrcweir 		OUString serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3) );
187cdf0e10cSrcweir 		OUString implname4( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4) );
188cdf0e10cSrcweir 		OUString serviceName4( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4) );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 		if (implname1.equals( OUString::createFromAscii(pImplName)))
191cdf0e10cSrcweir 		{
192cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
193cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
194cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
195cdf0e10cSrcweir 				implname1,
196cdf0e10cSrcweir 				test1_createInstance,
197cdf0e10cSrcweir 				Sequence<OUString>( &serviceName1, 1),
198cdf0e10cSrcweir 				&globalModuleCount.modCnt
199cdf0e10cSrcweir 				));
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 			if (xFactory.is())
202cdf0e10cSrcweir 			{
203cdf0e10cSrcweir 				xFactory->acquire();
204cdf0e10cSrcweir 				pRet = xFactory.get();
205cdf0e10cSrcweir 			}
206cdf0e10cSrcweir 		}
207cdf0e10cSrcweir 		else if( implname2.equals( OUString::createFromAscii(pImplName)))
208cdf0e10cSrcweir 		{
209cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
210cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
211cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
212cdf0e10cSrcweir 				implname2,
213cdf0e10cSrcweir 				test2_createInstance,
214cdf0e10cSrcweir 				Sequence<OUString>( &serviceName2, 1),
215cdf0e10cSrcweir 				&globalModuleCount.modCnt
216cdf0e10cSrcweir 				));
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 			if (xFactory.is())
219cdf0e10cSrcweir 			{
220cdf0e10cSrcweir 				xFactory->acquire();
221cdf0e10cSrcweir 				pRet = xFactory.get();
222cdf0e10cSrcweir 			}
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 		else if( implname3.equals( OUString::createFromAscii(pImplName)))
225cdf0e10cSrcweir 		{
226cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
227cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
228cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
229cdf0e10cSrcweir 				implname3,
230cdf0e10cSrcweir 				test3_createInstance,
231cdf0e10cSrcweir 				Sequence<OUString>( &serviceName3, 1),
232cdf0e10cSrcweir 				&globalModuleCount.modCnt
233cdf0e10cSrcweir 				));
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			if (xFactory.is())
236cdf0e10cSrcweir 			{
237cdf0e10cSrcweir 				xFactory->acquire();
238cdf0e10cSrcweir 				pRet = xFactory.get();
239cdf0e10cSrcweir 			}
240cdf0e10cSrcweir 		}
241cdf0e10cSrcweir 		else if( implname4.equals( OUString::createFromAscii(pImplName)))
242cdf0e10cSrcweir 		{
243cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
244cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createOneInstanceFactory(
245cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
246cdf0e10cSrcweir 				implname3,
247cdf0e10cSrcweir 				test4_createInstance,
248cdf0e10cSrcweir 				Sequence<OUString>( &serviceName3, 1),
249cdf0e10cSrcweir 				&globalModuleCount.modCnt
250cdf0e10cSrcweir 				));
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 			if (xFactory.is())
253cdf0e10cSrcweir 			{
254cdf0e10cSrcweir 				xFactory->acquire();
255cdf0e10cSrcweir 				pRet = xFactory.get();
256cdf0e10cSrcweir 			}
257cdf0e10cSrcweir 		}
258cdf0e10cSrcweir 		return pRet;
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 
component_canUnload(TimeValue * libUnused)261cdf0e10cSrcweir 	sal_Bool component_canUnload( TimeValue* libUnused)
262cdf0e10cSrcweir 	{
263cdf0e10cSrcweir 		return globalModuleCount.canUnload( &globalModuleCount, libUnused);
264cdf0e10cSrcweir 	}
265cdf0e10cSrcweir }
266