xref: /AOO41X/main/binaryurp/source/bridgefactory.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Copyright 2000, 2011 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 #include "sal/config.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <algorithm>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "com/sun/star/connection/XConnection.hpp"
33*cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
34*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
35*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
36*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
38*cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
39*cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
40*cdf0e10cSrcweir #include "osl/diagnose.h"
41*cdf0e10cSrcweir #include "rtl/ref.hxx"
42*cdf0e10cSrcweir #include "sal/types.h"
43*cdf0e10cSrcweir #include "uno/lbnames.h"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include "bridge.hxx"
46*cdf0e10cSrcweir #include "bridgefactory.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace binaryurp {
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir namespace css = com::sun::star;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > BridgeFactory::static_create(
57*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const & xContext)
58*cdf0e10cSrcweir     SAL_THROW((css::uno::Exception))
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     return static_cast< cppu::OWeakObject * >(new BridgeFactory(xContext));
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir rtl::OUString BridgeFactory::static_getImplementationName() {
64*cdf0e10cSrcweir     return rtl::OUString(
65*cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
66*cdf0e10cSrcweir             "com.sun.star.comp.bridge.BridgeFactory"));
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir css::uno::Sequence< rtl::OUString >
70*cdf0e10cSrcweir BridgeFactory::static_getSupportedServiceNames() {
71*cdf0e10cSrcweir     rtl::OUString name(
72*cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory"));
73*cdf0e10cSrcweir     return css::uno::Sequence< rtl::OUString >(&name, 1);
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir void BridgeFactory::removeBridge(
77*cdf0e10cSrcweir     css::uno::Reference< css::bridge::XBridge > const & bridge)
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir     OSL_ASSERT(bridge.is());
80*cdf0e10cSrcweir     rtl::OUString n(bridge->getName());
81*cdf0e10cSrcweir     osl::MutexGuard g(*this);
82*cdf0e10cSrcweir     if (n.getLength() == 0) {
83*cdf0e10cSrcweir         BridgeList::iterator i(
84*cdf0e10cSrcweir             std::find(unnamed_.begin(), unnamed_.end(), bridge));
85*cdf0e10cSrcweir         if (i != unnamed_.end()) {
86*cdf0e10cSrcweir             unnamed_.erase(i);
87*cdf0e10cSrcweir         }
88*cdf0e10cSrcweir     } else {
89*cdf0e10cSrcweir         BridgeMap::iterator i(named_.find(n));
90*cdf0e10cSrcweir         if (i != named_.end() && i->second == bridge) {
91*cdf0e10cSrcweir             named_.erase(i);
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir BridgeFactory::BridgeFactory(
97*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const & context):
98*cdf0e10cSrcweir     BridgeFactoryBase(*static_cast< osl::Mutex * >(this)), context_(context)
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     OSL_ASSERT(context.is());
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir BridgeFactory::~BridgeFactory() {}
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir rtl::OUString BridgeFactory::getImplementationName()
106*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     return static_getImplementationName();
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir sal_Bool BridgeFactory::supportsService(rtl::OUString const & ServiceName)
112*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > s(getSupportedServiceNames());
115*cdf0e10cSrcweir     for (sal_Int32 i = 0; i != s.getLength(); ++i) {
116*cdf0e10cSrcweir         if (ServiceName == s[i]) {
117*cdf0e10cSrcweir             return true;
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir     return false;
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir css::uno::Sequence< rtl::OUString > BridgeFactory::getSupportedServiceNames()
124*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     return static_getSupportedServiceNames();
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir css::uno::Reference< css::bridge::XBridge > BridgeFactory::createBridge(
130*cdf0e10cSrcweir     rtl::OUString const & sName, rtl::OUString const & sProtocol,
131*cdf0e10cSrcweir     css::uno::Reference< css::connection::XConnection > const & aConnection,
132*cdf0e10cSrcweir     css::uno::Reference< css::bridge::XInstanceProvider > const &
133*cdf0e10cSrcweir         anInstanceProvider)
134*cdf0e10cSrcweir     throw (
135*cdf0e10cSrcweir         css::bridge::BridgeExistsException, css::lang::IllegalArgumentException,
136*cdf0e10cSrcweir         css::uno::RuntimeException)
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     rtl::Reference< Bridge > b;
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         osl::MutexGuard g(*this);
141*cdf0e10cSrcweir         if (named_.find(sName) != named_.end()) {
142*cdf0e10cSrcweir             throw css::bridge::BridgeExistsException(
143*cdf0e10cSrcweir                 sName, static_cast< cppu::OWeakObject * >(this));
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir         if (!(sProtocol.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("urp")) &&
146*cdf0e10cSrcweir               aConnection.is()))
147*cdf0e10cSrcweir         {
148*cdf0e10cSrcweir             throw css::lang::IllegalArgumentException(
149*cdf0e10cSrcweir                 rtl::OUString(
150*cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM(
151*cdf0e10cSrcweir                         "BridgeFactory::createBridge: sProtocol != urp ||"
152*cdf0e10cSrcweir                         " aConnection == null")),
153*cdf0e10cSrcweir                 static_cast< cppu::OWeakObject * >(this), -1);
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
156*cdf0e10cSrcweir         if (sName.getLength() == 0) {
157*cdf0e10cSrcweir             unnamed_.push_back(
158*cdf0e10cSrcweir                 css::uno::Reference< css::bridge::XBridge >(b.get()));
159*cdf0e10cSrcweir         } else {
160*cdf0e10cSrcweir             named_[sName] = b.get();
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir     b->start();
164*cdf0e10cSrcweir     return css::uno::Reference< css::bridge::XBridge >(b.get());
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
168*cdf0e10cSrcweir     rtl::OUString const & sName) throw (css::uno::RuntimeException)
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir     osl::MutexGuard g(*this);
171*cdf0e10cSrcweir     BridgeMap::iterator i(named_.find(sName));
172*cdf0e10cSrcweir     return i == named_.end()
173*cdf0e10cSrcweir         ? css::uno::Reference< css::bridge::XBridge >() : i->second;
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
177*cdf0e10cSrcweir BridgeFactory::getExistingBridges() throw (css::uno::RuntimeException) {
178*cdf0e10cSrcweir     osl::MutexGuard g(*this);
179*cdf0e10cSrcweir     if (unnamed_.size() > SAL_MAX_INT32) {
180*cdf0e10cSrcweir         throw css::uno::RuntimeException(
181*cdf0e10cSrcweir             rtl::OUString(
182*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
183*cdf0e10cSrcweir                     "BridgeFactory::getExistingBridges: too many")),
184*cdf0e10cSrcweir             static_cast< cppu::OWeakObject * >(this));
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir     sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
187*cdf0e10cSrcweir     if (named_.size() > static_cast< sal_uInt32 >(SAL_MAX_INT32 - n)) {
188*cdf0e10cSrcweir         throw css::uno::RuntimeException(
189*cdf0e10cSrcweir             rtl::OUString(
190*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
191*cdf0e10cSrcweir                     "BridgeFactory::getExistingBridges: too many")),
192*cdf0e10cSrcweir             static_cast< cppu::OWeakObject * >(this));
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir     n = static_cast< sal_Int32 >(n + named_.size());
195*cdf0e10cSrcweir     css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
196*cdf0e10cSrcweir     sal_Int32 i = 0;
197*cdf0e10cSrcweir     for (BridgeList::iterator j(unnamed_.begin()); j != unnamed_.end(); ++j) {
198*cdf0e10cSrcweir         s[i++] = *j;
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir     for (BridgeMap::iterator j(named_.begin()); j != named_.end(); ++j) {
201*cdf0e10cSrcweir         s[i++] = j->second;
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir     return s;
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir namespace {
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir static cppu::ImplementationEntry const services[] = {
211*cdf0e10cSrcweir     { &binaryurp::BridgeFactory::static_create,
212*cdf0e10cSrcweir       &binaryurp::BridgeFactory::static_getImplementationName,
213*cdf0e10cSrcweir       &binaryurp::BridgeFactory::static_getSupportedServiceNames,
214*cdf0e10cSrcweir       &cppu::createSingleComponentFactory, 0, 0 },
215*cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
216*cdf0e10cSrcweir };
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
221*cdf0e10cSrcweir     char const * pImplName, void * pServiceManager, void * pRegistryKey)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     return cppu::component_getFactoryHelper(
224*cdf0e10cSrcweir         pImplName, pServiceManager, pRegistryKey, services);
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL
228*cdf0e10cSrcweir component_getImplementationEnvironment(
229*cdf0e10cSrcweir     char const ** ppEnvTypeName, uno_Environment **)
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
232*cdf0e10cSrcweir }
233