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