xref: /aoo42x/main/sal/test/unloading/samplelib1.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 
32 #include <sal/types.h>
33 #include <osl/time.h>
34 #include <rtl/ustring.hxx>
35 #include <uno/environment.h>
36 #include <cppu/macros.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <com/sun/star/uno/Reference.hxx>
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 #include <rtl/unload.h>
42 #include <rtl/string.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 #include <com/sun/star/registry/XRegistryKey.hpp>
47 
48 using namespace ::com::sun::star::registry;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::rtl;
52 using namespace ::cppu;
53 
54 #define IMPLNAME1 "com.sun.star.comp.sal.UnloadingTest1"
55 #define SERVICENAME1 "com.sun.star.UnloadingTest1"
56 #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest2"
57 #define SERVICENAME2 "com.sun.star.UnloadingTest2"
58 #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest3"
59 #define SERVICENAME3 "com.sun.star.UnloadingTest3"
60 #define IMPLNAME4 "com.sun.star.comp.sal.OneInstanceTest"
61 #define SERVICENAME4 "com.sun.star.OneInstanceTest"
62 
63 
64 // Unloading Support ----------------------------------------------
65 rtl_StandardModuleCount globalModuleCount= MODULE_COUNT_INIT;
66 //rtl_StandardModuleCount globalModuleCount= { {rtl_moduleCount_acquire,rtl_moduleCount_release}, rtl_moduleCount_canUnload,0,{0,0}}; //, 0, {0, 0}};
67 // Services -------------------------------------------------------
68 class TestService: public WeakImplHelper1<XServiceInfo>
69 {
70 OUString m_implName;
71 OUString m_serviceName;
72 public:
73 	TestService( OUString implName, OUString serviceName);
74 	~TestService();
75 	virtual OUString SAL_CALL getImplementationName(  )  throw (RuntimeException);
76     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
77     virtual Sequence<OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
78 };
79 
80 TestService::TestService( OUString implName, OUString serviceName):
81 			m_implName(implName),m_serviceName(serviceName)
82 {	// Library unloading support
83 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
84 }
85 
86 TestService::~TestService()
87 {	// Library unloading support
88 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
89 }
90 
91 OUString SAL_CALL TestService::getImplementationName(  )  throw (RuntimeException)
92 {
93 	return m_implName;
94 }
95 sal_Bool SAL_CALL TestService::supportsService( const OUString& ServiceName ) throw (RuntimeException)
96 {
97 	return ServiceName.equals( m_serviceName);
98 }
99 Sequence<OUString > SAL_CALL TestService::getSupportedServiceNames(  ) throw (RuntimeException)
100 {
101 	return Sequence<OUString>( &m_serviceName, 1);
102 }
103 
104 
105 // Creator functions for Services -------------------------------------------------
106 static Reference<XInterface> SAL_CALL test1_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
107 		throw (RuntimeException)
108 {
109 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
110 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1)),
111 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)) )), UNO_QUERY);
112 }
113 static Reference<XInterface> SAL_CALL test2_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
114 		throw (RuntimeException)
115 {
116 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
117 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2)),
118 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)) )), UNO_QUERY);
119 }
120 static Reference<XInterface> SAL_CALL test3_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
121 		throw (RuntimeException)
122 {
123 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
124 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3)),
125 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)) )), UNO_QUERY);
126 }
127 static Reference<XInterface> SAL_CALL test4_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
128 		throw (RuntimeException)
129 {
130 	return Reference<XInterface>( static_cast<XWeak*>( new TestService(
131 		OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4)),
132 		OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)) )), UNO_QUERY);
133 }
134 
135 
136 // Standard UNO library interface -------------------------------------------------
137 extern "C" {
138 	void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv){
139 		*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
140 	}
141 
142 	sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey) throw()
143 	{
144 		if (pRegistryKey)
145 		{
146 			try
147 			{
148 				Reference< XRegistryKey > xNewKey(
149 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
150 						OUString::createFromAscii( "/" IMPLNAME1 "/UNO/SERVICES" ) ) );
151 
152 				xNewKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)));
153 
154 				xNewKey=
155 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
156 						OUString::createFromAscii( "/" IMPLNAME2 "/UNO/SERVICES" ) );
157 
158 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)));
159 				xNewKey=
160 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
161 						OUString::createFromAscii( "/" IMPLNAME3 "/UNO/SERVICES" )   );
162 
163 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)));
164 
165 				xNewKey=
166 					reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
167 						OUString::createFromAscii( "/" IMPLNAME4 "/UNO/SERVICES" )   );
168 
169 				xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)));
170 				return sal_True;
171 			}
172 			catch (InvalidRegistryException &)
173 			{
174 				OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
175 			}
176 		}
177 		return sal_False;
178 	}
179 
180 	void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey) throw()
181 	{
182 		void * pRet = 0;
183 
184 
185 		OUString implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1) );
186 		OUString serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1) );
187 		OUString implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2) );
188 		OUString serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2) );
189 		OUString implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3) );
190 		OUString serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3) );
191 		OUString implname4( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4) );
192 		OUString serviceName4( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4) );
193 
194 		if (implname1.equals( OUString::createFromAscii(pImplName)))
195 		{
196 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
197 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
198 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
199 				implname1,
200 				test1_createInstance,
201 				Sequence<OUString>( &serviceName1, 1),
202 				&globalModuleCount.modCnt
203 				));
204 
205 			if (xFactory.is())
206 			{
207 				xFactory->acquire();
208 				pRet = xFactory.get();
209 			}
210 		}
211 		else if( implname2.equals( OUString::createFromAscii(pImplName)))
212 		{
213 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
214 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
215 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
216 				implname2,
217 				test2_createInstance,
218 				Sequence<OUString>( &serviceName2, 1),
219 				&globalModuleCount.modCnt
220 				));
221 
222 			if (xFactory.is())
223 			{
224 				xFactory->acquire();
225 				pRet = xFactory.get();
226 			}
227 		}
228 		else if( implname3.equals( OUString::createFromAscii(pImplName)))
229 		{
230 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
231 			Reference<XSingleServiceFactory> xFactory( createSingleFactory(
232 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
233 				implname3,
234 				test3_createInstance,
235 				Sequence<OUString>( &serviceName3, 1),
236 				&globalModuleCount.modCnt
237 				));
238 
239 			if (xFactory.is())
240 			{
241 				xFactory->acquire();
242 				pRet = xFactory.get();
243 			}
244 		}
245 		else if( implname4.equals( OUString::createFromAscii(pImplName)))
246 		{
247 			Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
248 			Reference<XSingleServiceFactory> xFactory( createOneInstanceFactory(
249 				reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
250 				implname3,
251 				test4_createInstance,
252 				Sequence<OUString>( &serviceName3, 1),
253 				&globalModuleCount.modCnt
254 				));
255 
256 			if (xFactory.is())
257 			{
258 				xFactory->acquire();
259 				pRet = xFactory.get();
260 			}
261 		}
262 		return pRet;
263 	}
264 
265 	sal_Bool component_canUnload( TimeValue* libUnused)
266 	{
267 		return globalModuleCount.canUnload( &globalModuleCount, libUnused);
268 	}
269 }
270