1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: samplelib1.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
35 #include <sal/types.h>
37 #include <rtl/ustring.hxx>
38 #include <uno/environment.h>
39 #include <cppu/macros.hxx>
40 #include <cppuhelper/factory.hxx>
41 #include <com/sun/star/uno/Reference.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 #include <rtl/unload.h>
45 #include <rtl/string.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
48 #include <com/sun/star/lang/XServiceInfo.hpp>
49 #include <com/sun/star/registry/XRegistryKey.hpp>
51 using namespace ::com::sun::star::registry
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::rtl
;
55 using namespace ::cppu
;
57 #define IMPLNAME1 "com.sun.star.comp.sal.UnloadingTest1"
58 #define SERVICENAME1 "com.sun.star.UnloadingTest1"
59 #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest2"
60 #define SERVICENAME2 "com.sun.star.UnloadingTest2"
61 #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest3"
62 #define SERVICENAME3 "com.sun.star.UnloadingTest3"
63 #define IMPLNAME4 "com.sun.star.comp.sal.OneInstanceTest"
64 #define SERVICENAME4 "com.sun.star.OneInstanceTest"
67 // Unloading Support ----------------------------------------------
68 rtl_StandardModuleCount globalModuleCount
= MODULE_COUNT_INIT
;
69 //rtl_StandardModuleCount globalModuleCount= { {rtl_moduleCount_acquire,rtl_moduleCount_release}, rtl_moduleCount_canUnload,0,{0,0}}; //, 0, {0, 0}};
70 // Services -------------------------------------------------------
71 class TestService
: public WeakImplHelper1
<XServiceInfo
>
74 OUString m_serviceName
;
76 TestService( OUString implName
, OUString serviceName
);
78 virtual OUString SAL_CALL
getImplementationName( ) throw (RuntimeException
);
79 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (RuntimeException
);
80 virtual Sequence
<OUString
> SAL_CALL
getSupportedServiceNames( ) throw (RuntimeException
);
83 TestService::TestService( OUString implName
, OUString serviceName
):
84 m_implName(implName
),m_serviceName(serviceName
)
85 { // Library unloading support
86 globalModuleCount
.modCnt
.acquire( &globalModuleCount
.modCnt
);
89 TestService::~TestService()
90 { // Library unloading support
91 globalModuleCount
.modCnt
.release( &globalModuleCount
.modCnt
);
94 OUString SAL_CALL
TestService::getImplementationName( ) throw (RuntimeException
)
98 sal_Bool SAL_CALL
TestService::supportsService( const OUString
& ServiceName
) throw (RuntimeException
)
100 return ServiceName
.equals( m_serviceName
);
102 Sequence
<OUString
> SAL_CALL
TestService::getSupportedServiceNames( ) throw (RuntimeException
)
104 return Sequence
<OUString
>( &m_serviceName
, 1);
108 // Creator functions for Services -------------------------------------------------
109 static Reference
<XInterface
> SAL_CALL
test1_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
110 throw (RuntimeException
)
112 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
113 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1
)),
114 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
)) )), UNO_QUERY
);
116 static Reference
<XInterface
> SAL_CALL
test2_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
117 throw (RuntimeException
)
119 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
120 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2
)),
121 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
)) )), UNO_QUERY
);
123 static Reference
<XInterface
> SAL_CALL
test3_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
124 throw (RuntimeException
)
126 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
127 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3
)),
128 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
)) )), UNO_QUERY
);
130 static Reference
<XInterface
> SAL_CALL
test4_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
131 throw (RuntimeException
)
133 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
134 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4
)),
135 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4
)) )), UNO_QUERY
);
139 // Standard UNO library interface -------------------------------------------------
141 void SAL_CALL
component_getImplementationEnvironment(const sal_Char
** ppEnvTypeName
, uno_Environment
** ppEnv
){
142 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
145 sal_Bool SAL_CALL
component_writeInfo(void * pServiceManager
, void * pRegistryKey
) throw()
151 Reference
< XRegistryKey
> xNewKey(
152 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
153 OUString::createFromAscii( "/" IMPLNAME1
"/UNO/SERVICES" ) ) );
155 xNewKey
->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
)));
158 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
159 OUString::createFromAscii( "/" IMPLNAME2
"/UNO/SERVICES" ) );
161 xNewKey
->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
)));
163 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
164 OUString::createFromAscii( "/" IMPLNAME3
"/UNO/SERVICES" ) );
166 xNewKey
->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
)));
169 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
170 OUString::createFromAscii( "/" IMPLNAME4
"/UNO/SERVICES" ) );
172 xNewKey
->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4
)));
175 catch (InvalidRegistryException
&)
177 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
183 void * SAL_CALL
component_getFactory(const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
) throw()
188 OUString
implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1
) );
189 OUString
serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
) );
190 OUString
implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2
) );
191 OUString
serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
) );
192 OUString
implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3
) );
193 OUString
serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
) );
194 OUString
implname4( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME4
) );
195 OUString
serviceName4( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4
) );
197 if (implname1
.equals( OUString::createFromAscii(pImplName
)))
199 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
200 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
201 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
203 test1_createInstance
,
204 Sequence
<OUString
>( &serviceName1
, 1),
205 &globalModuleCount
.modCnt
211 pRet
= xFactory
.get();
214 else if( implname2
.equals( OUString::createFromAscii(pImplName
)))
216 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
217 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
218 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
220 test2_createInstance
,
221 Sequence
<OUString
>( &serviceName2
, 1),
222 &globalModuleCount
.modCnt
228 pRet
= xFactory
.get();
231 else if( implname3
.equals( OUString::createFromAscii(pImplName
)))
233 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
234 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
235 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
237 test3_createInstance
,
238 Sequence
<OUString
>( &serviceName3
, 1),
239 &globalModuleCount
.modCnt
245 pRet
= xFactory
.get();
248 else if( implname4
.equals( OUString::createFromAscii(pImplName
)))
250 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
251 Reference
<XSingleServiceFactory
> xFactory( createOneInstanceFactory(
252 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
254 test4_createInstance
,
255 Sequence
<OUString
>( &serviceName3
, 1),
256 &globalModuleCount
.modCnt
262 pRet
= xFactory
.get();
268 sal_Bool
component_canUnload( TimeValue
* libUnused
)
270 return globalModuleCount
.canUnload( &globalModuleCount
, libUnused
);