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