xref: /AOO41X/main/sal/test/unloading/samplelib1.cxx (revision 87d2adbc9cadf14644c3679b041b9226f7630199)
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(  )  throw (RuntimeException);
72     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
73     virtual Sequence<OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
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(  )  throw (RuntimeException)
88 {
89     return m_implName;
90 }
supportsService(const OUString & ServiceName)91 sal_Bool SAL_CALL TestService::supportsService( const OUString& ServiceName ) throw (RuntimeException)
92 {
93     return ServiceName.equals( m_serviceName);
94 }
getSupportedServiceNames()95 Sequence<OUString > SAL_CALL TestService::getSupportedServiceNames(  ) throw (RuntimeException)
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         throw (RuntimeException)
104 {
105     return Reference<XInterface>( static_cast<XWeak*>( new TestService(
106         OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1)),
107         OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)) )), UNO_QUERY);
108 }
test2_createInstance(const Reference<XMultiServiceFactory> & rSMgr)109 static Reference<XInterface> SAL_CALL test2_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
110         throw (RuntimeException)
111 {
112     return Reference<XInterface>( static_cast<XWeak*>( new TestService(
113         OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2)),
114         OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)) )), UNO_QUERY);
115 }
test3_createInstance(const Reference<XMultiServiceFactory> & rSMgr)116 static Reference<XInterface> SAL_CALL test3_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
117         throw (RuntimeException)
118 {
119     return Reference<XInterface>( static_cast<XWeak*>( new TestService(
120         OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3)),
121         OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)) )), UNO_QUERY);
122 }
test4_createInstance(const Reference<XMultiServiceFactory> & rSMgr)123 static Reference<XInterface> SAL_CALL test4_createInstance(const Reference<XMultiServiceFactory> & rSMgr)
124         throw (RuntimeException)
125 {
126     return Reference<XInterface>( static_cast<XWeak*>( new TestService(
127         OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4)),
128         OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)) )), UNO_QUERY);
129 }
130 
131 
132 // Standard UNO library interface -------------------------------------------------
133 extern "C" {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)134     void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv){
135         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
136     }
137 
component_writeInfo(void * pServiceManager,void * pRegistryKey)138     sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey) throw()
139     {
140         if (pRegistryKey)
141         {
142             try
143             {
144                 Reference< XRegistryKey > xNewKey(
145                     reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
146                         OUString::createFromAscii( "/" IMPLNAME1 "/UNO/SERVICES" ) ) );
147 
148                 xNewKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1)));
149 
150                 xNewKey=
151                     reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
152                         OUString::createFromAscii( "/" IMPLNAME2 "/UNO/SERVICES" ) );
153 
154                 xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2)));
155                 xNewKey=
156                     reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
157                         OUString::createFromAscii( "/" IMPLNAME3 "/UNO/SERVICES" )   );
158 
159                 xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3)));
160 
161                 xNewKey=
162                     reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
163                         OUString::createFromAscii( "/" IMPLNAME4 "/UNO/SERVICES" )   );
164 
165                 xNewKey->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)));
166                 return sal_True;
167             }
168             catch (InvalidRegistryException &)
169             {
170                 OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
171             }
172         }
173         return sal_False;
174     }
175 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)176     void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey) throw()
177     {
178         void * pRet = 0;
179 
180 
181         OUString implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1) );
182         OUString serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1) );
183         OUString implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2) );
184         OUString serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2) );
185         OUString implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3) );
186         OUString serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3) );
187         OUString implname4( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4) );
188         OUString serviceName4( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4) );
189 
190         if (implname1.equals( OUString::createFromAscii(pImplName)))
191         {
192             Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
193             Reference<XSingleServiceFactory> xFactory( createSingleFactory(
194                 reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
195                 implname1,
196                 test1_createInstance,
197                 Sequence<OUString>( &serviceName1, 1),
198                 &globalModuleCount.modCnt
199                 ));
200 
201             if (xFactory.is())
202             {
203                 xFactory->acquire();
204                 pRet = xFactory.get();
205             }
206         }
207         else if( implname2.equals( OUString::createFromAscii(pImplName)))
208         {
209             Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
210             Reference<XSingleServiceFactory> xFactory( createSingleFactory(
211                 reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
212                 implname2,
213                 test2_createInstance,
214                 Sequence<OUString>( &serviceName2, 1),
215                 &globalModuleCount.modCnt
216                 ));
217 
218             if (xFactory.is())
219             {
220                 xFactory->acquire();
221                 pRet = xFactory.get();
222             }
223         }
224         else if( implname3.equals( OUString::createFromAscii(pImplName)))
225         {
226             Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
227             Reference<XSingleServiceFactory> xFactory( createSingleFactory(
228                 reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
229                 implname3,
230                 test3_createInstance,
231                 Sequence<OUString>( &serviceName3, 1),
232                 &globalModuleCount.modCnt
233                 ));
234 
235             if (xFactory.is())
236             {
237                 xFactory->acquire();
238                 pRet = xFactory.get();
239             }
240         }
241         else if( implname4.equals( OUString::createFromAscii(pImplName)))
242         {
243             Reference<XMultiServiceFactory> mgr= reinterpret_cast<XMultiServiceFactory *>(pServiceManager);
244             Reference<XSingleServiceFactory> xFactory( createOneInstanceFactory(
245                 reinterpret_cast<XMultiServiceFactory *>(pServiceManager),
246                 implname3,
247                 test4_createInstance,
248                 Sequence<OUString>( &serviceName3, 1),
249                 &globalModuleCount.modCnt
250                 ));
251 
252             if (xFactory.is())
253             {
254                 xFactory->acquire();
255                 pRet = xFactory.get();
256             }
257         }
258         return pRet;
259     }
260 
component_canUnload(TimeValue * libUnused)261     sal_Bool component_canUnload( TimeValue* libUnused)
262     {
263         return globalModuleCount.canUnload( &globalModuleCount, libUnused);
264     }
265 }
266