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 "com/sun/star/bridge/XBridge.hpp"
28cdf0e10cSrcweir #include "com/sun/star/bridge/XBridgeFactory.hpp"
29cdf0e10cSrcweir #include "com/sun/star/connection/Connector.hpp"
30cdf0e10cSrcweir #include "com/sun/star/connection/XConnection.hpp"
31cdf0e10cSrcweir #include "com/sun/star/connection/XConnector.hpp"
32cdf0e10cSrcweir #include "com/sun/star/lang/XMultiComponentFactory.hpp"
33cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
34cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp"
35cdf0e10cSrcweir #include "com/sun/star/registry/InvalidRegistryException.hpp"
36cdf0e10cSrcweir #include "com/sun/star/registry/XRegistryKey.hpp"
37cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
38cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
39cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
40cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
41cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
42cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
43cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
44cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx"
45cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
46cdf0e10cSrcweir #include "rtl/string.h"
47cdf0e10cSrcweir #include "rtl/ustring.hxx"
48cdf0e10cSrcweir #include "sal/types.h"
49cdf0e10cSrcweir #include "test/java_uno/equals/XBase.hpp"
50cdf0e10cSrcweir #include "test/java_uno/equals/XDerived.hpp"
51cdf0e10cSrcweir #include "test/java_uno/equals/XTestInterface.hpp"
52cdf0e10cSrcweir #include "uno/environment.h"
53cdf0e10cSrcweir #include "uno/lbnames.h"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace css = com::sun::star;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir class Service: public cppu::WeakImplHelper2<
60cdf0e10cSrcweir     css::lang::XServiceInfo, test::java_uno::equals::XTestInterface >
61cdf0e10cSrcweir {
62cdf0e10cSrcweir public:
getImplementationName()63cdf0e10cSrcweir     virtual inline rtl::OUString SAL_CALL getImplementationName()
64cdf0e10cSrcweir         throw (css::uno::RuntimeException)
65cdf0e10cSrcweir     { return rtl::OUString::createFromAscii(getImplementationName_static()); }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService(
68cdf0e10cSrcweir         rtl::OUString const & rServiceName) throw (css::uno::RuntimeException);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     virtual inline css::uno::Sequence< rtl::OUString > SAL_CALL
getSupportedServiceNames()71cdf0e10cSrcweir     getSupportedServiceNames()  throw (css::uno::RuntimeException)
72cdf0e10cSrcweir     { return getSupportedServiceNames_static(); }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     virtual void SAL_CALL connect(rtl::OUString const & rConnection,
75cdf0e10cSrcweir                                   rtl::OUString const & rProtocol)
76cdf0e10cSrcweir         throw (css::uno::Exception);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL get(
79cdf0e10cSrcweir         rtl::OUString const & rName) throw (css::uno::RuntimeException);
80cdf0e10cSrcweir 
getImplementationName_static()81cdf0e10cSrcweir     static inline sal_Char const * getImplementationName_static()
82cdf0e10cSrcweir     { return "com.sun.star.test.bridges.testequals.impl"; }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     static css::uno::Sequence< rtl::OUString >
85cdf0e10cSrcweir     getSupportedServiceNames_static();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
88cdf0e10cSrcweir         css::uno::Reference< css::uno::XComponentContext > const & rContext)
89cdf0e10cSrcweir         throw (css::uno::Exception);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir private:
Service(css::uno::Reference<css::uno::XComponentContext> const & rContext)92cdf0e10cSrcweir     explicit inline Service(
93cdf0e10cSrcweir         css::uno::Reference< css::uno::XComponentContext > const & rContext):
94cdf0e10cSrcweir         m_xContext(rContext) {}
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > m_xContext;
97cdf0e10cSrcweir     css::uno::Reference< css::bridge::XBridge > m_xBridge;
98cdf0e10cSrcweir };
99cdf0e10cSrcweir 
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
supportsService(rtl::OUString const & rServiceName)102cdf0e10cSrcweir sal_Bool Service::supportsService(rtl::OUString const & rServiceName)
103cdf0e10cSrcweir     throw (css::uno::RuntimeException)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > aNames(
106cdf0e10cSrcweir         getSupportedServiceNames_static());
107cdf0e10cSrcweir     for (sal_Int32 i = 0; i< aNames.getLength(); ++i)
108cdf0e10cSrcweir         if (aNames[i] == rServiceName)
109cdf0e10cSrcweir             return true;
110cdf0e10cSrcweir     return false;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
connect(rtl::OUString const & rConnection,rtl::OUString const & rProtocol)113cdf0e10cSrcweir void Service::connect(rtl::OUString const & rConnection,
114cdf0e10cSrcweir                       rtl::OUString const & rProtocol)
115cdf0e10cSrcweir     throw (css::uno::Exception)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     css::uno::Reference< css::connection::XConnection > xConnection(
118cdf0e10cSrcweir         css::connection::Connector::create(m_xContext)->connect(rConnection));
119cdf0e10cSrcweir     css::uno::Reference< css::bridge::XBridgeFactory > xBridgeFactory(
120cdf0e10cSrcweir         m_xContext->getServiceManager()->createInstanceWithContext(
121cdf0e10cSrcweir             rtl::OUString::createFromAscii("com.sun.star.bridge.BridgeFactory"),
122cdf0e10cSrcweir             m_xContext),
123cdf0e10cSrcweir         css::uno::UNO_QUERY);
124cdf0e10cSrcweir     m_xBridge = xBridgeFactory->createBridge(rtl::OUString(), rProtocol,
125cdf0e10cSrcweir                                              xConnection, 0);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface >
get(rtl::OUString const & rName)129cdf0e10cSrcweir Service::get(rtl::OUString const & rName) throw (css::uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     return m_xBridge->getInstance(rName);
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
getSupportedServiceNames_static()134cdf0e10cSrcweir css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames_static()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > aNames(1);
137cdf0e10cSrcweir     aNames[0] = rtl::OUString::createFromAscii(
138cdf0e10cSrcweir         "com.sun.star.test.bridges.testequals");
139cdf0e10cSrcweir     return aNames;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
createInstance(css::uno::Reference<css::uno::XComponentContext> const & rContext)142cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > Service::createInstance(
143cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const & rContext)
144cdf0e10cSrcweir     throw (css::uno::Exception)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     // Make types known:
147cdf0e10cSrcweir     getCppuType(
148cdf0e10cSrcweir         static_cast<
149cdf0e10cSrcweir         css::uno::Reference< test::java_uno::equals::XBase > const * >(0));
150cdf0e10cSrcweir     getCppuType(
151cdf0e10cSrcweir         static_cast<
152cdf0e10cSrcweir         css::uno::Reference< test::java_uno::equals::XDerived > const * >(0));
153cdf0e10cSrcweir     getCppuType(
154cdf0e10cSrcweir         static_cast<
155cdf0e10cSrcweir         css::uno::Reference< test::java_uno::equals::XTestInterface > const * >(
156cdf0e10cSrcweir             0));
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     return static_cast< cppu::OWeakObject * >(new Service(rContext));
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
component_getImplementationEnvironment(sal_Char const ** pEnvTypeName,uno_Environment **)161cdf0e10cSrcweir extern "C" void SAL_CALL component_getImplementationEnvironment(
162cdf0e10cSrcweir     sal_Char const ** pEnvTypeName, uno_Environment **)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     *pEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
component_getFactory(sal_Char const * pImplName,void * pServiceManager,void *)167cdf0e10cSrcweir extern "C" void * SAL_CALL component_getFactory(sal_Char const * pImplName,
168cdf0e10cSrcweir                                                 void * pServiceManager, void *)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     void * pFactory = 0;
171cdf0e10cSrcweir     if (pServiceManager)
172cdf0e10cSrcweir         if (rtl_str_compare(pImplName, Service::getImplementationName_static())
173cdf0e10cSrcweir             == 0)
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             css::uno::Reference< css::lang::XSingleComponentFactory >
176cdf0e10cSrcweir                 xFactory(cppu::createSingleComponentFactory(
177cdf0e10cSrcweir                              &Service::createInstance,
178cdf0e10cSrcweir                              rtl::OUString::createFromAscii(
179cdf0e10cSrcweir                                  Service::getImplementationName_static()),
180cdf0e10cSrcweir                              Service::getSupportedServiceNames_static()));
181cdf0e10cSrcweir             if (xFactory.is())
182cdf0e10cSrcweir             {
183cdf0e10cSrcweir                 xFactory->acquire();
184cdf0e10cSrcweir                 pFactory = xFactory.get();
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     return pFactory;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir namespace {
191cdf0e10cSrcweir 
writeInfo(void * pRegistryKey,sal_Char const * pImplementationName,css::uno::Sequence<rtl::OUString> const & rServiceNames)192cdf0e10cSrcweir bool writeInfo(void * pRegistryKey, sal_Char const * pImplementationName,
193cdf0e10cSrcweir                css::uno::Sequence< rtl::OUString > const & rServiceNames)
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     rtl::OUString aKeyName(rtl::OUString::createFromAscii("/"));
196cdf0e10cSrcweir     aKeyName += rtl::OUString::createFromAscii(pImplementationName);
197cdf0e10cSrcweir     aKeyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
198cdf0e10cSrcweir     css::uno::Reference< css::registry::XRegistryKey > xKey;
199cdf0e10cSrcweir     try
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         xKey = static_cast< css::registry::XRegistryKey * >(pRegistryKey)->
202cdf0e10cSrcweir             createKey(aKeyName);
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir     catch (css::registry::InvalidRegistryException &) {}
205cdf0e10cSrcweir     if (!xKey.is())
206cdf0e10cSrcweir         return false;
207cdf0e10cSrcweir     bool bSuccess = true;
208cdf0e10cSrcweir     for (sal_Int32 i = 0; i < rServiceNames.getLength(); ++i)
209cdf0e10cSrcweir         try
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             xKey->createKey(rServiceNames[i]);
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir         catch (css::registry::InvalidRegistryException &)
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             bSuccess = false;
216cdf0e10cSrcweir             break;
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir     return bSuccess;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
component_writeInfo(void *,void * pRegistryKey)223cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * pRegistryKey)
224cdf0e10cSrcweir {
225cdf0e10cSrcweir     return pRegistryKey
226cdf0e10cSrcweir         && writeInfo(pRegistryKey, Service::getImplementationName_static(),
227cdf0e10cSrcweir                      Service::getSupportedServiceNames_static());
228cdf0e10cSrcweir }
229