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