1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testproxyfac.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
35 #include "osl/diagnose.h"
36 #include "rtl/alloc.h"
37 #include "uno/environment.hxx"
38 #include "cppuhelper/servicefactory.hxx"
39 #include "cppuhelper/implbase1.hxx"
40 #include "cppuhelper/implbase3.hxx"
41 #include "com/sun/star/uno/XCurrentContext.hpp"
42 #include "com/sun/star/lang/DisposedException.hpp"
43 #include "com/sun/star/lang/XComponent.hpp"
44 #include "com/sun/star/lang/XServiceInfo.hpp"
45 #include "com/sun/star/registry/XSimpleRegistry.hpp"
46 #include "com/sun/star/registry/XImplementationRegistration.hpp"
47 #include "com/sun/star/beans/XPropertySet.hpp"
48 #include "com/sun/star/reflection/XProxyFactory.hpp"
53 using namespace ::rtl
;
54 using namespace ::osl
;
55 using namespace ::cppu
;
56 using namespace ::com::sun::star
;
57 using namespace ::com::sun::star::uno
;
60 typedef WeakImplHelper3
< lang::XServiceInfo
,
62 reflection::XProxyFactory
> t_impl
;
64 //==============================================================================
65 class TargetObject
: public t_impl
70 virtual ~TargetObject() {
72 OSL_TRACE( "~TargetObject()" );
77 Any SAL_CALL
queryInterface( Type
const & type
)
78 throw (RuntimeException
);
81 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
82 { return OUString::createFromAscii( "target" ); }
83 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
84 throw (RuntimeException
)
86 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
87 throw (RuntimeException
)
88 { return Sequence
< OUString
>(); }
90 virtual Reference
< XAggregation
> SAL_CALL
createProxy(
91 const Reference
< XInterface
> & xTarget
) throw (RuntimeException
)
92 { return Reference
< XAggregation
>( xTarget
, UNO_QUERY
); }
94 virtual Any SAL_CALL
getValueByName( OUString
const & name
)
95 throw (RuntimeException
)
96 { return makeAny( name
); }
99 //______________________________________________________________________________
100 Any
TargetObject::queryInterface( Type
const & type
)
101 throw (RuntimeException
)
103 Any
ret( t_impl::queryInterface( type
) );
106 throw lang::DisposedException(
107 OUString( RTL_CONSTASCII_USTRINGPARAM("my test exception") ),
108 static_cast< OWeakObject
* >(this) );
111 int TargetObject::s_obj
= 0;
114 //==============================================================================
115 class TestMaster
: public WeakImplHelper1
< lang::XServiceInfo
>
117 Reference
< XAggregation
> m_xProxyTarget
;
118 Reference
<lang::XServiceInfo
> m_xOtherProxyTargetBeforeSetDelegator
;
120 inline TestMaster() { ++s_obj
; }
123 static Reference
< XInterface
> create(
124 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
125 static Reference
< XInterface
> create(
126 Reference
< XInterface
> const & xTarget
,
127 Reference
< reflection::XProxyFactory
> const & xProxyFac
);
129 virtual ~TestMaster() {
131 OSL_TRACE( "~TestMaster()" );
134 virtual Any SAL_CALL
queryInterface( const Type
& rType
)
135 throw (RuntimeException
)
138 WeakImplHelper1
< lang::XServiceInfo
>::queryInterface( rType
) );
141 return m_xProxyTarget
->queryAggregation( rType
);
145 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
)
146 { return OUString::createFromAscii( "master" ); }
147 virtual sal_Bool SAL_CALL
supportsService( const OUString
& /*rServiceName*/ )
148 throw (RuntimeException
)
149 { return sal_False
; }
150 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
151 throw (RuntimeException
)
152 { return Sequence
< OUString
>(); }
155 int TestMaster::s_obj
= 0;
158 Reference
< XInterface
> TestMaster::create(
159 Reference
< XInterface
> const & xTarget
,
160 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
162 TestMaster
* that
= new TestMaster
;
163 Reference
< XInterface
> xRet( static_cast< OWeakObject
* >( that
) );
165 Reference
< XAggregation
> xAgg( xProxyFac
->createProxy( xTarget
) );
166 // ownership take over
167 that
->m_xProxyTarget
.set( xAgg
, UNO_QUERY_THROW
);
168 that
->m_xOtherProxyTargetBeforeSetDelegator
.set(
169 that
->m_xProxyTarget
, UNO_QUERY
);
171 that
->m_xProxyTarget
->setDelegator( xRet
);
175 Reference
< XInterface
> TestMaster::create(
176 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
179 static_cast< OWeakObject
* >( new TargetObject
), xProxyFac
);
183 static void test_proxyfac_(
184 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
185 Reference
< reflection::XProxyFactory
> const & /*xProxyFac*/ )
188 Reference
< lang::XServiceInfo
> xMaster_XServiceInfo(
189 xMaster
, UNO_QUERY_THROW
);
190 OSL_ASSERT( xMaster_XServiceInfo
->getImplementationName().equals( test
) );
192 Reference
< reflection::XProxyFactory
> xTarget( xMaster
, UNO_QUERY_THROW
);
193 Reference
< XCurrentContext
> xTarget_XCurrentContext(
194 xTarget
, UNO_QUERY_THROW
);
195 Reference
< XCurrentContext
> xMaster_XCurrentContext(
196 xMaster
, UNO_QUERY_THROW
);
199 xTarget_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
201 xMaster_XCurrentContext
->getValueByName( test
) == makeAny( test
) );
203 Reference
< XAggregation
> xFakeAgg( xTarget
->createProxy( xTarget
) );
206 OSL_ASSERT( xTarget
== xFakeAgg
);
207 OSL_ASSERT( xMaster
== xFakeAgg
);
210 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo(
211 xTarget
, UNO_QUERY_THROW
);
212 OSL_ASSERT( xTarget_XServiceInfo
->getImplementationName().equals( test
) );
213 Reference
< lang::XServiceInfo
> xTarget_XServiceInfo2(
214 xTarget
, UNO_QUERY_THROW
);
215 OSL_ASSERT( xTarget_XServiceInfo2
.get() == xTarget_XServiceInfo
.get() );
217 OSL_ASSERT( xTarget
== xTarget_XCurrentContext
);
218 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
220 xTarget_XCurrentContext
.get() == xMaster_XCurrentContext
.get() );
221 OSL_ASSERT( xTarget_XCurrentContext
== xMaster
);
222 OSL_ASSERT( xTarget
== xMaster
);
223 OSL_ASSERT( xTarget_XServiceInfo
.get() == xMaster_XServiceInfo
.get() );
224 OSL_ASSERT( xTarget_XServiceInfo
== xMaster
);
225 OSL_ASSERT( xMaster_XServiceInfo
== xMaster
);
229 Reference
< registry::XRegistryKey
>(
230 xMaster
, UNO_QUERY_THROW
);
232 catch (lang::DisposedException
& exc
)
234 if (! exc
.Message
.equalsAsciiL(
235 RTL_CONSTASCII_STRINGPARAM("my test exception") ))
240 static void test_proxyfac(
241 Reference
< XInterface
> const & xMaster
, OUString
const & test
,
242 Reference
< reflection::XProxyFactory
> const & xProxyFac
)
244 test_proxyfac_( xMaster
, test
, xProxyFac
);
245 // proxy the proxy...
246 Reference
< XInterface
> xNew( TestMaster::create( xMaster
, xProxyFac
) );
248 xNew
, OUString( RTL_CONSTASCII_USTRINGPARAM("master") ), xProxyFac
);
256 OUString
cpp( RTL_CONSTASCII_USTRINGPARAM(
257 CPPU_CURRENT_LANGUAGE_BINDING_NAME
) );
259 reinterpret_cast< uno_Environment
** >( &cpp_env
),
261 OSL_ENSURE( cpp_env
.is(), "### cannot get C++ uno env!" );
264 Reference
< lang::XMultiServiceFactory
> xMgr(
265 createRegistryServiceFactory(
266 OUString( RTL_CONSTASCII_USTRINGPARAM("stoctest.rdb") ) ) );
270 Reference
< registry::XImplementationRegistration
> xImplReg(
271 xMgr
->createInstance(
273 RTL_CONSTASCII_USTRINGPARAM(
274 "com.sun.star.registry.ImplementationRegistration")
277 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
280 RTL_CONSTASCII_USTRINGPARAM("proxyfac.uno" SAL_DLLEXTENSION
) );
281 xImplReg
->registerImplementation(
283 RTL_CONSTASCII_USTRINGPARAM(
284 "com.sun.star.loader.SharedLibrary") ),
285 aLibName
, Reference
< registry::XSimpleRegistry
>() );
287 Reference
< reflection::XProxyFactory
> xProxyFac(
288 xMgr
->createInstance(
289 OUString::createFromAscii(
290 "com.sun.star.reflection.ProxyFactory") ),
293 Reference
< XAggregation
> x(
294 xProxyFac
->createProxy(
295 static_cast< OWeakObject
* >( new TargetObject
) ) );
299 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
302 OUString( RTL_CONSTASCII_USTRINGPARAM("master") ),
306 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
311 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
312 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
313 xMaster
, UNO_QUERY_THROW
);
317 OUString( RTL_CONSTASCII_USTRINGPARAM("master") ),
319 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
322 Reference
< XInterface
> xMaster( TestMaster::create( xProxyFac
) );
323 Reference
< reflection::XProxyFactory
> xSlave_lives_alone(
324 xMaster
, UNO_QUERY_THROW
);
329 xProxyFac
->createProxy(
330 static_cast< OWeakObject
* >( new TargetObject
) ),
331 OUString( RTL_CONSTASCII_USTRINGPARAM("target") ),
333 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
335 catch (Exception
& rExc
)
341 rExc
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
346 Reference
< lang::XComponent
> xComp
;
347 Reference
< beans::XPropertySet
>(
348 xMgr
, UNO_QUERY_THROW
)->getPropertyValue(
349 OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) )
354 if (TestMaster::s_obj
!= 0)
355 fprintf( stderr
, "TestMaster objects: %d\n", TestMaster::s_obj
);
356 if (TargetObject::s_obj
!= 0)
357 fprintf( stderr
, "TargetObject objects: %d\n", TargetObject::s_obj
);
359 uno_dumpEnvironment( stdout
, cpp_env
.get(), 0 );
360 void ** ppInterfaces
;
362 uno_ExtEnvironment
* env
= cpp_env
.get()->pExtEnv
;
363 (*env
->getRegisteredInterfaces
)(
364 env
, &ppInterfaces
, &len
, rtl_allocateMemory
);
366 fprintf( stderr
, "%d registered C++ interfaces left!\n", len
);
368 success
&= (TestMaster::s_obj
== 0 &&
369 TargetObject::s_obj
== 0 &&
373 printf( "testproxyfac succeeded.\n" );
378 fprintf( stderr
, "testproxyfac failed!\n" );