xref: /aoo41x/main/cppuhelper/test/cfg_test.cxx (revision 9d7e27ac)
1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // starting the executable:
28cdf0e10cSrcweir // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
29cdf0e10cSrcweir // -env:UNO_TYPES=cpputest.rdb
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sal/main.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <rtl/strbuf.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
38cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
39cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
43cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
44cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir using namespace ::cppu;
52cdf0e10cSrcweir using namespace ::rtl;
53cdf0e10cSrcweir using namespace ::osl;
54cdf0e10cSrcweir using namespace ::com::sun::star;
55cdf0e10cSrcweir using namespace ::com::sun::star::uno;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace cfg_test
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
impl0_getSupportedServiceNames()61cdf0e10cSrcweir static Sequence< OUString > impl0_getSupportedServiceNames()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent0") );
64cdf0e10cSrcweir     return Sequence< OUString >( &str, 1 );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
impl0_getImplementationName()67cdf0e10cSrcweir static OUString impl0_getImplementationName()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent0") );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
impl1_getSupportedServiceNames()72cdf0e10cSrcweir static Sequence< OUString > impl1_getSupportedServiceNames()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent1") );
75cdf0e10cSrcweir     return Sequence< OUString >( &str, 1 );
76cdf0e10cSrcweir }
77cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
impl1_getImplementationName()78cdf0e10cSrcweir static OUString impl1_getImplementationName()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent1") );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //==================================================================================================
84cdf0e10cSrcweir class ServiceImpl0
85cdf0e10cSrcweir     : public WeakImplHelper2< lang::XServiceInfo, lang::XInitialization >
86cdf0e10cSrcweir {
87cdf0e10cSrcweir 	Reference< XComponentContext > m_xContext;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir public:
90cdf0e10cSrcweir     ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW( () );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     // XInitialization
93cdf0e10cSrcweir     virtual void SAL_CALL initialize( const Sequence< Any >& rArgs ) throw (Exception, RuntimeException);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	// XServiceInfo
96cdf0e10cSrcweir 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
97cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
98cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw (RuntimeException);
99cdf0e10cSrcweir };
100cdf0e10cSrcweir //__________________________________________________________________________________________________
ServiceImpl0(Reference<XComponentContext> const & xContext)101cdf0e10cSrcweir ServiceImpl0::ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW( () )
102cdf0e10cSrcweir     : m_xContext( xContext )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     sal_Int32 n;
105cdf0e10cSrcweir     OUString val;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     // service properties
108cdf0e10cSrcweir     OSL_VERIFY( m_xContext->getValueByName(
109cdf0e10cSrcweir         OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0") ) >>= n );
110cdf0e10cSrcweir     OSL_VERIFY( n == 13 );
111cdf0e10cSrcweir     OSL_VERIFY( m_xContext->getValueByName(
112cdf0e10cSrcweir         OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1") ) >>= val );
113cdf0e10cSrcweir     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of serviceprop1") ) );
114cdf0e10cSrcweir     // impl properties
115cdf0e10cSrcweir     OSL_VERIFY( m_xContext->getValueByName(
116cdf0e10cSrcweir         OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0") ) >>= n );
117cdf0e10cSrcweir     OSL_VERIFY( n == 15 );
118cdf0e10cSrcweir     OSL_VERIFY( m_xContext->getValueByName(
119cdf0e10cSrcweir         OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1") ) >>= val );
120cdf0e10cSrcweir     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of implprop1") ) );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir // XInitialization
123cdf0e10cSrcweir //__________________________________________________________________________________________________
initialize(const Sequence<Any> & rArgs)124cdf0e10cSrcweir void ServiceImpl0::initialize( const Sequence< Any >& rArgs )
125cdf0e10cSrcweir     throw (Exception, RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     // check args
128cdf0e10cSrcweir     OUString val;
129cdf0e10cSrcweir     OSL_VERIFY( rArgs.getLength() == 3 );
130cdf0e10cSrcweir     OSL_VERIFY( rArgs[ 0 ] >>= val );
131cdf0e10cSrcweir     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("first argument") ) );
132cdf0e10cSrcweir     OSL_VERIFY( rArgs[ 1 ] >>= val );
133cdf0e10cSrcweir     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("second argument") ) );
134cdf0e10cSrcweir     OSL_VERIFY( rArgs[ 2 ] >>= val );
135cdf0e10cSrcweir     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("third argument") ) );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir // XServiceInfo
138cdf0e10cSrcweir //__________________________________________________________________________________________________
getImplementationName()139cdf0e10cSrcweir OUString ServiceImpl0::getImplementationName()
140cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	return impl0_getImplementationName();
143cdf0e10cSrcweir }
144cdf0e10cSrcweir //__________________________________________________________________________________________________
getSupportedServiceNames()145cdf0e10cSrcweir Sequence< OUString > ServiceImpl0::getSupportedServiceNames()
146cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir 	return impl0_getSupportedServiceNames();
149cdf0e10cSrcweir }
150cdf0e10cSrcweir //__________________________________________________________________________________________________
supportsService(const OUString & rServiceName)151cdf0e10cSrcweir sal_Bool ServiceImpl0::supportsService( const OUString & rServiceName )
152cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
155cdf0e10cSrcweir 	const OUString * pArray = rSNL.getConstArray();
156cdf0e10cSrcweir 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
157cdf0e10cSrcweir 	{
158cdf0e10cSrcweir 		if (pArray[nPos] == rServiceName)
159cdf0e10cSrcweir 			return sal_True;
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 	return sal_False;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir //==================================================================================================
165cdf0e10cSrcweir class ServiceImpl1 : public ServiceImpl0
166cdf0e10cSrcweir {
167cdf0e10cSrcweir public:
168cdf0e10cSrcweir     inline ServiceImpl1( Reference< XComponentContext > const & xContext ) SAL_THROW( () )
169cdf0e10cSrcweir         : ServiceImpl0( xContext )
170cdf0e10cSrcweir         {}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     // XServiceInfo
173cdf0e10cSrcweir 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
174cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
175cdf0e10cSrcweir };
176cdf0e10cSrcweir //__________________________________________________________________________________________________
getImplementationName()177cdf0e10cSrcweir OUString ServiceImpl1::getImplementationName()
178cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	return impl1_getImplementationName();
181cdf0e10cSrcweir }
182cdf0e10cSrcweir //__________________________________________________________________________________________________
getSupportedServiceNames()183cdf0e10cSrcweir Sequence< OUString > ServiceImpl1::getSupportedServiceNames()
184cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	return impl1_getSupportedServiceNames();
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir //==================================================================================================
ServiceImpl0_create(Reference<XComponentContext> const & xContext)190cdf0e10cSrcweir static Reference< XInterface > SAL_CALL ServiceImpl0_create(
191cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
192cdf0e10cSrcweir 	SAL_THROW( (Exception) )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	return (OWeakObject *)new ServiceImpl0( xContext );
195cdf0e10cSrcweir }
196cdf0e10cSrcweir //==================================================================================================
ServiceImpl1_create(Reference<XComponentContext> const & xContext)197cdf0e10cSrcweir static Reference< XInterface > SAL_CALL ServiceImpl1_create(
198cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
199cdf0e10cSrcweir 	SAL_THROW( (Exception) )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	return (OWeakObject *)new ServiceImpl1( xContext );
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir } // namespace cfg_test
205cdf0e10cSrcweir 
206cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
207cdf0e10cSrcweir {
208cdf0e10cSrcweir 	{
209cdf0e10cSrcweir 		::cfg_test::ServiceImpl0_create, ::cfg_test::impl0_getImplementationName,
210cdf0e10cSrcweir 		::cfg_test::impl0_getSupportedServiceNames, createSingleComponentFactory,
211cdf0e10cSrcweir 		0, 0
212cdf0e10cSrcweir 	},
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		::cfg_test::ServiceImpl1_create, ::cfg_test::impl1_getImplementationName,
215cdf0e10cSrcweir 		::cfg_test::impl1_getSupportedServiceNames, createSingleComponentFactory,
216cdf0e10cSrcweir 		0, 0
217cdf0e10cSrcweir 	},
218cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
219cdf0e10cSrcweir };
220cdf0e10cSrcweir 
221cdf0e10cSrcweir // component exports
222cdf0e10cSrcweir extern "C"
223cdf0e10cSrcweir {
224cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)225cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
226cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
229cdf0e10cSrcweir }
230cdf0e10cSrcweir //==================================================================================================
component_writeInfo(void * pServiceManager,void * pRegistryKey)231cdf0e10cSrcweir sal_Bool SAL_CALL component_writeInfo(
232cdf0e10cSrcweir 	void * pServiceManager, void * pRegistryKey )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir 	return component_writeInfoHelper(
235cdf0e10cSrcweir         pServiceManager, pRegistryKey, g_entries );
236cdf0e10cSrcweir }
237cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)238cdf0e10cSrcweir void * SAL_CALL component_getFactory(
239cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir 	return component_getFactoryHelper(
242cdf0e10cSrcweir         pImplName, pServiceManager, pRegistryKey , g_entries );
243cdf0e10cSrcweir }
244cdf0e10cSrcweir }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 
247cdf0e10cSrcweir //##################################################################################################
248cdf0e10cSrcweir //##################################################################################################
249cdf0e10cSrcweir //##################################################################################################
250cdf0e10cSrcweir 
SAL_IMPLEMENT_MAIN()251cdf0e10cSrcweir SAL_IMPLEMENT_MAIN()
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     try
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         Reference< XComponentContext > xContext( defaultBootstrap_InitialComponentContext() );
256cdf0e10cSrcweir         Reference< lang::XMultiComponentFactory > xMgr( xContext->getServiceManager() );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         // show what is in context
259cdf0e10cSrcweir         xContext->getValueByName( OUSTR("dump_maps") );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         sal_Int32 n;
262cdf0e10cSrcweir         OSL_VERIFY( xContext->getValueByName( OUSTR("/global-context-properties/TestValue") ) >>= n );
263cdf0e10cSrcweir         ::fprintf( stderr, "> n=%d\n", n );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         Reference< XInterface > x;
266cdf0e10cSrcweir         OSL_VERIFY( !(xContext->getValueByName( OUSTR("/singletons/my_converter") ) >>= x) );
267cdf0e10cSrcweir         OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.script.theConverter") ) >>= x );
268cdf0e10cSrcweir         OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.bootstrap.theTestComponent0") ) >>= x );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir         ::fprintf( stderr, "> registering service...\n", n );
271cdf0e10cSrcweir #if defined(SAL_W32) || defined(SAL_OS2)
272cdf0e10cSrcweir         OUString libName( OUSTR("cfg_test.dll") );
273cdf0e10cSrcweir #elif defined(SAL_UNX)
274cdf0e10cSrcweir         OUString libName( OUSTR("libcfg_test.so") );
275cdf0e10cSrcweir #endif
276cdf0e10cSrcweir 		Reference< registry::XImplementationRegistration > xImplReg( xMgr->createInstanceWithContext(
277cdf0e10cSrcweir             OUSTR("com.sun.star.registry.ImplementationRegistration"), xContext ), UNO_QUERY );
278cdf0e10cSrcweir 		OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
279cdf0e10cSrcweir 		xImplReg->registerImplementation(
280cdf0e10cSrcweir             OUSTR("com.sun.star.loader.SharedLibrary"), libName,
281cdf0e10cSrcweir             Reference< registry::XSimpleRegistry >() );
282cdf0e10cSrcweir 
283cdf0e10cSrcweir         OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent0"), xContext )).is() );
284cdf0e10cSrcweir         OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent1"), xContext )).is() );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir         Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
287cdf0e10cSrcweir         if (xComp.is())
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             xComp->dispose();
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir         return 0;
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir     catch (Exception & exc)
294cdf0e10cSrcweir     {
295cdf0e10cSrcweir         OString str( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
296cdf0e10cSrcweir         ::fprintf( stderr, "# caught exception: %s\n", str.getStr() );
297cdf0e10cSrcweir         return 1;
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir }
300