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 <uno/lbnames.h>
26 #include <cppuhelper/servicefactory.hxx>
27 #include <cppuhelper/implbase.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 ::osl
;
41 using namespace ::cppu
;
42 using namespace ::com::sun::star
;
43 using namespace css::uno
;
46 typedef WeakImplHelper
< lang::XServiceInfo
,
48 reflection::XProxyFactory
> t_impl
;
51 class TargetObject
: public t_impl
56 virtual ~TargetObject() {
58 SAL_INFO("stoc", "~TargetObject()" );
63 Any SAL_CALL
queryInterface( Type
const & type
)
64 throw (RuntimeException
);
67 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
68 { return OUString("target"); }
69 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
70 throw (RuntimeException
)
72 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
73 throw (RuntimeException
)
74 { return Sequence
< OUString
>(); }
76 virtual Reference
< XAggregation
> SAL_CALL
createProxy(
77 const Reference
< XInterface
> & xTarget
) throw (RuntimeException
)
78 { return Reference
< XAggregation
>( xTarget
, UNO_QUERY
); }
80 virtual Any SAL_CALL
getValueByName( OUString
const & name
)
81 throw (RuntimeException
)
82 { return makeAny( name
); }
86 Any
TargetObject::queryInterface( Type
const & type
)
87 throw (RuntimeException
)
89 Any
ret( t_impl::queryInterface( type
) );
92 throw lang::DisposedException(
93 OUString( "my test exception" ),
94 static_cast< OWeakObject
* >(this) );
97 int TargetObject::s_obj
= 0;
100 class TestMaster
: public WeakImplHelper
< lang::XServiceInfo
>
102 Reference
< XAggregation
> m_xProxyTarget
;
103 Reference
<lang::XServiceInfo
> m_xOtherProxyTargetBeforeSetDelegator
;
105 inline TestMaster() { ++s_obj
; }
108 static Reference
< XInterface
> create(
109 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
110 static Reference
< XInterface
> create(
111 Reference
< XInterface
> const & xTarget
,
112 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
114 virtual ~TestMaster() {
116 SAL_INFO("stoc", "~TestMaster()" );
119 virtual Any SAL_CALL
queryInterface( const Type
& rType
)
120 throw (RuntimeException
)
123 WeakImplHelper
< lang::XServiceInfo
>::queryInterface( rType
) );
126 return m_xProxyTarget
->queryAggregation( rType
);
130 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
131 { return OUString("master"); }
132 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
133 throw (RuntimeException
)
134 { return sal_False
; }
135 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
136 throw (RuntimeException
)
137 { return Sequence
< OUString
>(); }
140 int TestMaster::s_obj
= 0;
143 Reference
< XInterface
> TestMaster::create(
144 Reference
< XInterface
> const & xTarget
,
145 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
147 TestMaster
* that
= new TestMaster
;
148 Reference
< XInterface
> xRet( static_cast< OWeakObject
* >( that
) );
150 Reference
< XAggregation
> xAgg( xProxyFac
->createProxy( xTarget
) );
151 // ownership take over
152 that
->m_xProxyTarget
.set( xAgg
, UNO_QUERY_THROW
);
153 that
->m_xOtherProxyTargetBeforeSetDelegator
.set(
154 that
->m_xProxyTarget
, UNO_QUERY
);
156 that
->m_xProxyTarget
->setDelegator( xRet
);
160 Reference
< XInterface
> TestMaster::create(
161 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
164 static_cast< OWeakObject
* >( new TargetObject
), xProxyFac
);
168 static void test_proxyfac_(
169 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
170 Reference
< reflection::XProxyFactory
> const & /*xProxyFac*/ )
173 Reference
< lang::XServiceInfo
> xMaster_XServiceInfo(
174 xMaster
, UNO_QUERY_THROW
);
175 OSL_ASSERT( xMaster_XServiceInfo
->getImplementationName().equals( test
) );
177 Reference
< reflection::XProxyFactory
> xTarget( xMaster
, UNO_QUERY_THROW
);
178 Reference
< XCurrentContext
> xTarget_XCurrentContext(
179 xTarget
, UNO_QUERY_THROW
);
180 Reference
< XCurrentContext
> xMaster_XCurrentContext(
181 xMaster
, UNO_QUERY_THROW
);
184 xTarget_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
186 xMaster_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
188 Reference
< XAggregation
> xFakeAgg( xTarget
->createProxy( xTarget
) );
191 OSL_ASSERT( xTarget
== xFakeAgg
);
192 OSL_ASSERT( xMaster
== xFakeAgg
);
195 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo(
196 xTarget
, UNO_QUERY_THROW
);
197 OSL_ASSERT( xTarget_XServiceInfo
->getImplementationName().equals( test
) );
198 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo2(
199 xTarget
, UNO_QUERY_THROW
);
200 OSL_ASSERT( xTarget_XServiceInfo2
.get() == xTarget_XServiceInfo
.get() );
202 OSL_ASSERT( xTarget
== xTarget_XCurrentContext
);
203 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
205 xTarget_XCurrentContext
.get() == xMaster_XCurrentContext
.get() );
206 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
207 OSL_ASSERT( xTarget
== xMaster
);
208 OSL_ASSERT( xTarget_XServiceInfo
.get() == xMaster_XServiceInfo
.get() );
209 OSL_ASSERT( xTarget_XServiceInfo
== xMaster
);
210 OSL_ASSERT( xMaster_XServiceInfo
== xMaster
);
214 Reference
< registry::XRegistryKey
>(
215 xMaster
, UNO_QUERY_THROW
);
217 catch (const lang::DisposedException
& exc
)
219 if ( exc
.Message
!= "my test exception" )
224 static void test_proxyfac(
225 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
226 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
228 test_proxyfac_( xMaster
, test
, xProxyFac
);
229 // proxy the proxy...
230 Reference
< XInterface
> xNew( TestMaster::create( xMaster
, xProxyFac
) );
232 xNew
, OUString( "master" ), xProxyFac
);
240 OUString
cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
242 reinterpret_cast< uno_Environment
** >( &cpp_env
),
244 OSL_ENSURE( cpp_env
.is(), "### cannot get C++ uno env!" );
247 Reference
< lang::XMultiServiceFactory
> xMgr(
248 createRegistryServiceFactory(
249 OUString( "stoctest.rdb" ) ) );
253 Reference
< registry::XImplementationRegistration
> xImplReg(
254 xMgr
->createInstance( "com.sun.star.registry.ImplementationRegistration" ),
256 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
259 "proxyfac.uno" SAL_DLLEXTENSION
);
260 xImplReg
->registerImplementation(
261 OUString( "com.sun.star.loader.SharedLibrary" ),
262 aLibName
, Reference
< registry::XSimpleRegistry
>() );
264 Reference
< reflection::XProxyFactory
> xProxyFac(
265 xMgr
->createInstance("com.sun.star.reflection.ProxyFactory"),
268 Reference
< XAggregation
> x(
269 xProxyFac
->createProxy(
270 static_cast< OWeakObject
* >( new TargetObject
) ) );
274 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
277 OUString( "master" ),
281 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
286 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
287 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
288 xMaster
, UNO_QUERY_THROW
);
292 OUString( "master" ),
294 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
297 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
298 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
299 xMaster
, UNO_QUERY_THROW
);
304 xProxyFac
->createProxy(
305 static_cast< OWeakObject
* >( new TargetObject
) ),
306 OUString( "target" ),
308 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
310 catch (const Exception
& rExc
)
316 rExc
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
321 Reference
< lang::XComponent
> xComp
;
322 Reference
< beans::XPropertySet
>(
323 xMgr
, UNO_QUERY_THROW
)->getPropertyValue(
324 OUString( "DefaultContext" ) )
329 if (TestMaster::s_obj
!= 0)
330 fprintf( stderr
, "TestMaster objects: %d\n", TestMaster::s_obj
);
331 if (TargetObject::s_obj
!= 0)
332 fprintf( stderr
, "TargetObject objects: %d\n", TargetObject::s_obj
);
334 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
335 void ** ppInterfaces
;
337 uno_ExtEnvironment
* env
= cpp_env
.get()->pExtEnv
;
338 (*env
->getRegisteredInterfaces
)(
339 env
, &ppInterfaces
, &len
, rtl_allocateMemory
);
341 fprintf( stderr
, "%d registered C++ interfaces left!\n", len
);
343 success
&= (TestMaster::s_obj
== 0 &&
344 TargetObject::s_obj
== 0 &&
348 printf( "testproxyfac succeeded.\n" );
353 fprintf( stderr
, "testproxyfac failed!\n" );
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */