merge the formfield patch from ooo-build
[ooovba.git] / bridges / test / java_uno / acquire / testacquire.cxx
blob93c6119b8c10289417fca6849ad1e8d1525f2355
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testacquire.cxx,v $
10 * $Revision: 1.7 $
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_bridges.hxx"
34 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
35 #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
36 #include "com/sun/star/lang/XMain.hpp"
37 #include "com/sun/star/lang/XServiceInfo.hpp"
38 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
39 #include "com/sun/star/registry/InvalidRegistryException.hpp"
40 #include "com/sun/star/registry/XRegistryKey.hpp"
41 #include "com/sun/star/uno/Any.hxx"
42 #include "com/sun/star/uno/Exception.hpp"
43 #include "com/sun/star/uno/Reference.hxx"
44 #include "com/sun/star/uno/RuntimeException.hpp"
45 #include "com/sun/star/uno/Sequence.hxx"
46 #include "com/sun/star/uno/Type.hxx"
47 #include "com/sun/star/uno/XComponentContext.hpp"
48 #include "com/sun/star/uno/XInterface.hpp"
49 #include "cppuhelper/factory.hxx"
50 #include "cppuhelper/implbase3.hxx"
51 #include "cppuhelper/weak.hxx"
52 #include "osl/conditn.hxx"
53 #include "osl/interlck.h"
54 #include "rtl/string.h"
55 #include "rtl/ustring.hxx"
56 #include "sal/types.h"
57 #include "test/javauno/acquire/XBase.hpp"
58 #include "test/javauno/acquire/XDerived.hpp"
59 #include "test/javauno/acquire/XTest.hpp"
60 #include "uno/environment.h"
61 #include "uno/lbnames.h"
63 #include <iostream>
64 #include <cstdlib>
66 namespace css = com::sun::star;
68 namespace {
70 class WaitCondition {
71 public:
72 WaitCondition() {}
74 ~WaitCondition();
76 osl::Condition & get() { return m_condition; }
78 private:
79 WaitCondition(WaitCondition &); // not implemented
80 void operator =(WaitCondition); // not implemented
82 osl::Condition m_condition;
87 WaitCondition::~WaitCondition() {
88 std::cout << "waiting for condition\n";
89 if (m_condition.wait() != osl::Condition::result_ok) {
90 throw "osl::Condition::wait failed";
94 namespace {
96 class Interface: public css::uno::XInterface {
97 public:
98 explicit Interface(osl::Condition & condition):
99 m_condition(condition), m_refCount(0) {}
101 virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
102 throw (css::uno::RuntimeException);
104 virtual void SAL_CALL acquire() throw ()
105 { osl_incrementInterlockedCount(&m_refCount); }
107 virtual void SAL_CALL release() throw ();
109 protected:
110 virtual ~Interface() { m_condition.set(); }
112 private:
113 Interface(Interface &); // not implemented
114 void operator =(Interface); // not implemented
116 osl::Condition & m_condition;
117 oslInterlockedCount m_refCount;
122 css::uno::Any Interface::queryInterface(css::uno::Type const & type)
123 throw (css::uno::RuntimeException)
125 return type.getTypeName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
126 "com.sun.star.uno.XInterface"))
127 ? css::uno::makeAny(css::uno::Reference< css::uno::XInterface >(this))
128 : css::uno::Any();
131 void Interface::release() throw () {
132 if (osl_decrementInterlockedCount(&m_refCount) == 0) {
133 delete this;
137 namespace {
139 class Base: public Interface, public test::javauno::acquire::XBase {
140 public:
141 explicit Base(osl::Condition & condition): Interface(condition) {}
143 virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
144 throw (css::uno::RuntimeException);
146 virtual void SAL_CALL acquire() throw () { Interface::acquire(); }
148 virtual void SAL_CALL release() throw () { Interface::release(); }
150 protected:
151 virtual ~Base() {}
156 css::uno::Any Base::queryInterface(css::uno::Type const & type)
157 throw (css::uno::RuntimeException)
159 return type.getTypeName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
160 "test.javauno.acquire.XBase"))
161 ? css::uno::makeAny(
162 css::uno::Reference< test::javauno::acquire::XBase >(this))
163 : Interface::queryInterface(type);
166 namespace {
168 class Derived: public Base, public test::javauno::acquire::XDerived {
169 public:
170 explicit Derived(osl::Condition & condition): Base(condition) {}
172 virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
173 throw (css::uno::RuntimeException);
175 virtual void SAL_CALL acquire() throw () { Base::acquire(); }
177 virtual void SAL_CALL release() throw () { Base::release(); }
179 private:
180 virtual ~Derived() {}
185 css::uno::Any Derived::queryInterface(css::uno::Type const & type)
186 throw (css::uno::RuntimeException)
188 return (type.getTypeName().equalsAsciiL(
189 RTL_CONSTASCII_STRINGPARAM("test.javauno.acquire.XDerived")))
190 ? css::uno::makeAny(
191 css::uno::Reference< test::javauno::acquire::XDerived >(this))
192 : Interface::queryInterface(type);
195 namespace {
197 class Service: public cppu::WeakImplHelper3<
198 css::lang::XServiceInfo, css::lang::XMain, test::javauno::acquire::XTest >
200 public:
201 virtual rtl::OUString SAL_CALL getImplementationName()
202 throw (css::uno::RuntimeException)
203 { return getImplementationName_static(); }
205 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
206 throw (css::uno::RuntimeException);
208 virtual css::uno::Sequence< rtl::OUString > SAL_CALL
209 getSupportedServiceNames() throw (css::uno::RuntimeException)
210 { return getSupportedServiceNames_static(); }
212 virtual sal_Int32 SAL_CALL
213 run(css::uno::Sequence< rtl::OUString > const & arguments)
214 throw (css::uno::RuntimeException);
216 virtual void SAL_CALL setInterfaceToInterface(
217 css::uno::Reference< css::uno::XInterface > const & obj)
218 throw (css::uno::RuntimeException)
219 { m_interface = obj; }
221 virtual void SAL_CALL setBaseToInterface(
222 css::uno::Reference< test::javauno::acquire::XBase > const & obj)
223 throw (css::uno::RuntimeException)
224 { m_interface = obj; }
226 virtual void SAL_CALL setDerivedToInterface(
227 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
228 throw (css::uno::RuntimeException)
229 { m_interface = obj; }
231 virtual css::uno::Reference< css::uno::XInterface >
232 SAL_CALL getInterfaceFromInterface() throw (css::uno::RuntimeException)
233 { return m_interface; }
235 virtual void SAL_CALL clearInterface() throw (css::uno::RuntimeException)
236 { m_interface.clear(); }
238 virtual void SAL_CALL setBaseToBase(
239 css::uno::Reference< test::javauno::acquire::XBase > const & obj)
240 throw (css::uno::RuntimeException)
241 { m_base = obj; }
243 virtual void SAL_CALL setDerivedToBase(
244 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
245 throw (css::uno::RuntimeException)
246 { m_base = obj.get(); }
248 virtual css::uno::Reference< css::uno::XInterface >
249 SAL_CALL getInterfaceFromBase() throw (css::uno::RuntimeException)
250 { return m_base; }
252 virtual css::uno::Reference< test::javauno::acquire::XBase >
253 SAL_CALL getBaseFromBase() throw (css::uno::RuntimeException)
254 { return m_base; }
256 virtual void SAL_CALL clearBase() throw (css::uno::RuntimeException)
257 { m_base.clear(); }
259 virtual void SAL_CALL setDerivedToDerived(
260 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
261 throw (css::uno::RuntimeException)
262 { m_derived = obj; }
264 virtual css::uno::Reference< css::uno::XInterface >
265 SAL_CALL getInterfaceFromDerived() throw (css::uno::RuntimeException)
266 { return m_derived; }
268 virtual css::uno::Reference< test::javauno::acquire::XBase >
269 SAL_CALL getBaseFromDerived() throw (css::uno::RuntimeException)
270 { return m_derived.get(); }
272 virtual css::uno::Reference< test::javauno::acquire::XDerived >
273 SAL_CALL getDerivedFromDerived() throw (css::uno::RuntimeException)
274 { return m_derived; }
276 virtual void SAL_CALL clearDerived() throw (css::uno::RuntimeException)
277 { m_derived.clear(); }
279 virtual css::uno::Reference< css::uno::XInterface >
280 SAL_CALL roundTripInterfaceToInterface(
281 css::uno::Reference< css::uno::XInterface > const & obj)
282 throw (css::uno::RuntimeException)
283 { return obj; }
285 virtual css::uno::Reference< css::uno::XInterface >
286 SAL_CALL roundTripBaseToInterface(
287 css::uno::Reference< test::javauno::acquire::XBase > const & obj)
288 throw (css::uno::RuntimeException)
289 { return obj; }
291 virtual css::uno::Reference< css::uno::XInterface >
292 SAL_CALL roundTripDerivedToInterface(
293 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
294 throw (css::uno::RuntimeException)
295 { return obj; }
297 virtual css::uno::Reference< test::javauno::acquire::XBase >
298 SAL_CALL roundTripBaseToBase(
299 css::uno::Reference< test::javauno::acquire::XBase > const & obj)
300 throw (css::uno::RuntimeException)
301 { return obj; }
303 virtual css::uno::Reference< test::javauno::acquire::XBase >
304 SAL_CALL roundTripDerivedToBase(
305 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
306 throw (css::uno::RuntimeException)
307 { return obj.get(); }
309 virtual css::uno::Reference< test::javauno::acquire::XDerived >
310 SAL_CALL roundTripDerivedToDerived(
311 css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
312 throw (css::uno::RuntimeException)
313 { return obj; }
315 static rtl::OUString getImplementationName_static();
317 static css::uno::Sequence< rtl::OUString >
318 getSupportedServiceNames_static();
320 static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
321 css::uno::Reference< css::uno::XComponentContext > const & context)
322 throw (css::uno::Exception);
324 private:
325 explicit Service(
326 css::uno::Reference< css::uno::XComponentContext > const & context):
327 m_context(context) {}
329 css::uno::Reference< css::uno::XComponentContext > m_context;
330 css::uno::Reference< css::uno::XInterface > m_interface;
331 css::uno::Reference< test::javauno::acquire::XBase > m_base;
332 css::uno::Reference< test::javauno::acquire::XDerived > m_derived;
337 sal_Bool Service::supportsService(rtl::OUString const & serviceName)
338 throw (css::uno::RuntimeException)
340 css::uno::Sequence< rtl::OUString > names(
341 getSupportedServiceNames_static());
342 for (sal_Int32 i = 0; i< names.getLength(); ++i) {
343 if (names[i] == serviceName) {
344 return true;
347 return false;
350 namespace {
352 template< typename T > void assertNotNull(css::uno::Reference< T > const & ref)
354 if (!ref.is()) {
355 std::cerr << "assertNotNull failed\n";
356 std::abort();
362 sal_Int32 Service::run(css::uno::Sequence< rtl::OUString > const & arguments)
363 throw (css::uno::RuntimeException)
365 // - arguments[0] must be the UNO URL to connect to:
366 css::uno::Reference< XTest > test(
367 css::bridge::UnoUrlResolver::create(m_context)->resolve(arguments[0]),
368 css::uno::UNO_QUERY_THROW);
371 WaitCondition c;
372 test->setInterfaceToInterface(new Interface(c.get()));
373 assertNotNull(test->getInterfaceFromInterface());
374 test->clearInterface();
377 WaitCondition c;
378 test->setInterfaceToInterface(
379 static_cast< Interface * >(new Base(c.get())));
380 assertNotNull(test->getInterfaceFromInterface());
381 test->clearInterface();
384 WaitCondition c;
385 test->setInterfaceToInterface(
386 static_cast< Interface * >(new Derived(c.get())));
387 assertNotNull(test->getInterfaceFromInterface());
388 test->clearInterface();
392 WaitCondition c;
393 test->setBaseToInterface(new Base(c.get()));
394 assertNotNull(test->getInterfaceFromInterface());
395 test->clearInterface();
398 WaitCondition c;
399 test->setBaseToInterface(static_cast< Base * >(new Derived(c.get())));
400 assertNotNull(test->getInterfaceFromInterface());
401 test->clearInterface();
405 WaitCondition c;
406 test->setDerivedToInterface(new Derived(c.get()));
407 assertNotNull(test->getInterfaceFromInterface());
408 test->clearInterface();
412 WaitCondition c;
413 test->setBaseToBase(new Base(c.get()));
414 assertNotNull(test->getInterfaceFromBase());
415 assertNotNull(test->getBaseFromBase());
416 test->clearBase();
419 WaitCondition c;
420 test->setBaseToBase(static_cast< Base * >(new Derived(c.get())));
421 assertNotNull(test->getInterfaceFromBase());
422 assertNotNull(test->getBaseFromBase());
423 test->clearBase();
427 WaitCondition c;
428 test->setDerivedToBase(new Derived(c.get()));
429 assertNotNull(test->getInterfaceFromBase());
430 assertNotNull(test->getBaseFromBase());
431 test->clearBase();
435 WaitCondition c;
436 test->setDerivedToDerived(new Derived(c.get()));
437 assertNotNull(test->getInterfaceFromDerived());
438 assertNotNull(test->getBaseFromDerived());
439 assertNotNull(test->getDerivedFromDerived());
440 test->clearDerived();
444 WaitCondition c;
445 assertNotNull(
446 test->roundTripInterfaceToInterface(new Interface(c.get())));
449 WaitCondition c;
450 assertNotNull(test->roundTripInterfaceToInterface(
451 static_cast< Interface * >(new Base(c.get()))));
454 WaitCondition c;
455 assertNotNull(test->roundTripInterfaceToInterface(
456 static_cast< Interface * >(new Derived(c.get()))));
460 WaitCondition c;
461 assertNotNull(test->roundTripBaseToInterface(new Base(c.get())));
464 WaitCondition c;
465 assertNotNull(test->roundTripBaseToInterface(
466 static_cast< Base * >(new Derived(c.get()))));
470 WaitCondition c;
471 assertNotNull(test->roundTripDerivedToInterface(new Derived(c.get())));
475 WaitCondition c;
476 assertNotNull(test->roundTripBaseToBase(new Base(c.get())));
479 WaitCondition c;
480 assertNotNull(test->roundTripBaseToBase(
481 static_cast< Base * >(new Derived(c.get()))));
485 WaitCondition c;
486 assertNotNull(test->roundTripDerivedToBase(new Derived(c.get())));
490 WaitCondition c;
491 assertNotNull(test->roundTripDerivedToDerived(new Derived(c.get())));
494 std::cout << "Client and server both cleanly terminate now: Success\n";
495 return 0;
498 rtl::OUString Service::getImplementationName_static() {
499 return rtl::OUString::createFromAscii(
500 "com.sun.star.test.bridges.testacquire.impl");
503 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames_static() {
504 css::uno::Sequence< rtl::OUString > names(1);
505 names[0] = rtl::OUString::createFromAscii(
506 "com.sun.star.test.bridges.testacquire");
507 return names;
510 css::uno::Reference< css::uno::XInterface > Service::createInstance(
511 css::uno::Reference< css::uno::XComponentContext > const & context)
512 throw (css::uno::Exception)
514 return static_cast< cppu::OWeakObject * >(new Service(context));
517 extern "C" void SAL_CALL component_getImplementationEnvironment(
518 char const ** envTypeName, uno_Environment **)
520 if (envTypeName != 0) {
521 *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
525 extern "C" void * SAL_CALL component_getFactory(char const * implName,
526 void * serviceManager, void *) {
527 void * p = 0;
528 if (serviceManager != 0) {
529 css::uno::Reference< css::lang::XSingleComponentFactory > f;
530 if (Service::getImplementationName_static().equalsAscii(implName)) {
531 f = cppu::createSingleComponentFactory(
532 &Service::createInstance,
533 Service::getImplementationName_static(),
534 Service::getSupportedServiceNames_static());
536 if (f.is()) {
537 f->acquire();
538 p = f.get();
541 return p;
544 namespace {
546 bool writeInfo(void * registryKey, rtl::OUString const & implementationName,
547 css::uno::Sequence< rtl::OUString > const & serviceNames) {
548 rtl::OUString keyName(rtl::OUString::createFromAscii("/"));
549 keyName += implementationName;
550 keyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
551 css::uno::Reference< css::registry::XRegistryKey > key;
552 try {
553 key = static_cast< css::registry::XRegistryKey * >(registryKey)->
554 createKey(keyName);
555 } catch (css::registry::InvalidRegistryException &) {}
556 if (!key.is()) {
557 return false;
559 bool success = true;
560 for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
561 try {
562 key->createKey(serviceNames[i]);
563 } catch (css::registry::InvalidRegistryException &) {
564 success = false;
565 break;
568 return success;
573 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) {
574 return registryKey
575 && writeInfo(registryKey, Service::getImplementationName_static(),
576 Service::getSupportedServiceNames_static());