1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include "com/sun/star/bridge/XBridge.hpp"
32 #include "com/sun/star/bridge/XBridgeFactory.hpp"
33 #include "com/sun/star/connection/Connector.hpp"
34 #include "com/sun/star/connection/XConnection.hpp"
35 #include "com/sun/star/connection/XConnector.hpp"
36 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
37 #include "com/sun/star/lang/XServiceInfo.hpp"
38 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
39 #include "com/sun/star/registry/InvalidRegistryException.hpp"
40 #include "com/sun/star/registry/XRegistryKey.hpp"
41 #include "com/sun/star/uno/Exception.hpp"
42 #include "com/sun/star/uno/Reference.hxx"
43 #include "com/sun/star/uno/RuntimeException.hpp"
44 #include "com/sun/star/uno/Sequence.hxx"
45 #include "com/sun/star/uno/XComponentContext.hpp"
46 #include "com/sun/star/uno/XInterface.hpp"
47 #include "cppuhelper/factory.hxx"
48 #include "cppuhelper/implbase2.hxx"
49 #include "cppuhelper/weak.hxx"
50 #include "rtl/string.h"
51 #include "rtl/ustring.hxx"
52 #include "sal/types.h"
53 #include "test/java_uno/equals/XBase.hpp"
54 #include "test/java_uno/equals/XDerived.hpp"
55 #include "test/java_uno/equals/XTestInterface.hpp"
56 #include "uno/environment.h"
57 #include "uno/lbnames.h"
58 
59 namespace css = com::sun::star;
60 
61 namespace {
62 
63 class Service: public cppu::WeakImplHelper2<
64     css::lang::XServiceInfo, test::java_uno::equals::XTestInterface >
65 {
66 public:
67     virtual inline rtl::OUString SAL_CALL getImplementationName()
68         throw (css::uno::RuntimeException)
69     { return rtl::OUString::createFromAscii(getImplementationName_static()); }
70 
71     virtual sal_Bool SAL_CALL supportsService(
72         rtl::OUString const & rServiceName) throw (css::uno::RuntimeException);
73 
74     virtual inline css::uno::Sequence< rtl::OUString > SAL_CALL
75     getSupportedServiceNames()  throw (css::uno::RuntimeException)
76     { return getSupportedServiceNames_static(); }
77 
78     virtual void SAL_CALL connect(rtl::OUString const & rConnection,
79                                   rtl::OUString const & rProtocol)
80         throw (css::uno::Exception);
81 
82     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL get(
83         rtl::OUString const & rName) throw (css::uno::RuntimeException);
84 
85     static inline sal_Char const * getImplementationName_static()
86     { return "com.sun.star.test.bridges.testequals.impl"; }
87 
88     static css::uno::Sequence< rtl::OUString >
89     getSupportedServiceNames_static();
90 
91     static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
92         css::uno::Reference< css::uno::XComponentContext > const & rContext)
93         throw (css::uno::Exception);
94 
95 private:
96     explicit inline Service(
97         css::uno::Reference< css::uno::XComponentContext > const & rContext):
98         m_xContext(rContext) {}
99 
100     css::uno::Reference< css::uno::XComponentContext > m_xContext;
101     css::uno::Reference< css::bridge::XBridge > m_xBridge;
102 };
103 
104 }
105 
106 sal_Bool Service::supportsService(rtl::OUString const & rServiceName)
107     throw (css::uno::RuntimeException)
108 {
109     css::uno::Sequence< rtl::OUString > aNames(
110         getSupportedServiceNames_static());
111     for (sal_Int32 i = 0; i< aNames.getLength(); ++i)
112         if (aNames[i] == rServiceName)
113             return true;
114     return false;
115 }
116 
117 void Service::connect(rtl::OUString const & rConnection,
118                       rtl::OUString const & rProtocol)
119     throw (css::uno::Exception)
120 {
121     css::uno::Reference< css::connection::XConnection > xConnection(
122         css::connection::Connector::create(m_xContext)->connect(rConnection));
123     css::uno::Reference< css::bridge::XBridgeFactory > xBridgeFactory(
124         m_xContext->getServiceManager()->createInstanceWithContext(
125             rtl::OUString::createFromAscii("com.sun.star.bridge.BridgeFactory"),
126             m_xContext),
127         css::uno::UNO_QUERY);
128     m_xBridge = xBridgeFactory->createBridge(rtl::OUString(), rProtocol,
129                                              xConnection, 0);
130 }
131 
132 css::uno::Reference< css::uno::XInterface >
133 Service::get(rtl::OUString const & rName) throw (css::uno::RuntimeException)
134 {
135     return m_xBridge->getInstance(rName);
136 }
137 
138 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames_static()
139 {
140     css::uno::Sequence< rtl::OUString > aNames(1);
141     aNames[0] = rtl::OUString::createFromAscii(
142         "com.sun.star.test.bridges.testequals");
143     return aNames;
144 }
145 
146 css::uno::Reference< css::uno::XInterface > Service::createInstance(
147     css::uno::Reference< css::uno::XComponentContext > const & rContext)
148     throw (css::uno::Exception)
149 {
150     // Make types known:
151     getCppuType(
152         static_cast<
153         css::uno::Reference< test::java_uno::equals::XBase > const * >(0));
154     getCppuType(
155         static_cast<
156         css::uno::Reference< test::java_uno::equals::XDerived > const * >(0));
157     getCppuType(
158         static_cast<
159         css::uno::Reference< test::java_uno::equals::XTestInterface > const * >(
160             0));
161 
162     return static_cast< cppu::OWeakObject * >(new Service(rContext));
163 }
164 
165 extern "C" void SAL_CALL component_getImplementationEnvironment(
166     sal_Char const ** pEnvTypeName, uno_Environment **)
167 {
168     *pEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
169 }
170 
171 extern "C" void * SAL_CALL component_getFactory(sal_Char const * pImplName,
172                                                 void * pServiceManager, void *)
173 {
174     void * pFactory = 0;
175     if (pServiceManager)
176         if (rtl_str_compare(pImplName, Service::getImplementationName_static())
177             == 0)
178         {
179             css::uno::Reference< css::lang::XSingleComponentFactory >
180                 xFactory(cppu::createSingleComponentFactory(
181                              &Service::createInstance,
182                              rtl::OUString::createFromAscii(
183                                  Service::getImplementationName_static()),
184                              Service::getSupportedServiceNames_static()));
185             if (xFactory.is())
186             {
187                 xFactory->acquire();
188                 pFactory = xFactory.get();
189             }
190         }
191     return pFactory;
192 }
193 
194 namespace {
195 
196 bool writeInfo(void * pRegistryKey, sal_Char const * pImplementationName,
197                css::uno::Sequence< rtl::OUString > const & rServiceNames)
198 {
199     rtl::OUString aKeyName(rtl::OUString::createFromAscii("/"));
200     aKeyName += rtl::OUString::createFromAscii(pImplementationName);
201     aKeyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
202     css::uno::Reference< css::registry::XRegistryKey > xKey;
203     try
204     {
205         xKey = static_cast< css::registry::XRegistryKey * >(pRegistryKey)->
206             createKey(aKeyName);
207     }
208     catch (css::registry::InvalidRegistryException &) {}
209     if (!xKey.is())
210         return false;
211     bool bSuccess = true;
212     for (sal_Int32 i = 0; i < rServiceNames.getLength(); ++i)
213         try
214         {
215             xKey->createKey(rServiceNames[i]);
216         }
217         catch (css::registry::InvalidRegistryException &)
218         {
219             bSuccess = false;
220             break;
221         }
222     return bSuccess;
223 }
224 
225 }
226 
227 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * pRegistryKey)
228 {
229     return pRegistryKey
230         && writeInfo(pRegistryKey, Service::getImplementationName_static(),
231                      Service::getSupportedServiceNames_static());
232 }
233