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: samplelib2.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.UnloadingTest21"
58 #define SERVICENAME1 "com.sun.star.UnloadingTest21"
59 #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest22"
60 #define SERVICENAME2 "com.sun.star.UnloadingTest22"
61 #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest23"
62 #define SERVICENAME3 "com.sun.star.UnloadingTest23"
64 // Unloading Support ----------------------------------------------
65 rtl_StandardModuleCount globalModuleCount
= MODULE_COUNT_INIT
;
66 //rtl_StandardModuleCount globalModuleCount= { {rtl_moduleCount_acquire,rtl_moduleCount_release}, rtl_moduleCount_canUnload,0,{0,0}}; //, 0, {0, 0}};
67 // Services -------------------------------------------------------
68 class TestService
: public WeakImplHelper1
<XServiceInfo
>
71 OUString m_serviceName
;
73 TestService( OUString implName
, OUString serviceName
);
75 virtual OUString SAL_CALL
getImplementationName( ) throw (RuntimeException
);
76 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (RuntimeException
);
77 virtual Sequence
<OUString
> SAL_CALL
getSupportedServiceNames( ) throw (RuntimeException
);
80 TestService::TestService( OUString implName
, OUString serviceName
):
81 m_implName(implName
),m_serviceName(serviceName
)
82 { // Library unloading support
83 globalModuleCount
.modCnt
.acquire( &globalModuleCount
.modCnt
);
86 TestService::~TestService()
87 { // Library unloading support
88 globalModuleCount
.modCnt
.release( &globalModuleCount
.modCnt
);
91 OUString SAL_CALL
TestService::getImplementationName( ) throw (RuntimeException
)
95 sal_Bool SAL_CALL
TestService::supportsService( const OUString
& ServiceName
) throw (RuntimeException
)
97 return ServiceName
.equals( m_serviceName
);
99 Sequence
<OUString
> SAL_CALL
TestService::getSupportedServiceNames( ) throw (RuntimeException
)
101 return Sequence
<OUString
>( &m_serviceName
, 1);
105 // Creator functions for Services -------------------------------------------------
106 static Reference
<XInterface
> SAL_CALL
test21_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
107 throw (RuntimeException
)
109 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
110 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1
)),
111 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
)) )), UNO_QUERY
);
113 static Reference
<XInterface
> SAL_CALL
test22_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
114 throw (RuntimeException
)
116 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
117 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2
)),
118 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
)) )), UNO_QUERY
);
120 static Reference
<XInterface
> SAL_CALL
test23_createInstance(const Reference
<XMultiServiceFactory
> & rSMgr
)
121 throw (RuntimeException
)
123 return Reference
<XInterface
>( static_cast<XWeak
*>( new TestService(
124 OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3
)),
125 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
)) )), UNO_QUERY
);
129 // Standard UNO library interface -------------------------------------------------
131 void SAL_CALL
component_getImplementationEnvironment(const sal_Char
** ppEnvTypeName
, uno_Environment
** ppEnv
){
132 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
135 sal_Bool SAL_CALL
component_writeInfo(void * pServiceManager
, void * pRegistryKey
) throw()
141 Reference
< XRegistryKey
> xNewKey(
142 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
143 OUString::createFromAscii( "/" IMPLNAME1
"/UNO/SERVICES" ) ) );
145 xNewKey
->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
)));
148 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
149 OUString::createFromAscii( "/" IMPLNAME2
"/UNO/SERVICES" ) );
151 xNewKey
->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
)));
153 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
154 OUString::createFromAscii( "/" IMPLNAME3
"/UNO/SERVICES" ) );
156 xNewKey
->createKey(OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
)));
160 catch (InvalidRegistryException
&)
162 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
168 void * SAL_CALL
component_getFactory(const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
) throw()
173 OUString
implname1( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME1
) );
174 OUString
serviceName1( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME1
) );
175 OUString
implname2( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME2
) );
176 OUString
serviceName2( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME2
) );
177 OUString
implname3( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME3
) );
178 OUString
serviceName3( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME3
) );
180 if (implname1
.equals( OUString::createFromAscii(pImplName
)))
182 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
183 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
184 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
186 test21_createInstance
,
187 Sequence
<OUString
>( &serviceName1
, 1),
188 &globalModuleCount
.modCnt
194 pRet
= xFactory
.get();
197 else if( implname2
.equals( OUString::createFromAscii(pImplName
)))
199 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
200 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
201 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
203 test22_createInstance
,
204 Sequence
<OUString
>( &serviceName2
, 1),
205 &globalModuleCount
.modCnt
211 pRet
= xFactory
.get();
214 else if( implname3
.equals( OUString::createFromAscii(pImplName
)))
216 Reference
<XMultiServiceFactory
> mgr
= reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
);
217 Reference
<XSingleServiceFactory
> xFactory( createSingleFactory(
218 reinterpret_cast<XMultiServiceFactory
*>(pServiceManager
),
220 test23_createInstance
,
221 Sequence
<OUString
>( &serviceName3
, 1),
222 &globalModuleCount
.modCnt
228 pRet
= xFactory
.get();
235 sal_Bool
component_canUnload( TimeValue
* libUnused
)
237 return globalModuleCount
.canUnload( &globalModuleCount
, libUnused
);