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