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