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 <sal/log.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/alloc.h>
25 #include <uno/environment.hxx>
26 #include <uno/lbnames.h>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <com/sun/star/uno/XCurrentContext.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 #include <com/sun/star/registry/XImplementationRegistration.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/reflection/XProxyFactory.hpp>
41 using namespace ::osl
;
42 using namespace ::cppu
;
43 using namespace ::com::sun::star
;
44 using namespace css::uno
;
47 typedef WeakImplHelper
< lang::XServiceInfo
,
49 reflection::XProxyFactory
> t_impl
;
52 class TargetObject
: public t_impl
57 virtual ~TargetObject() {
59 SAL_INFO("stoc", "~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
); }
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" ),
98 int TargetObject::s_obj
= 0;
101 class TestMaster
: public WeakImplHelper
< lang::XServiceInfo
>
103 Reference
< XAggregation
> m_xProxyTarget
;
104 Reference
<lang::XServiceInfo
> m_xOtherProxyTargetBeforeSetDelegator
;
106 inline TestMaster() { ++s_obj
; }
109 static Reference
< XInterface
> create(
110 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
111 static Reference
< XInterface
> create(
112 Reference
< XInterface
> const & xTarget
,
113 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
115 virtual ~TestMaster() {
117 SAL_INFO("stoc", "~TestMaster()" );
120 virtual Any SAL_CALL
queryInterface( const Type
& rType
)
121 throw (RuntimeException
)
124 WeakImplHelper
< lang::XServiceInfo
>::queryInterface( rType
) );
127 return m_xProxyTarget
->queryAggregation( rType
);
131 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
132 { return OUString("master"); }
133 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
134 throw (RuntimeException
)
135 { return sal_False
; }
136 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
137 throw (RuntimeException
)
138 { return Sequence
< OUString
>(); }
141 int TestMaster::s_obj
= 0;
144 Reference
< XInterface
> TestMaster::create(
145 Reference
< XInterface
> const & xTarget
,
146 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
148 TestMaster
* that
= new TestMaster
;
149 Reference
< XInterface
> xRet( getXWeak( that
) );
151 Reference
< XAggregation
> xAgg( xProxyFac
->createProxy( xTarget
) );
152 // ownership take over
153 that
->m_xProxyTarget
.set( xAgg
, UNO_QUERY_THROW
);
154 that
->m_xOtherProxyTargetBeforeSetDelegator
.set(
155 that
->m_xProxyTarget
, UNO_QUERY
);
157 that
->m_xProxyTarget
->setDelegator( xRet
);
161 Reference
< XInterface
> TestMaster::create(
162 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
165 getXWeak( new TargetObject
), xProxyFac
);
169 static void test_proxyfac_(
170 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
171 Reference
< reflection::XProxyFactory
> const & /*xProxyFac*/ )
174 Reference
< lang::XServiceInfo
> xMaster_XServiceInfo(
175 xMaster
, UNO_QUERY_THROW
);
176 OSL_ASSERT( xMaster_XServiceInfo
->getImplementationName().equals( test
) );
178 Reference
< reflection::XProxyFactory
> xTarget( xMaster
, UNO_QUERY_THROW
);
179 Reference
< XCurrentContext
> xTarget_XCurrentContext(
180 xTarget
, UNO_QUERY_THROW
);
181 Reference
< XCurrentContext
> xMaster_XCurrentContext(
182 xMaster
, UNO_QUERY_THROW
);
185 xTarget_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
187 xMaster_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
189 Reference
< XAggregation
> xFakeAgg( xTarget
->createProxy( xTarget
) );
192 OSL_ASSERT( xTarget
== xFakeAgg
);
193 OSL_ASSERT( xMaster
== xFakeAgg
);
196 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo(
197 xTarget
, UNO_QUERY_THROW
);
198 OSL_ASSERT( xTarget_XServiceInfo
->getImplementationName().equals( test
) );
199 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo2(
200 xTarget
, UNO_QUERY_THROW
);
201 OSL_ASSERT( xTarget_XServiceInfo2
.get() == xTarget_XServiceInfo
.get() );
203 OSL_ASSERT( xTarget
== xTarget_XCurrentContext
);
204 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
206 xTarget_XCurrentContext
.get() == xMaster_XCurrentContext
.get() );
207 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
208 OSL_ASSERT( xTarget
== xMaster
);
209 OSL_ASSERT( xTarget_XServiceInfo
.get() == xMaster_XServiceInfo
.get() );
210 OSL_ASSERT( xTarget_XServiceInfo
== xMaster
);
211 OSL_ASSERT( xMaster_XServiceInfo
== xMaster
);
215 Reference
< registry::XRegistryKey
>(
216 xMaster
, UNO_QUERY_THROW
);
218 catch (const lang::DisposedException
& exc
)
220 if ( exc
.Message
!= "my test exception" )
225 static void test_proxyfac(
226 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
227 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
229 test_proxyfac_( xMaster
, test
, xProxyFac
);
230 // proxy the proxy...
231 Reference
< XInterface
> xNew( TestMaster::create( xMaster
, xProxyFac
) );
233 xNew
, OUString( "master" ), xProxyFac
);
241 OUString
cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
243 reinterpret_cast< uno_Environment
** >( &cpp_env
),
245 OSL_ENSURE( cpp_env
.is(), "### cannot get C++ uno env!" );
248 Reference
< lang::XMultiServiceFactory
> xMgr(
249 createRegistryServiceFactory(
250 OUString( "stoctest.rdb" ) ) );
254 Reference
< registry::XImplementationRegistration
> xImplReg(
255 xMgr
->createInstance( "com.sun.star.registry.ImplementationRegistration" ),
257 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
260 "proxyfac.uno" SAL_DLLEXTENSION
);
261 xImplReg
->registerImplementation(
262 OUString( "com.sun.star.loader.SharedLibrary" ),
263 aLibName
, Reference
< registry::XSimpleRegistry
>() );
265 Reference
< reflection::XProxyFactory
> xProxyFac(
266 xMgr
->createInstance("com.sun.star.reflection.ProxyFactory"),
269 Reference
< XAggregation
> x(
270 xProxyFac
->createProxy(
271 getXWeak( new TargetObject
) ) );
275 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
278 OUString( "master" ),
282 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
287 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
288 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
289 xMaster
, UNO_QUERY_THROW
);
293 OUString( "master" ),
295 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
298 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
299 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
300 xMaster
, UNO_QUERY_THROW
);
305 xProxyFac
->createProxy(
306 getXWeak( new TargetObject
) ),
307 OUString( "target" ),
309 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
311 catch (const Exception
& rExc
)
317 rExc
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
322 Reference
< lang::XComponent
> xComp
;
323 Reference
< beans::XPropertySet
>(
324 xMgr
, UNO_QUERY_THROW
)->getPropertyValue(
325 OUString( "DefaultContext" ) )
330 if (TestMaster::s_obj
!= 0)
331 fprintf( stderr
, "TestMaster objects: %d\n", TestMaster::s_obj
);
332 if (TargetObject::s_obj
!= 0)
333 fprintf( stderr
, "TargetObject objects: %d\n", TargetObject::s_obj
);
335 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
336 void ** ppInterfaces
;
338 uno_ExtEnvironment
* env
= cpp_env
.get()->pExtEnv
;
339 (*env
->getRegisteredInterfaces
)(
340 env
, &ppInterfaces
, &len
, rtl_allocateMemory
);
342 fprintf( stderr
, "%d registered C++ interfaces left!\n", len
);
344 success
&= (TestMaster::s_obj
== 0 &&
345 TargetObject::s_obj
== 0 &&
349 printf( "testproxyfac succeeded.\n" );
354 fprintf( stderr
, "testproxyfac failed!\n" );
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */