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 "test/javauno/nativethreadpool/XRelay.hpp"
32 #include "test/javauno/nativethreadpool/XSource.hpp"
33 
34 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
35 #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
36 #include "com/sun/star/connection/ConnectionSetupException.hpp"
37 #include "com/sun/star/connection/NoConnectException.hpp"
38 #include "com/sun/star/lang/IllegalArgumentException.hpp"
39 #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
40 #include "com/sun/star/lang/XMain.hpp"
41 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
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/XComponentContext.hpp"
47 #include "com/sun/star/uno/XInterface.hpp"
48 #include "cppuhelper/factory.hxx"
49 #include "cppuhelper/implbase2.hxx"
50 #include "cppuhelper/implementationentry.hxx"
51 #include "cppuhelper/weak.hxx"
52 #include "osl/thread.hxx"
53 #include "rtl/ustring.hxx"
54 #include "sal/types.h"
55 #include "uno/lbnames.h"
56 
57 #include <iostream>
58 
59 namespace css = com::sun::star;
60 
61 namespace {
62 
63 class Client: public cppu::WeakImplHelper2<
64     css::lang::XMain, test::javauno::nativethreadpool::XSource >
65 {
66 public:
67     explicit Client(
68         css::uno::Reference< css::uno::XComponentContext > const & theContext):
69         context(theContext) {}
70 
71 private:
72     virtual ~Client() {}
73 
74     virtual sal_Int32 SAL_CALL run(css::uno::Sequence< rtl::OUString > const &)
75         throw (css::uno::RuntimeException);
76 
77     virtual sal_Int32 SAL_CALL get() throw (css::uno::RuntimeException);
78 
79     css::uno::Reference< css::uno::XComponentContext > context;
80     osl::ThreadData data;
81 };
82 
83 sal_Int32 Client::run(css::uno::Sequence< rtl::OUString > const &)
84     throw (css::uno::RuntimeException)
85 {
86     css::uno::Reference< css::lang::XMultiComponentFactory > factory(
87         context->getServiceManager());
88     if (!factory.is()) {
89         throw new css::uno::RuntimeException(
90             rtl::OUString::createFromAscii(
91                 "no component context service manager"),
92             static_cast< cppu::OWeakObject * >(this));
93     }
94     css::uno::Reference< test::javauno::nativethreadpool::XRelay > relay;
95     try {
96         relay = css::uno::Reference< test::javauno::nativethreadpool::XRelay >(
97             factory->createInstanceWithContext(
98                 rtl::OUString::createFromAscii(
99                     "test.javauno.nativethreadpool.Relay"),
100                 context),
101             css::uno::UNO_QUERY_THROW);
102     } catch (css::uno::RuntimeException &) {
103         throw;
104     } catch (css::uno::Exception & e) {
105         throw css::lang::WrappedTargetRuntimeException(
106             rtl::OUString::createFromAscii(
107                 "creating test.javauno.nativethreadpool.Relay service"),
108             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
109     }
110     relay->start(this);
111     if (!data.setData(reinterpret_cast< void * >(12345))) {
112         throw new css::uno::RuntimeException(
113             rtl::OUString::createFromAscii("osl::ThreadData::setData failed"),
114             static_cast< cppu::OWeakObject * >(this));
115     }
116     css::uno::Reference< test::javauno::nativethreadpool::XSource > source;
117     try {
118         source
119             = css::uno::Reference< test::javauno::nativethreadpool::XSource >(
120                 css::bridge::UnoUrlResolver::create(context)->resolve(
121                     rtl::OUString::createFromAscii(
122                         "uno:socket,host=localhost,port=3830;urp;test")),
123                 css::uno::UNO_QUERY_THROW);
124     } catch (css::connection::NoConnectException & e) {
125         throw css::lang::WrappedTargetRuntimeException(
126             rtl::OUString::createFromAscii(
127                 "com.sun.star.uno.UnoUrlResolver.resolve"),
128             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
129     } catch (css::connection::ConnectionSetupException & e) {
130         throw css::lang::WrappedTargetRuntimeException(
131             rtl::OUString::createFromAscii(
132                 "com.sun.star.uno.UnoUrlResolver.resolve"),
133             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
134     } catch (css::lang::IllegalArgumentException & e) {
135         throw css::lang::WrappedTargetRuntimeException(
136             rtl::OUString::createFromAscii(
137                 "com.sun.star.uno.UnoUrlResolver.resolve"),
138             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
139     }
140     bool success = source->get() == 12345;
141     std::cout << "success? " << (success ? "yes" : "no") << '\n';
142     return success ? 0 : 1;
143 }
144 
145 sal_Int32 Client::get() throw (css::uno::RuntimeException) {
146     return reinterpret_cast< sal_Int32 >(data.getData());
147 }
148 
149 css::uno::Reference< css::uno::XInterface > SAL_CALL create(
150     css::uno::Reference< css::uno::XComponentContext > const & context)
151     SAL_THROW((css::uno::Exception))
152 {
153     return static_cast< cppu::OWeakObject * >(new Client(context));
154 }
155 
156 rtl::OUString SAL_CALL getImplementationName() {
157     return rtl::OUString::createFromAscii(
158         "test.javauno.nativethreadpool.client");
159 }
160 
161 css::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() {
162     return css::uno::Sequence< rtl::OUString >();
163 }
164 
165 cppu::ImplementationEntry entries[] = {
166     { &create, &getImplementationName, &getSupportedServiceNames,
167       &cppu::createSingleComponentFactory, 0, 0 },
168     { 0, 0, 0, 0, 0, 0 }
169 };
170 
171 }
172 
173 extern "C" void * SAL_CALL component_getFactory(
174     char const * implName, void * serviceManager, void * registryKey)
175 {
176     return cppu::component_getFactoryHelper(
177         implName, serviceManager, registryKey, entries);
178 }
179 
180 extern "C" void SAL_CALL component_getImplementationEnvironment(
181     char const ** envTypeName, uno_Environment **)
182 {
183     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
184 }
185