1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // starting the executable:
22 // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
23 // -env:UNO_TYPES=cpputest.rdb
29 #include <rtl/strbuf.hxx>
31 #include <cppuhelper/implementationentry.hxx>
32 #include <cppuhelper/bootstrap.hxx>
33 #include <cppuhelper/implbase2.hxx>
34 #include <cppuhelper/supportsservice.hxx>
36 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
37 #include <com/sun/star/lang/XInitialization.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/registry/XImplementationRegistration.hpp>
44 using namespace ::cppu
;
45 using namespace ::rtl
;
46 using namespace ::osl
;
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
54 static Sequence
< OUString
> impl0_getSupportedServiceNames()
56 OUString
str("com.sun.star.bootstrap.TestComponent0");
57 return Sequence
< OUString
>( &str
, 1 );
60 static OUString
impl0_getImplementationName()
62 return OUString("com.sun.star.comp.bootstrap.TestComponent0");
65 static Sequence
< OUString
> impl1_getSupportedServiceNames()
67 OUString
str("com.sun.star.bootstrap.TestComponent1");
68 return Sequence
< OUString
>( &str
, 1 );
71 static OUString
impl1_getImplementationName()
73 return OUString("com.sun.star.comp.bootstrap.TestComponent1");
78 : public WeakImplHelper2
< lang::XServiceInfo
, lang::XInitialization
>
80 Reference
< XComponentContext
> m_xContext
;
83 ServiceImpl0( Reference
< XComponentContext
> const & xContext
) SAL_THROW(());
86 virtual void SAL_CALL
initialize( const Sequence
< Any
>& rArgs
) throw (Exception
, RuntimeException
);
89 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (RuntimeException
);
90 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
);
91 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) throw (RuntimeException
);
94 ServiceImpl0::ServiceImpl0( Reference
< XComponentContext
> const & xContext
) SAL_THROW(())
95 : m_xContext( xContext
)
100 // service properties
101 OSL_VERIFY( m_xContext
->getValueByName(
102 "/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0" ) >>= n
);
103 OSL_VERIFY( n
== 13 );
104 OSL_VERIFY( m_xContext
->getValueByName(
105 "/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1" ) >>= val
);
106 OSL_VERIFY( val
== "value of serviceprop1" );
108 OSL_VERIFY( m_xContext
->getValueByName(
109 "/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0" ) >>= n
);
110 OSL_VERIFY( n
== 15 );
111 OSL_VERIFY( m_xContext
->getValueByName(
112 "/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1" ) >>= val
);
113 OSL_VERIFY( val
== "value of implprop1" );
117 void ServiceImpl0::initialize( const Sequence
< Any
>& rArgs
)
118 throw (Exception
, RuntimeException
)
122 OSL_VERIFY( rArgs
.getLength() == 3 );
123 OSL_VERIFY( rArgs
[ 0 ] >>= val
);
124 OSL_VERIFY( val
== "first argument" );
125 OSL_VERIFY( rArgs
[ 1 ] >>= val
);
126 OSL_VERIFY( val
== "second argument" );
127 OSL_VERIFY( rArgs
[ 2 ] >>= val
);
128 OSL_VERIFY( val
== "third argument" );
132 OUString
ServiceImpl0::getImplementationName()
133 throw(::com::sun::star::uno::RuntimeException
)
135 return impl0_getImplementationName();
138 Sequence
< OUString
> ServiceImpl0::getSupportedServiceNames()
139 throw(::com::sun::star::uno::RuntimeException
)
141 return impl0_getSupportedServiceNames();
144 sal_Bool
ServiceImpl0::supportsService( const OUString
& rServiceName
)
145 throw(::com::sun::star::uno::RuntimeException
)
147 return cppu::supportsService(this, rServiceName
);
151 class ServiceImpl1
: public ServiceImpl0
154 inline ServiceImpl1( Reference
< XComponentContext
> const & xContext
) SAL_THROW(())
155 : ServiceImpl0( xContext
)
159 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (RuntimeException
);
160 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
);
163 OUString
ServiceImpl1::getImplementationName()
164 throw(::com::sun::star::uno::RuntimeException
)
166 return impl1_getImplementationName();
169 Sequence
< OUString
> ServiceImpl1::getSupportedServiceNames()
170 throw(::com::sun::star::uno::RuntimeException
)
172 return impl1_getSupportedServiceNames();
176 static Reference
< XInterface
> SAL_CALL
ServiceImpl0_create(
177 Reference
< XComponentContext
> const & xContext
)
178 SAL_THROW( (Exception
) )
180 return (OWeakObject
*)new ServiceImpl0( xContext
);
183 static Reference
< XInterface
> SAL_CALL
ServiceImpl1_create(
184 Reference
< XComponentContext
> const & xContext
)
185 SAL_THROW( (Exception
) )
187 return (OWeakObject
*)new ServiceImpl1( xContext
);
190 } // namespace cfg_test
192 static const struct ImplementationEntry g_entries
[] =
195 ::cfg_test::ServiceImpl0_create
, ::cfg_test::impl0_getImplementationName
,
196 ::cfg_test::impl0_getSupportedServiceNames
, createSingleComponentFactory
,
200 ::cfg_test::ServiceImpl1_create
, ::cfg_test::impl1_getImplementationName
,
201 ::cfg_test::impl1_getSupportedServiceNames
, createSingleComponentFactory
,
211 sal_Bool SAL_CALL
component_writeInfo(
212 void * pServiceManager
, void * pRegistryKey
)
214 return component_writeInfoHelper(
215 pServiceManager
, pRegistryKey
, g_entries
);
218 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
219 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
221 return component_getFactoryHelper(
222 pImplName
, pServiceManager
, pRegistryKey
, g_entries
);
235 Reference
< XComponentContext
> xContext( defaultBootstrap_InitialComponentContext() );
236 Reference
< lang::XMultiComponentFactory
> xMgr( xContext
->getServiceManager() );
238 // show what is in context
239 xContext
->getValueByName( "dump_maps" );
242 OSL_VERIFY( xContext
->getValueByName( "/global-context-properties/TestValue" ) >>= n
);
243 ::fprintf( stderr
, "> n=%d\n", n
);
245 Reference
< XInterface
> x
;
246 OSL_VERIFY( !(xContext
->getValueByName( "/singletons/my_converter" ) >>= x
) );
247 OSL_VERIFY( xContext
->getValueByName( "/singletons/com.sun.star.script.theConverter" ) >>= x
);
248 OSL_VERIFY( xContext
->getValueByName( "/singletons/com.sun.star.bootstrap.theTestComponent0" ) >>= x
);
250 ::fprintf( stderr
, "> registering service...\n");
252 OUString
libName( "cfg_test.dll" );
253 #elif defined(SAL_UNX)
254 OUString
libName( "libcfg_test.so" );
256 Reference
< registry::XImplementationRegistration
> xImplReg( xMgr
->createInstanceWithContext(
257 "com.sun.star.registry.ImplementationRegistration", xContext
), UNO_QUERY
);
258 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
259 xImplReg
->registerImplementation(
260 "com.sun.star.loader.SharedLibrary", libName
,
261 Reference
< registry::XSimpleRegistry
>() );
263 OSL_VERIFY( (x
= xMgr
->createInstanceWithContext( "com.sun.star.bootstrap.TestComponent0", xContext
)).is() );
264 OSL_VERIFY( (x
= xMgr
->createInstanceWithContext( "com.sun.star.bootstrap.TestComponent1", xContext
)).is() );
266 Reference
< lang::XComponent
> xComp( xContext
, UNO_QUERY
);
273 catch (Exception
& exc
)
275 OString
str( OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
276 ::fprintf( stderr
, "# caught exception: %s\n", str
.getStr() );
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */