xref: /trunk/main/bridges/test/java_uno/nativethreadpool/testnativethreadpoolclient.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
72     virtual sal_Int32 SAL_CALL get();
73 
74     css::uno::Reference< css::uno::XComponentContext > context;
75     osl::ThreadData data;
76 };
77 
run(css::uno::Sequence<rtl::OUString> const &)78 sal_Int32 Client::run(css::uno::Sequence< rtl::OUString > const &)
79 {
80     css::uno::Reference< css::lang::XMultiComponentFactory > factory(
81         context->getServiceManager());
82     if (!factory.is()) {
83         throw new css::uno::RuntimeException(
84             rtl::OUString::createFromAscii(
85                 "no component context service manager"),
86             static_cast< cppu::OWeakObject * >(this));
87     }
88     css::uno::Reference< test::javauno::nativethreadpool::XRelay > relay;
89     try {
90         relay = css::uno::Reference< test::javauno::nativethreadpool::XRelay >(
91             factory->createInstanceWithContext(
92                 rtl::OUString::createFromAscii(
93                     "test.javauno.nativethreadpool.Relay"),
94                 context),
95             css::uno::UNO_QUERY_THROW);
96     } catch (css::uno::RuntimeException &) {
97         throw;
98     } catch (css::uno::Exception & e) {
99         throw css::lang::WrappedTargetRuntimeException(
100             rtl::OUString::createFromAscii(
101                 "creating test.javauno.nativethreadpool.Relay service"),
102             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
103     }
104     relay->start(this);
105     if (!data.setData(reinterpret_cast< void * >(12345))) {
106         throw new css::uno::RuntimeException(
107             rtl::OUString::createFromAscii("osl::ThreadData::setData failed"),
108             static_cast< cppu::OWeakObject * >(this));
109     }
110     css::uno::Reference< test::javauno::nativethreadpool::XSource > source;
111     try {
112         source
113             = css::uno::Reference< test::javauno::nativethreadpool::XSource >(
114                 css::bridge::UnoUrlResolver::create(context)->resolve(
115                     rtl::OUString::createFromAscii(
116                         "uno:socket,host=localhost,port=3830;urp;test")),
117                 css::uno::UNO_QUERY_THROW);
118     } catch (css::connection::NoConnectException & e) {
119         throw css::lang::WrappedTargetRuntimeException(
120             rtl::OUString::createFromAscii(
121                 "com.sun.star.uno.UnoUrlResolver.resolve"),
122             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
123     } catch (css::connection::ConnectionSetupException & e) {
124         throw css::lang::WrappedTargetRuntimeException(
125             rtl::OUString::createFromAscii(
126                 "com.sun.star.uno.UnoUrlResolver.resolve"),
127             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
128     } catch (css::lang::IllegalArgumentException & e) {
129         throw css::lang::WrappedTargetRuntimeException(
130             rtl::OUString::createFromAscii(
131                 "com.sun.star.uno.UnoUrlResolver.resolve"),
132             static_cast< cppu::OWeakObject * >(this), css::uno::makeAny(e));
133     }
134     bool success = source->get() == 12345;
135     std::cout << "success? " << (success ? "yes" : "no") << '\n';
136     return success ? 0 : 1;
137 }
138 
get()139 sal_Int32 Client::get() {
140     return reinterpret_cast< sal_Int32 >(data.getData());
141 }
142 
create(css::uno::Reference<css::uno::XComponentContext> const & context)143 css::uno::Reference< css::uno::XInterface > SAL_CALL create(
144     css::uno::Reference< css::uno::XComponentContext > const & context)
145 {
146     return static_cast< cppu::OWeakObject * >(new Client(context));
147 }
148 
getImplementationName()149 rtl::OUString SAL_CALL getImplementationName() {
150     return rtl::OUString::createFromAscii(
151         "test.javauno.nativethreadpool.client");
152 }
153 
getSupportedServiceNames()154 css::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() {
155     return css::uno::Sequence< rtl::OUString >();
156 }
157 
158 cppu::ImplementationEntry entries[] = {
159     { &create, &getImplementationName, &getSupportedServiceNames,
160       &cppu::createSingleComponentFactory, 0, 0 },
161     { 0, 0, 0, 0, 0, 0 }
162 };
163 
164 }
165 
component_getFactory(char const * implName,void * serviceManager,void * registryKey)166 extern "C" void * SAL_CALL component_getFactory(
167     char const * implName, void * serviceManager, void * registryKey)
168 {
169     return cppu::component_getFactoryHelper(
170         implName, serviceManager, registryKey, entries);
171 }
172 
component_getImplementationEnvironment(char const ** envTypeName,uno_Environment **)173 extern "C" void SAL_CALL component_getImplementationEnvironment(
174     char const ** envTypeName, uno_Environment **)
175 {
176     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
177 }
178