xref: /aoo41x/main/sal/test/unloading/samplelib2.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.UnloadingTest21"
51cdf0e10cSrcweir #define SERVICENAME1 "com.sun.star.UnloadingTest21"
52cdf0e10cSrcweir #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest22"
53cdf0e10cSrcweir #define SERVICENAME2 "com.sun.star.UnloadingTest22"
54cdf0e10cSrcweir #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest23"
55cdf0e10cSrcweir #define SERVICENAME3 "com.sun.star.UnloadingTest23"
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // Unloading Support ----------------------------------------------
58cdf0e10cSrcweir rtl_StandardModuleCount globalModuleCount= MODULE_COUNT_INIT;
59cdf0e10cSrcweir //rtl_StandardModuleCount globalModuleCount= { {rtl_moduleCount_acquire,rtl_moduleCount_release}, rtl_moduleCount_canUnload,0,{0,0}}; //, 0, {0, 0}};
60cdf0e10cSrcweir // Services -------------------------------------------------------
61cdf0e10cSrcweir class TestService: public WeakImplHelper1<XServiceInfo>
62cdf0e10cSrcweir {
63cdf0e10cSrcweir OUString m_implName;
64cdf0e10cSrcweir OUString m_serviceName;
65cdf0e10cSrcweir public:
66cdf0e10cSrcweir 	TestService( OUString implName, OUString serviceName);
67cdf0e10cSrcweir 	~TestService();
68cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName(  )  throw (RuntimeException);
69cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
70cdf0e10cSrcweir     virtual Sequence<OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
71cdf0e10cSrcweir };
72cdf0e10cSrcweir 
TestService(OUString implName,OUString serviceName)73cdf0e10cSrcweir TestService::TestService( OUString implName, OUString serviceName):
74cdf0e10cSrcweir 			m_implName(implName),m_serviceName(serviceName)
75cdf0e10cSrcweir {	// Library unloading support
76cdf0e10cSrcweir 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
~TestService()79cdf0e10cSrcweir TestService::~TestService()
80cdf0e10cSrcweir {	// Library unloading support
81cdf0e10cSrcweir 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
getImplementationName()84cdf0e10cSrcweir OUString SAL_CALL TestService::getImplementationName(  )  throw (RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	return m_implName;
87cdf0e10cSrcweir }
supportsService(const OUString & ServiceName)88cdf0e10cSrcweir sal_Bool SAL_CALL TestService::supportsService( const OUString& ServiceName ) throw (RuntimeException)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	return ServiceName.equals( m_serviceName);
91cdf0e10cSrcweir }
getSupportedServiceNames()92cdf0e10cSrcweir Sequence<OUString > SAL_CALL TestService::getSupportedServiceNames(  ) throw (RuntimeException)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	return Sequence<OUString>( &m_serviceName, 1);
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 
98cdf0e10cSrcweir // Creator functions for Services -------------------------------------------------
test21_createInstance(const Reference<XMultiServiceFactory> & rSMgr)99cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test21_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
100cdf0e10cSrcweir 		throw (RuntimeException)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
103cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1)),
104cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)) )), UNO_QUERY);
105cdf0e10cSrcweir }
test22_createInstance(const Reference<XMultiServiceFactory> & rSMgr)106cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test22_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
107cdf0e10cSrcweir 		throw (RuntimeException)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
110cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2)),
111cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)) )), UNO_QUERY);
112cdf0e10cSrcweir }
test23_createInstance(const Reference<XMultiServiceFactory> & rSMgr)113cdf0e10cSrcweir static Reference<XInterface> SAL_CALL test23_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
114cdf0e10cSrcweir 		throw (RuntimeException)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
117cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3)),
118cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)) )), UNO_QUERY);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
122cdf0e10cSrcweir // Standard UNO library interface -------------------------------------------------
123cdf0e10cSrcweir extern "C" {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)124cdf0e10cSrcweir 	void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv){
125cdf0e10cSrcweir 		*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir 
component_writeInfo(void * pServiceManager,void * pRegistryKey)128cdf0e10cSrcweir 	sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey) throw()
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		if (pRegistryKey)
131cdf0e10cSrcweir 		{
132cdf0e10cSrcweir 			try
133cdf0e10cSrcweir 			{
134cdf0e10cSrcweir 				Reference< XRegistryKey > xNewKey(
135cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
136cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME1 "/UNO/SERVICES" ) ) );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 				xNewKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)));
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 				xNewKey=
141cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
142cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME2 "/UNO/SERVICES" ) );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)));
145cdf0e10cSrcweir 				xNewKey=
146cdf0e10cSrcweir 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
147cdf0e10cSrcweir 						OUString::createFromAscii( "/" IMPLNAME3 "/UNO/SERVICES" )   );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)));
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 				return sal_True;
152cdf0e10cSrcweir 			}
153cdf0e10cSrcweir 			catch (InvalidRegistryException &)
154cdf0e10cSrcweir 			{
155cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
156cdf0e10cSrcweir 			}
157cdf0e10cSrcweir 		}
158cdf0e10cSrcweir 		return sal_False;
159cdf0e10cSrcweir 	}
160cdf0e10cSrcweir 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)161cdf0e10cSrcweir 	void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey) throw()
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		void * pRet = 0;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		OUString implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1) );
167cdf0e10cSrcweir 		OUString serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1) );
168cdf0e10cSrcweir 		OUString implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2) );
169cdf0e10cSrcweir 		OUString serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2) );
170cdf0e10cSrcweir 		OUString implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3) );
171cdf0e10cSrcweir 		OUString serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3) );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		if (implname1.equals( OUString::createFromAscii(pImplName)))
174cdf0e10cSrcweir 		{
175cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
176cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
177cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
178cdf0e10cSrcweir 				implname1,
179cdf0e10cSrcweir 				test21_createInstance,
180cdf0e10cSrcweir 				Sequence<OUString>( &serviceName1, 1),
181cdf0e10cSrcweir 				&globalModuleCount.modCnt
182cdf0e10cSrcweir 				));
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			if (xFactory.is())
185cdf0e10cSrcweir 			{
186cdf0e10cSrcweir 				xFactory->acquire();
187cdf0e10cSrcweir 				pRet = xFactory.get();
188cdf0e10cSrcweir 			}
189cdf0e10cSrcweir 		}
190cdf0e10cSrcweir 		else if( implname2.equals( OUString::createFromAscii(pImplName)))
191cdf0e10cSrcweir 		{
192cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
193cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
194cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
195cdf0e10cSrcweir 				implname2,
196cdf0e10cSrcweir 				test22_createInstance,
197cdf0e10cSrcweir 				Sequence<OUString>( &serviceName2, 1),
198cdf0e10cSrcweir 				&globalModuleCount.modCnt
199cdf0e10cSrcweir 				));
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 			if (xFactory.is())
202cdf0e10cSrcweir 			{
203cdf0e10cSrcweir 				xFactory->acquire();
204cdf0e10cSrcweir 				pRet = xFactory.get();
205cdf0e10cSrcweir 			}
206cdf0e10cSrcweir 		}
207cdf0e10cSrcweir 		else if( implname3.equals( OUString::createFromAscii(pImplName)))
208cdf0e10cSrcweir 		{
209cdf0e10cSrcweir 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
210cdf0e10cSrcweir 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
211cdf0e10cSrcweir 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
212cdf0e10cSrcweir 				implname3,
213cdf0e10cSrcweir 				test23_createInstance,
214cdf0e10cSrcweir 				Sequence<OUString>( &serviceName3, 1),
215cdf0e10cSrcweir 				&globalModuleCount.modCnt
216cdf0e10cSrcweir 				));
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 			if (xFactory.is())
219cdf0e10cSrcweir 			{
220cdf0e10cSrcweir 				xFactory->acquire();
221cdf0e10cSrcweir 				pRet = xFactory.get();
222cdf0e10cSrcweir 			}
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		return pRet;
226cdf0e10cSrcweir 	}
227cdf0e10cSrcweir 
component_canUnload(TimeValue * libUnused)228cdf0e10cSrcweir 	sal_Bool component_canUnload( TimeValue* libUnused)
229cdf0e10cSrcweir 	{
230cdf0e10cSrcweir 		return globalModuleCount.canUnload( &globalModuleCount, libUnused);
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir }
233