xref: /trunk/main/cppuhelper/test/cfg_test.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 );
94 
95     // XServiceInfo
96     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames();
97     virtual OUString SAL_CALL getImplementationName();
98     virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName );
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 {
126     // check args
127     OUString val;
128     OSL_VERIFY( rArgs.getLength() == 3 );
129     OSL_VERIFY( rArgs[ 0 ] >>= val );
130     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("first argument") ) );
131     OSL_VERIFY( rArgs[ 1 ] >>= val );
132     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("second argument") ) );
133     OSL_VERIFY( rArgs[ 2 ] >>= val );
134     OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("third argument") ) );
135 }
136 // XServiceInfo
137 //__________________________________________________________________________________________________
getImplementationName()138 OUString ServiceImpl0::getImplementationName()
139 {
140     return impl0_getImplementationName();
141 }
142 //__________________________________________________________________________________________________
getSupportedServiceNames()143 Sequence< OUString > ServiceImpl0::getSupportedServiceNames()
144 {
145     return impl0_getSupportedServiceNames();
146 }
147 //__________________________________________________________________________________________________
supportsService(const OUString & rServiceName)148 sal_Bool ServiceImpl0::supportsService( const OUString & rServiceName )
149 {
150     const Sequence< OUString > & rSNL = getSupportedServiceNames();
151     const OUString * pArray = rSNL.getConstArray();
152     for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
153     {
154         if (pArray[nPos] == rServiceName)
155             return sal_True;
156     }
157     return sal_False;
158 }
159 
160 //==================================================================================================
161 class ServiceImpl1 : public ServiceImpl0
162 {
163 public:
164     inline ServiceImpl1( Reference< XComponentContext > const & xContext ) SAL_THROW( () )
165         : ServiceImpl0( xContext )
166         {}
167 
168     // XServiceInfo
169     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames();
170     virtual OUString SAL_CALL getImplementationName();
171 };
172 //__________________________________________________________________________________________________
getImplementationName()173 OUString ServiceImpl1::getImplementationName()
174 {
175     return impl1_getImplementationName();
176 }
177 //__________________________________________________________________________________________________
getSupportedServiceNames()178 Sequence< OUString > ServiceImpl1::getSupportedServiceNames()
179 {
180     return impl1_getSupportedServiceNames();
181 }
182 
183 //==================================================================================================
ServiceImpl0_create(Reference<XComponentContext> const & xContext)184 static Reference< XInterface > SAL_CALL ServiceImpl0_create(
185     Reference< XComponentContext > const & xContext )
186 {
187     return (OWeakObject *)new ServiceImpl0( xContext );
188 }
189 //==================================================================================================
ServiceImpl1_create(Reference<XComponentContext> const & xContext)190 static Reference< XInterface > SAL_CALL ServiceImpl1_create(
191     Reference< XComponentContext > const & xContext )
192 {
193     return (OWeakObject *)new ServiceImpl1( xContext );
194 }
195 
196 } // namespace cfg_test
197 
198 static struct ImplementationEntry g_entries[] =
199 {
200     {
201         ::cfg_test::ServiceImpl0_create, ::cfg_test::impl0_getImplementationName,
202         ::cfg_test::impl0_getSupportedServiceNames, createSingleComponentFactory,
203         0, 0
204     },
205     {
206         ::cfg_test::ServiceImpl1_create, ::cfg_test::impl1_getImplementationName,
207         ::cfg_test::impl1_getSupportedServiceNames, createSingleComponentFactory,
208         0, 0
209     },
210     { 0, 0, 0, 0, 0, 0 }
211 };
212 
213 // component exports
214 extern "C"
215 {
216 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)217 void SAL_CALL component_getImplementationEnvironment(
218     const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
219 {
220     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
221 }
222 //==================================================================================================
component_writeInfo(void * pServiceManager,void * pRegistryKey)223 sal_Bool SAL_CALL component_writeInfo(
224     void * pServiceManager, void * pRegistryKey )
225 {
226     return component_writeInfoHelper(
227         pServiceManager, pRegistryKey, g_entries );
228 }
229 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)230 void * SAL_CALL component_getFactory(
231     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
232 {
233     return component_getFactoryHelper(
234         pImplName, pServiceManager, pRegistryKey , g_entries );
235 }
236 }
237 
238 
239 //##################################################################################################
240 //##################################################################################################
241 //##################################################################################################
242 
SAL_IMPLEMENT_MAIN()243 SAL_IMPLEMENT_MAIN()
244 {
245     try
246     {
247         Reference< XComponentContext > xContext( defaultBootstrap_InitialComponentContext() );
248         Reference< lang::XMultiComponentFactory > xMgr( xContext->getServiceManager() );
249 
250         // show what is in context
251         xContext->getValueByName( OUSTR("dump_maps") );
252 
253         sal_Int32 n;
254         OSL_VERIFY( xContext->getValueByName( OUSTR("/global-context-properties/TestValue") ) >>= n );
255         ::fprintf( stderr, "> n=%d\n", n );
256 
257         Reference< XInterface > x;
258         OSL_VERIFY( !(xContext->getValueByName( OUSTR("/singletons/my_converter") ) >>= x) );
259         OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.script.theConverter") ) >>= x );
260         OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.bootstrap.theTestComponent0") ) >>= x );
261 
262         ::fprintf( stderr, "> registering service...\n", n );
263 #if defined(SAL_W32) || defined(SAL_OS2)
264         OUString libName( OUSTR("cfg_test.dll") );
265 #elif defined(SAL_UNX)
266         OUString libName( OUSTR("libcfg_test.so") );
267 #endif
268         Reference< registry::XImplementationRegistration > xImplReg( xMgr->createInstanceWithContext(
269             OUSTR("com.sun.star.registry.ImplementationRegistration"), xContext ), UNO_QUERY );
270         OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
271         xImplReg->registerImplementation(
272             OUSTR("com.sun.star.loader.SharedLibrary"), libName,
273             Reference< registry::XSimpleRegistry >() );
274 
275         OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent0"), xContext )).is() );
276         OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent1"), xContext )).is() );
277 
278         Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
279         if (xComp.is())
280         {
281             xComp->dispose();
282         }
283         return 0;
284     }
285     catch (Exception & exc)
286     {
287         OString str( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
288         ::fprintf( stderr, "# caught exception: %s\n", str.getStr() );
289         return 1;
290     }
291 }
292