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