1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26 
27 #include "test/javauno/nativethreadpool/XRelay.hpp"
28 #include "test/javauno/nativethreadpool/XSource.hpp"
29 
30 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
31 #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
32 #include "com/sun/star/connection/ConnectionSetupException.hpp"
33 #include "com/sun/star/connection/NoConnectException.hpp"
34 #include "com/sun/star/lang/IllegalArgumentException.hpp"
35 #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
36 #include "com/sun/star/lang/XMain.hpp"
37 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
38 #include "com/sun/star/uno/Exception.hpp"
39 #include "com/sun/star/uno/Reference.hxx"
40 #include "com/sun/star/uno/RuntimeException.hpp"
41 #include "com/sun/star/uno/Sequence.hxx"
42 #include "com/sun/star/uno/XComponentContext.hpp"
43 #include "com/sun/star/uno/XInterface.hpp"
44 #include "cppuhelper/factory.hxx"
45 #include "cppuhelper/implbase2.hxx"
46 #include "cppuhelper/implementationentry.hxx"
47 #include "cppuhelper/weak.hxx"
48 #include "osl/thread.hxx"
49 #include "rtl/ustring.hxx"
50 #include "sal/types.h"
51 #include "uno/lbnames.h"
52 
53 #include <iostream>
54 
55 namespace css = com::sun::star;
56 
57 namespace {
58 
59 class Client: public cppu::WeakImplHelper2<
60     css::lang::XMain, test::javauno::nativethreadpool::XSource >
61 {
62 public:
Client(css::uno::Reference<css::uno::XComponentContext> const & theContext)63     explicit Client(
64         css::uno::Reference< css::uno::XComponentContext > const & theContext):
65         context(theContext) {}
66 
67 private:
~Client()68     virtual ~Client() {}
69 
70     virtual sal_Int32 SAL_CALL run(css::uno::Sequence< rtl::OUString > const &)
71         throw (css::uno::RuntimeException);
72 
73     virtual sal_Int32 SAL_CALL get() throw (css::uno::RuntimeException);
74 
75     css::uno::Reference< css::uno::XComponentContext > context;
76     osl::ThreadData data;
77 };
78 
run(css::uno::Sequence<rtl::OUString> const &)79 sal_Int32 Client::run(css::uno::Sequence< rtl::OUString > const &)
80     throw (css::uno::RuntimeException)
81 {
82     css::uno::Reference< css::lang::XMultiComponentFactory > factory(
83         context->getServiceManager());
84     if (!factory.is()) {
85         throw new css::uno::RuntimeException(
86             rtl::OUString::createFromAscii(
87                 "no component context service manager"),
88             static_cast< cppu::OWeakObject * >(this));
89     }
90     css::uno::Reference< test::javauno::nativethreadpool::XRelay > relay;
91     try {
92         relay = css::uno::Reference< test::javauno::nativethreadpool::XRelay >(
93             factory->createInstanceWithContext(
94                 rtl::OUString::createFromAscii(
95                     "test.javauno.nativethreadpool.Relay"),
96                 context),
97             css::uno::UNO_QUERY_THROW);
98     } catch (css::uno::RuntimeException &) {
99         throw;
100     } catch (css::uno::Exception & e) {
101         throw css::lang::WrappedTargetRuntimeException(
102             rtl::OUString::createFromAscii(
103                 "creating test.javauno.nativethreadpool.Relay service"),
104             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
105     }
106     relay->start(this);
107     if (!data.setData(reinterpret_cast< void * >(12345))) {
108         throw new css::uno::RuntimeException(
109             rtl::OUString::createFromAscii("osl::ThreadData::setData failed"),
110             static_cast< cppu::OWeakObject * >(this));
111     }
112     css::uno::Reference< test::javauno::nativethreadpool::XSource > source;
113     try {
114         source
115             = css::uno::Reference< test::javauno::nativethreadpool::XSource >(
116                 css::bridge::UnoUrlResolver::create(context)->resolve(
117                     rtl::OUString::createFromAscii(
118                         "uno:socket,host=localhost,port=3830;urp;test")),
119                 css::uno::UNO_QUERY_THROW);
120     } catch (css::connection::NoConnectException & e) {
121         throw css::lang::WrappedTargetRuntimeException(
122             rtl::OUString::createFromAscii(
123                 "com.sun.star.uno.UnoUrlResolver.resolve"),
124             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
125     } catch (css::connection::ConnectionSetupException & e) {
126         throw css::lang::WrappedTargetRuntimeException(
127             rtl::OUString::createFromAscii(
128                 "com.sun.star.uno.UnoUrlResolver.resolve"),
129             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
130     } catch (css::lang::IllegalArgumentException & e) {
131         throw css::lang::WrappedTargetRuntimeException(
132             rtl::OUString::createFromAscii(
133                 "com.sun.star.uno.UnoUrlResolver.resolve"),
134             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
135     }
136     bool success = source->get() == 12345;
137     std::cout << "success? " << (success ? "yes" : "no") << '\n';
138     return success ? 0 : 1;
139 }
140 
get()141 sal_Int32 Client::get() throw (css::uno::RuntimeException) {
142     return reinterpret_cast< sal_Int32 >(data.getData());
143 }
144 
create(css::uno::Reference<css::uno::XComponentContext> const & context)145 css::uno::Reference< css::uno::XInterface > SAL_CALL create(
146     css::uno::Reference< css::uno::XComponentContext > const & context)
147     SAL_THROW((css::uno::Exception))
148 {
149     return static_cast< cppu::OWeakObject * >(new Client(context));
150 }
151 
getImplementationName()152 rtl::OUString SAL_CALL getImplementationName() {
153     return rtl::OUString::createFromAscii(
154         "test.javauno.nativethreadpool.client");
155 }
156 
getSupportedServiceNames()157 css::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() {
158     return css::uno::Sequence< rtl::OUString >();
159 }
160 
161 cppu::ImplementationEntry entries[] = {
162     { &create, &getImplementationName, &getSupportedServiceNames,
163       &cppu::createSingleComponentFactory, 0, 0 },
164     { 0, 0, 0, 0, 0, 0 }
165 };
166 
167 }
168 
component_getFactory(char const * implName,void * serviceManager,void * registryKey)169 extern "C" void * SAL_CALL component_getFactory(
170     char const * implName, void * serviceManager, void * registryKey)
171 {
172     return cppu::component_getFactoryHelper(
173         implName, serviceManager, registryKey, entries);
174 }
175 
component_getImplementationEnvironment(char const ** envTypeName,uno_Environment **)176 extern "C" void SAL_CALL component_getImplementationEnvironment(
177     char const ** envTypeName, uno_Environment **)
178 {
179     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
180 }
181