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