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 .
22 #include "osl/diagnose.h"
23 #include "rtl/alloc.h"
24 #include "uno/environment.hxx"
25 #include "cppuhelper/servicefactory.hxx"
26 #include "cppuhelper/implbase1.hxx"
27 #include "cppuhelper/implbase3.hxx"
28 #include "com/sun/star/uno/XCurrentContext.hpp"
29 #include "com/sun/star/lang/DisposedException.hpp"
30 #include "com/sun/star/lang/XComponent.hpp"
31 #include "com/sun/star/lang/XServiceInfo.hpp"
32 #include "com/sun/star/registry/XSimpleRegistry.hpp"
33 #include "com/sun/star/registry/XImplementationRegistration.hpp"
34 #include "com/sun/star/beans/XPropertySet.hpp"
35 #include "com/sun/star/reflection/XProxyFactory.hpp"
40 using namespace ::rtl
;
41 using namespace ::osl
;
42 using namespace ::cppu
;
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::uno
;
47 typedef WeakImplHelper3
< lang::XServiceInfo
,
49 reflection::XProxyFactory
> t_impl
;
51 //==============================================================================
52 class TargetObject
: public t_impl
57 virtual ~TargetObject() {
59 OSL_TRACE( "~TargetObject()" );
64 Any SAL_CALL
queryInterface( Type
const & type
)
65 throw (RuntimeException
);
68 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
69 { return OUString("target"); }
70 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
71 throw (RuntimeException
)
73 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
74 throw (RuntimeException
)
75 { return Sequence
< OUString
>(); }
77 virtual Reference
< XAggregation
> SAL_CALL
createProxy(
78 const Reference
< XInterface
> & xTarget
) throw (RuntimeException
)
79 { return Reference
< XAggregation
>( xTarget
, UNO_QUERY
); }
81 virtual Any SAL_CALL
getValueByName( OUString
const & name
)
82 throw (RuntimeException
)
83 { return makeAny( name
); }
86 //______________________________________________________________________________
87 Any
TargetObject::queryInterface( Type
const & type
)
88 throw (RuntimeException
)
90 Any
ret( t_impl::queryInterface( type
) );
93 throw lang::DisposedException(
94 OUString( "my test exception" ),
95 static_cast< OWeakObject
* >(this) );
98 int TargetObject::s_obj
= 0;
101 //==============================================================================
102 class TestMaster
: public WeakImplHelper1
< lang::XServiceInfo
>
104 Reference
< XAggregation
> m_xProxyTarget
;
105 Reference
<lang::XServiceInfo
> m_xOtherProxyTargetBeforeSetDelegator
;
107 inline TestMaster() { ++s_obj
; }
110 static Reference
< XInterface
> create(
111 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
112 static Reference
< XInterface
> create(
113 Reference
< XInterface
> const & xTarget
,
114 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
116 virtual ~TestMaster() {
118 OSL_TRACE( "~TestMaster()" );
121 virtual Any SAL_CALL
queryInterface( const Type
& rType
)
122 throw (RuntimeException
)
125 WeakImplHelper1
< lang::XServiceInfo
>::queryInterface( rType
) );
128 return m_xProxyTarget
->queryAggregation( rType
);
132 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
133 { return OUString("master"); }
134 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
135 throw (RuntimeException
)
136 { return sal_False
; }
137 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
138 throw (RuntimeException
)
139 { return Sequence
< OUString
>(); }
142 int TestMaster::s_obj
= 0;
145 Reference
< XInterface
> TestMaster::create(
146 Reference
< XInterface
> const & xTarget
,
147 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
149 TestMaster
* that
= new TestMaster
;
150 Reference
< XInterface
> xRet( static_cast< OWeakObject
* >( that
) );
152 Reference
< XAggregation
> xAgg( xProxyFac
->createProxy( xTarget
) );
153 // ownership take over
154 that
->m_xProxyTarget
.set( xAgg
, UNO_QUERY_THROW
);
155 that
->m_xOtherProxyTargetBeforeSetDelegator
.set(
156 that
->m_xProxyTarget
, UNO_QUERY
);
158 that
->m_xProxyTarget
->setDelegator( xRet
);
162 Reference
< XInterface
> TestMaster::create(
163 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
166 static_cast< OWeakObject
* >( new TargetObject
), xProxyFac
);
170 static void test_proxyfac_(
171 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
172 Reference
< reflection::XProxyFactory
> const & /*xProxyFac*/ )
175 Reference
< lang::XServiceInfo
> xMaster_XServiceInfo(
176 xMaster
, UNO_QUERY_THROW
);
177 OSL_ASSERT( xMaster_XServiceInfo
->getImplementationName().equals( test
) );
179 Reference
< reflection::XProxyFactory
> xTarget( xMaster
, UNO_QUERY_THROW
);
180 Reference
< XCurrentContext
> xTarget_XCurrentContext(
181 xTarget
, UNO_QUERY_THROW
);
182 Reference
< XCurrentContext
> xMaster_XCurrentContext(
183 xMaster
, UNO_QUERY_THROW
);
186 xTarget_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
188 xMaster_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
190 Reference
< XAggregation
> xFakeAgg( xTarget
->createProxy( xTarget
) );
193 OSL_ASSERT( xTarget
== xFakeAgg
);
194 OSL_ASSERT( xMaster
== xFakeAgg
);
197 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo(
198 xTarget
, UNO_QUERY_THROW
);
199 OSL_ASSERT( xTarget_XServiceInfo
->getImplementationName().equals( test
) );
200 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo2(
201 xTarget
, UNO_QUERY_THROW
);
202 OSL_ASSERT( xTarget_XServiceInfo2
.get() == xTarget_XServiceInfo
.get() );
204 OSL_ASSERT( xTarget
== xTarget_XCurrentContext
);
205 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
207 xTarget_XCurrentContext
.get() == xMaster_XCurrentContext
.get() );
208 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
209 OSL_ASSERT( xTarget
== xMaster
);
210 OSL_ASSERT( xTarget_XServiceInfo
.get() == xMaster_XServiceInfo
.get() );
211 OSL_ASSERT( xTarget_XServiceInfo
== xMaster
);
212 OSL_ASSERT( xMaster_XServiceInfo
== xMaster
);
216 Reference
< registry::XRegistryKey
>(
217 xMaster
, UNO_QUERY_THROW
);
219 catch (const lang::DisposedException
& exc
)
221 if ( exc
.Message
!= "my test exception" )
226 static void test_proxyfac(
227 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
228 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
230 test_proxyfac_( xMaster
, test
, xProxyFac
);
231 // proxy the proxy...
232 Reference
< XInterface
> xNew( TestMaster::create( xMaster
, xProxyFac
) );
234 xNew
, OUString( "master" ), xProxyFac
);
242 OUString
cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
244 reinterpret_cast< uno_Environment
** >( &cpp_env
),
246 OSL_ENSURE( cpp_env
.is(), "### cannot get C++ uno env!" );
249 Reference
< lang::XMultiServiceFactory
> xMgr(
250 createRegistryServiceFactory(
251 OUString( "stoctest.rdb" ) ) );
255 Reference
< registry::XImplementationRegistration
> xImplReg(
256 xMgr
->createInstance(
257 OUString( "com.sun.star.registry.ImplementationRegistration"
260 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
263 "proxyfac.uno" SAL_DLLEXTENSION
);
264 xImplReg
->registerImplementation(
265 OUString( "com.sun.star.loader.SharedLibrary" ),
266 aLibName
, Reference
< registry::XSimpleRegistry
>() );
268 Reference
< reflection::XProxyFactory
> xProxyFac(
269 xMgr
->createInstance(
271 "com.sun.star.reflection.ProxyFactory") ),
274 Reference
< XAggregation
> x(
275 xProxyFac
->createProxy(
276 static_cast< OWeakObject
* >( new TargetObject
) ) );
280 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
283 OUString( "master" ),
287 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
292 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
293 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
294 xMaster
, UNO_QUERY_THROW
);
298 OUString( "master" ),
300 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
303 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
304 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
305 xMaster
, UNO_QUERY_THROW
);
310 xProxyFac
->createProxy(
311 static_cast< OWeakObject
* >( new TargetObject
) ),
312 OUString( "target" ),
314 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
316 catch (const Exception
& rExc
)
322 rExc
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
327 Reference
< lang::XComponent
> xComp
;
328 Reference
< beans::XPropertySet
>(
329 xMgr
, UNO_QUERY_THROW
)->getPropertyValue(
330 OUString( "DefaultContext" ) )
335 if (TestMaster::s_obj
!= 0)
336 fprintf( stderr
, "TestMaster objects: %d\n", TestMaster::s_obj
);
337 if (TargetObject::s_obj
!= 0)
338 fprintf( stderr
, "TargetObject objects: %d\n", TargetObject::s_obj
);
340 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
341 void ** ppInterfaces
;
343 uno_ExtEnvironment
* env
= cpp_env
.get()->pExtEnv
;
344 (*env
->getRegisteredInterfaces
)(
345 env
, &ppInterfaces
, &len
, rtl_allocateMemory
);
347 fprintf( stderr
, "%d registered C++ interfaces left!\n", len
);
349 success
&= (TestMaster::s_obj
== 0 &&
350 TargetObject::s_obj
== 0 &&
354 printf( "testproxyfac succeeded.\n" );
359 fprintf( stderr
, "testproxyfac failed!\n" );
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */