1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
32 #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
33 #include "com/sun/star/lang/XMain.hpp"
34 #include "com/sun/star/lang/XServiceInfo.hpp"
35 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
36 #include "com/sun/star/registry/InvalidRegistryException.hpp"
37 #include "com/sun/star/registry/XRegistryKey.hpp"
38 #include "com/sun/star/uno/Any.hxx"
39 #include "com/sun/star/uno/Exception.hpp"
40 #include "com/sun/star/uno/Reference.hxx"
41 #include "com/sun/star/uno/RuntimeException.hpp"
42 #include "com/sun/star/uno/Sequence.hxx"
43 #include "com/sun/star/uno/Type.hxx"
44 #include "com/sun/star/uno/XComponentContext.hpp"
45 #include "com/sun/star/uno/XInterface.hpp"
46 #include "cppuhelper/factory.hxx"
47 #include "cppuhelper/implbase3.hxx"
48 #include "cppuhelper/weak.hxx"
49 #include "osl/conditn.hxx"
50 #include "osl/interlck.h"
51 #include "rtl/string.h"
52 #include "rtl/ustring.hxx"
53 #include "sal/types.h"
54 #include "test/javauno/acquire/XBase.hpp"
55 #include "test/javauno/acquire/XDerived.hpp"
56 #include "test/javauno/acquire/XTest.hpp"
57 #include "uno/environment.h"
58 #include "uno/lbnames.h"
59 
60 #include <iostream>
61 #include <cstdlib>
62 
63 namespace css = com::sun::star;
64 
65 namespace {
66 
67 class WaitCondition {
68 public:
69     WaitCondition() {}
70 
71     ~WaitCondition();
72 
73     osl::Condition & get() { return m_condition; }
74 
75 private:
76     WaitCondition(WaitCondition &); // not implemented
77     void operator =(WaitCondition); // not implemented
78 
79     osl::Condition m_condition;
80 };
81 
82 }
83 
84 WaitCondition::~WaitCondition() {
85     std::cout << "waiting for condition\n";
86     if (m_condition.wait() != osl::Condition::result_ok) {
87         throw "osl::Condition::wait failed";
88     }
89 }
90 
91 namespace {
92 
93 class Interface: public css::uno::XInterface {
94 public:
95     explicit Interface(osl::Condition & condition):
96         m_condition(condition), m_refCount(0) {}
97 
98     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
99         throw (css::uno::RuntimeException);
100 
101     virtual void SAL_CALL acquire() throw ()
102     { osl_incrementInterlockedCount(&m_refCount); }
103 
104     virtual void SAL_CALL release() throw ();
105 
106 protected:
107     virtual ~Interface() { m_condition.set(); }
108 
109 private:
110     Interface(Interface &); // not implemented
111     void operator =(Interface); // not implemented
112 
113     osl::Condition & m_condition;
114     oslInterlockedCount m_refCount;
115 };
116 
117 }
118 
119 css::uno::Any Interface::queryInterface(css::uno::Type const & type)
120     throw (css::uno::RuntimeException)
121 {
122     return type.getTypeName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
123                                                "com.sun.star.uno.XInterface"))
124         ? css::uno::makeAny(css::uno::Reference< css::uno::XInterface >(this))
125         : css::uno::Any();
126 }
127 
128 void Interface::release() throw () {
129     if (osl_decrementInterlockedCount(&m_refCount) == 0) {
130         delete this;
131     }
132 }
133 
134 namespace {
135 
136 class Base: public Interface, public test::javauno::acquire::XBase {
137 public:
138     explicit Base(osl::Condition & condition): Interface(condition) {}
139 
140     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
141         throw (css::uno::RuntimeException);
142 
143     virtual void SAL_CALL acquire() throw () { Interface::acquire(); }
144 
145     virtual void SAL_CALL release() throw () { Interface::release(); }
146 
147 protected:
148     virtual ~Base() {}
149 };
150 
151 }
152 
153 css::uno::Any Base::queryInterface(css::uno::Type const & type)
154     throw (css::uno::RuntimeException)
155 {
156     return type.getTypeName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
157                                                "test.javauno.acquire.XBase"))
158         ? css::uno::makeAny(
159             css::uno::Reference< test::javauno::acquire::XBase >(this))
160         : Interface::queryInterface(type);
161 }
162 
163 namespace {
164 
165 class Derived: public Base, public test::javauno::acquire::XDerived {
166 public:
167     explicit Derived(osl::Condition & condition): Base(condition) {}
168 
169     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
170         throw (css::uno::RuntimeException);
171 
172     virtual void SAL_CALL acquire() throw () { Base::acquire(); }
173 
174     virtual void SAL_CALL release() throw () { Base::release(); }
175 
176 private:
177     virtual ~Derived() {}
178 };
179 
180 }
181 
182 css::uno::Any Derived::queryInterface(css::uno::Type const & type)
183     throw (css::uno::RuntimeException)
184 {
185     return (type.getTypeName().equalsAsciiL(
186                 RTL_CONSTASCII_STRINGPARAM("test.javauno.acquire.XDerived")))
187         ? css::uno::makeAny(
188             css::uno::Reference< test::javauno::acquire::XDerived >(this))
189         : Interface::queryInterface(type);
190 }
191 
192 namespace {
193 
194 class Service: public cppu::WeakImplHelper3<
195     css::lang::XServiceInfo, css::lang::XMain, test::javauno::acquire::XTest >
196 {
197 public:
198     virtual rtl::OUString SAL_CALL getImplementationName()
199         throw (css::uno::RuntimeException)
200     { return getImplementationName_static(); }
201 
202     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
203         throw (css::uno::RuntimeException);
204 
205     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
206     getSupportedServiceNames()  throw (css::uno::RuntimeException)
207     { return getSupportedServiceNames_static(); }
208 
209     virtual sal_Int32 SAL_CALL
210     run(css::uno::Sequence< rtl::OUString > const & arguments)
211         throw (css::uno::RuntimeException);
212 
213     virtual void SAL_CALL setInterfaceToInterface(
214         css::uno::Reference< css::uno::XInterface > const & obj)
215         throw (css::uno::RuntimeException)
216     { m_interface = obj; }
217 
218     virtual void SAL_CALL setBaseToInterface(
219         css::uno::Reference< test::javauno::acquire::XBase > const & obj)
220         throw (css::uno::RuntimeException)
221     { m_interface = obj; }
222 
223     virtual void SAL_CALL setDerivedToInterface(
224         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
225         throw (css::uno::RuntimeException)
226     { m_interface = obj; }
227 
228     virtual css::uno::Reference< css::uno::XInterface >
229     SAL_CALL getInterfaceFromInterface() throw (css::uno::RuntimeException)
230     { return m_interface; }
231 
232     virtual void SAL_CALL clearInterface() throw (css::uno::RuntimeException)
233     { m_interface.clear(); }
234 
235     virtual void SAL_CALL setBaseToBase(
236         css::uno::Reference< test::javauno::acquire::XBase > const & obj)
237         throw (css::uno::RuntimeException)
238     { m_base = obj; }
239 
240     virtual void SAL_CALL setDerivedToBase(
241         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
242         throw (css::uno::RuntimeException)
243     { m_base = obj.get(); }
244 
245     virtual css::uno::Reference< css::uno::XInterface >
246     SAL_CALL getInterfaceFromBase() throw (css::uno::RuntimeException)
247     { return m_base; }
248 
249     virtual css::uno::Reference< test::javauno::acquire::XBase >
250     SAL_CALL getBaseFromBase() throw (css::uno::RuntimeException)
251     { return m_base; }
252 
253     virtual void SAL_CALL clearBase() throw (css::uno::RuntimeException)
254     { m_base.clear(); }
255 
256     virtual void SAL_CALL setDerivedToDerived(
257         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
258         throw (css::uno::RuntimeException)
259     { m_derived = obj; }
260 
261     virtual css::uno::Reference< css::uno::XInterface >
262     SAL_CALL getInterfaceFromDerived() throw (css::uno::RuntimeException)
263     { return m_derived; }
264 
265     virtual css::uno::Reference< test::javauno::acquire::XBase >
266     SAL_CALL getBaseFromDerived() throw (css::uno::RuntimeException)
267     { return m_derived.get(); }
268 
269     virtual css::uno::Reference< test::javauno::acquire::XDerived >
270     SAL_CALL getDerivedFromDerived() throw (css::uno::RuntimeException)
271     { return m_derived; }
272 
273     virtual void SAL_CALL clearDerived() throw (css::uno::RuntimeException)
274     { m_derived.clear(); }
275 
276     virtual css::uno::Reference< css::uno::XInterface >
277     SAL_CALL roundTripInterfaceToInterface(
278         css::uno::Reference< css::uno::XInterface > const & obj)
279         throw (css::uno::RuntimeException)
280     { return obj; }
281 
282     virtual css::uno::Reference< css::uno::XInterface >
283     SAL_CALL roundTripBaseToInterface(
284         css::uno::Reference< test::javauno::acquire::XBase > const & obj)
285         throw (css::uno::RuntimeException)
286     { return obj; }
287 
288     virtual css::uno::Reference< css::uno::XInterface >
289     SAL_CALL roundTripDerivedToInterface(
290         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
291         throw (css::uno::RuntimeException)
292     { return obj; }
293 
294     virtual css::uno::Reference< test::javauno::acquire::XBase >
295     SAL_CALL roundTripBaseToBase(
296         css::uno::Reference< test::javauno::acquire::XBase > const & obj)
297         throw (css::uno::RuntimeException)
298     { return obj; }
299 
300     virtual css::uno::Reference< test::javauno::acquire::XBase >
301     SAL_CALL roundTripDerivedToBase(
302         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
303         throw (css::uno::RuntimeException)
304     { return obj.get(); }
305 
306     virtual css::uno::Reference< test::javauno::acquire::XDerived >
307     SAL_CALL roundTripDerivedToDerived(
308         css::uno::Reference< test::javauno::acquire::XDerived > const & obj)
309         throw (css::uno::RuntimeException)
310     { return obj; }
311 
312     static rtl::OUString getImplementationName_static();
313 
314     static css::uno::Sequence< rtl::OUString >
315     getSupportedServiceNames_static();
316 
317     static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
318         css::uno::Reference< css::uno::XComponentContext > const & context)
319         throw (css::uno::Exception);
320 
321 private:
322     explicit Service(
323         css::uno::Reference< css::uno::XComponentContext > const & context):
324         m_context(context) {}
325 
326     css::uno::Reference< css::uno::XComponentContext > m_context;
327     css::uno::Reference< css::uno::XInterface > m_interface;
328     css::uno::Reference< test::javauno::acquire::XBase > m_base;
329     css::uno::Reference< test::javauno::acquire::XDerived > m_derived;
330 };
331 
332 }
333 
334 sal_Bool Service::supportsService(rtl::OUString const & serviceName)
335     throw (css::uno::RuntimeException)
336 {
337     css::uno::Sequence< rtl::OUString > names(
338         getSupportedServiceNames_static());
339     for (sal_Int32 i = 0; i< names.getLength(); ++i) {
340         if (names[i] == serviceName) {
341             return true;
342         }
343     }
344     return false;
345 }
346 
347 namespace {
348 
349 template< typename T > void assertNotNull(css::uno::Reference< T > const & ref)
350 {
351     if (!ref.is()) {
352         std::cerr << "assertNotNull failed\n";
353         std::abort();
354     }
355 }
356 
357 }
358 
359 sal_Int32 Service::run(css::uno::Sequence< rtl::OUString > const & arguments)
360     throw (css::uno::RuntimeException)
361 {
362     // - arguments[0] must be the UNO URL to connect to:
363     css::uno::Reference< XTest > test(
364         css::bridge::UnoUrlResolver::create(m_context)->resolve(arguments[0]),
365         css::uno::UNO_QUERY_THROW);
366 
367     {
368         WaitCondition c;
369         test->setInterfaceToInterface(new Interface(c.get()));
370         assertNotNull(test->getInterfaceFromInterface());
371         test->clearInterface();
372     }
373     {
374         WaitCondition c;
375         test->setInterfaceToInterface(
376             static_cast< Interface * >(new Base(c.get())));
377         assertNotNull(test->getInterfaceFromInterface());
378         test->clearInterface();
379     }
380     {
381         WaitCondition c;
382         test->setInterfaceToInterface(
383             static_cast< Interface * >(new Derived(c.get())));
384         assertNotNull(test->getInterfaceFromInterface());
385         test->clearInterface();
386     }
387 
388     {
389         WaitCondition c;
390         test->setBaseToInterface(new Base(c.get()));
391         assertNotNull(test->getInterfaceFromInterface());
392         test->clearInterface();
393     }
394     {
395         WaitCondition c;
396         test->setBaseToInterface(static_cast< Base * >(new Derived(c.get())));
397         assertNotNull(test->getInterfaceFromInterface());
398         test->clearInterface();
399     }
400 
401     {
402         WaitCondition c;
403         test->setDerivedToInterface(new Derived(c.get()));
404         assertNotNull(test->getInterfaceFromInterface());
405         test->clearInterface();
406     }
407 
408     {
409         WaitCondition c;
410         test->setBaseToBase(new Base(c.get()));
411         assertNotNull(test->getInterfaceFromBase());
412         assertNotNull(test->getBaseFromBase());
413         test->clearBase();
414     }
415     {
416         WaitCondition c;
417         test->setBaseToBase(static_cast< Base * >(new Derived(c.get())));
418         assertNotNull(test->getInterfaceFromBase());
419         assertNotNull(test->getBaseFromBase());
420         test->clearBase();
421     }
422 
423     {
424         WaitCondition c;
425         test->setDerivedToBase(new Derived(c.get()));
426         assertNotNull(test->getInterfaceFromBase());
427         assertNotNull(test->getBaseFromBase());
428         test->clearBase();
429     }
430 
431     {
432         WaitCondition c;
433         test->setDerivedToDerived(new Derived(c.get()));
434         assertNotNull(test->getInterfaceFromDerived());
435         assertNotNull(test->getBaseFromDerived());
436         assertNotNull(test->getDerivedFromDerived());
437         test->clearDerived();
438     }
439 
440     {
441         WaitCondition c;
442         assertNotNull(
443             test->roundTripInterfaceToInterface(new Interface(c.get())));
444     }
445     {
446         WaitCondition c;
447         assertNotNull(test->roundTripInterfaceToInterface(
448                           static_cast< Interface * >(new Base(c.get()))));
449     }
450     {
451         WaitCondition c;
452         assertNotNull(test->roundTripInterfaceToInterface(
453                           static_cast< Interface * >(new Derived(c.get()))));
454     }
455 
456     {
457         WaitCondition c;
458         assertNotNull(test->roundTripBaseToInterface(new Base(c.get())));
459     }
460     {
461         WaitCondition c;
462         assertNotNull(test->roundTripBaseToInterface(
463                           static_cast< Base * >(new Derived(c.get()))));
464     }
465 
466     {
467         WaitCondition c;
468         assertNotNull(test->roundTripDerivedToInterface(new Derived(c.get())));
469     }
470 
471     {
472         WaitCondition c;
473         assertNotNull(test->roundTripBaseToBase(new Base(c.get())));
474     }
475     {
476         WaitCondition c;
477         assertNotNull(test->roundTripBaseToBase(
478                           static_cast< Base * >(new Derived(c.get()))));
479     }
480 
481     {
482         WaitCondition c;
483         assertNotNull(test->roundTripDerivedToBase(new Derived(c.get())));
484     }
485 
486     {
487         WaitCondition c;
488         assertNotNull(test->roundTripDerivedToDerived(new Derived(c.get())));
489     }
490 
491     std::cout << "Client and server both cleanly terminate now: Success\n";
492     return 0;
493 }
494 
495 rtl::OUString Service::getImplementationName_static() {
496     return rtl::OUString::createFromAscii(
497         "com.sun.star.test.bridges.testacquire.impl");
498 }
499 
500 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames_static() {
501     css::uno::Sequence< rtl::OUString > names(1);
502     names[0] = rtl::OUString::createFromAscii(
503         "com.sun.star.test.bridges.testacquire");
504     return names;
505 }
506 
507 css::uno::Reference< css::uno::XInterface > Service::createInstance(
508     css::uno::Reference< css::uno::XComponentContext > const & context)
509     throw (css::uno::Exception)
510 {
511     return static_cast< cppu::OWeakObject * >(new Service(context));
512 }
513 
514 extern "C" void SAL_CALL component_getImplementationEnvironment(
515     char const ** envTypeName, uno_Environment **)
516 {
517     if (envTypeName != 0) {
518         *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
519     }
520 }
521 
522 extern "C" void * SAL_CALL component_getFactory(char const * implName,
523                                                 void * serviceManager, void *) {
524     void * p = 0;
525     if (serviceManager != 0) {
526         css::uno::Reference< css::lang::XSingleComponentFactory > f;
527         if (Service::getImplementationName_static().equalsAscii(implName)) {
528             f = cppu::createSingleComponentFactory(
529                 &Service::createInstance,
530                 Service::getImplementationName_static(),
531                 Service::getSupportedServiceNames_static());
532         }
533         if (f.is()) {
534             f->acquire();
535             p = f.get();
536         }
537     }
538     return p;
539 }
540 
541 namespace {
542 
543 bool writeInfo(void * registryKey, rtl::OUString const & implementationName,
544                css::uno::Sequence< rtl::OUString > const & serviceNames) {
545     rtl::OUString keyName(rtl::OUString::createFromAscii("/"));
546     keyName += implementationName;
547     keyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
548     css::uno::Reference< css::registry::XRegistryKey > key;
549     try {
550         key = static_cast< css::registry::XRegistryKey * >(registryKey)->
551             createKey(keyName);
552     } catch (css::registry::InvalidRegistryException &) {}
553     if (!key.is()) {
554         return false;
555     }
556     bool success = true;
557     for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
558         try {
559             key->createKey(serviceNames[i]);
560         } catch (css::registry::InvalidRegistryException &) {
561             success = false;
562             break;
563         }
564     }
565     return success;
566 }
567 
568 }
569 
570 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) {
571     return registryKey
572         && writeInfo(registryKey, Service::getImplementationName_static(),
573                      Service::getSupportedServiceNames_static());
574 }
575