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