1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
25 #include <com/sun/star/bridge/BridgeExistsException.hpp>
26 #include <com/sun/star/connection/XConnection.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/uno/Exception.hpp>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <o3tl/safeint.hxx>
35 #include <rtl/ref.hxx>
36 #include <sal/log.hxx>
37 #include <sal/types.h>
40 #include "bridgefactory.hxx"
44 void BridgeFactory::removeBridge(
45 css::uno::Reference
< css::bridge::XBridge
> const & bridge
)
48 OUString
n(bridge
->getName());
49 osl::MutexGuard
g(m_aMutex
);
52 std::erase(unnamed_
, bridge
);
56 BridgeMap::iterator
i(named_
.find(n
));
57 if (i
!= named_
.end() && i
->second
== bridge
)
62 BridgeFactory::BridgeFactory():
63 BridgeFactoryBase(m_aMutex
)
67 BridgeFactory::~BridgeFactory() {}
69 OUString
BridgeFactory::getImplementationName()
71 return "com.sun.star.comp.bridge.BridgeFactory";
74 sal_Bool
BridgeFactory::supportsService(OUString
const & ServiceName
)
76 return cppu::supportsService(this, ServiceName
);
79 css::uno::Sequence
< OUString
> BridgeFactory::getSupportedServiceNames()
81 return { "com.sun.star.bridge.BridgeFactory" };
84 css::uno::Reference
< css::bridge::XBridge
> BridgeFactory::createBridge(
85 OUString
const & sName
, OUString
const & sProtocol
,
86 css::uno::Reference
< css::connection::XConnection
> const & aConnection
,
87 css::uno::Reference
< css::bridge::XInstanceProvider
> const &
90 rtl::Reference
< Bridge
> b
;
92 osl::MutexGuard
g(m_aMutex
);
93 if (rBHelper
.bDisposed
) {
94 throw css::lang::DisposedException(
95 "BridgeFactory disposed",
98 if (named_
.find(sName
) != named_
.end()) {
99 throw css::bridge::BridgeExistsException(
102 if (sProtocol
!= "urp" || !aConnection
.is()) {
103 throw css::lang::IllegalArgumentException(
104 ("BridgeFactory::createBridge: sProtocol != urp ||"
105 " aConnection == null"),
108 b
.set(new Bridge(this, sName
, aConnection
, anInstanceProvider
));
109 if (sName
.isEmpty()) {
110 unnamed_
.emplace_back(b
.get());
112 named_
[sName
] = b
.get();
119 css::uno::Reference
< css::bridge::XBridge
> BridgeFactory::getBridge(
120 OUString
const & sName
)
122 osl::MutexGuard
g(m_aMutex
);
123 BridgeMap::iterator
i(named_
.find(sName
));
124 return i
== named_
.end()
125 ? css::uno::Reference
< css::bridge::XBridge
>() : i
->second
;
128 css::uno::Sequence
< css::uno::Reference
< css::bridge::XBridge
> >
129 BridgeFactory::getExistingBridges() {
130 osl::MutexGuard
g(m_aMutex
);
131 if (unnamed_
.size() > SAL_MAX_INT32
) {
132 throw css::uno::RuntimeException(
133 "BridgeFactory::getExistingBridges: too many",
136 sal_Int32 n
= static_cast< sal_Int32
>(unnamed_
.size());
137 if (named_
.size() > o3tl::make_unsigned(SAL_MAX_INT32
- n
)) {
138 throw css::uno::RuntimeException(
139 "BridgeFactory::getExistingBridges: too many",
142 n
= static_cast< sal_Int32
>(n
+ named_
.size());
143 css::uno::Sequence
< css::uno::Reference
< css::bridge::XBridge
> > s(n
);
144 auto r
= asNonConstRange(s
);
146 for (auto const& item
: unnamed_
)
149 for (auto const& item
: named_
)
150 r
[i
++] = item
.second
;
155 void BridgeFactory::disposing() {
159 osl::MutexGuard
g(m_aMutex
);
163 for (auto const& item
: l1
)
166 css::uno::Reference
<css::lang::XComponent
>(
167 item
, css::uno::UNO_QUERY_THROW
)->dispose();
168 } catch (css::uno::Exception
& e
) {
169 SAL_WARN("binaryurp", "ignoring " << e
);
172 for (auto const& item
: l2
)
175 css::uno::Reference
<css::lang::XComponent
>(
176 item
.second
, css::uno::UNO_QUERY_THROW
)->dispose();
177 } catch (css::uno::Exception
& e
) {
178 SAL_WARN("binaryurp", "ignoring " << e
);
185 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
186 com_sun_star_comp_bridge_BridgeFactory_get_implementation(
187 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
189 return cppu::acquire(new binaryurp::BridgeFactory
);
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */