LibreOffice Module binaryurp (master) 1
bridgefactory.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <algorithm>
23#include <cassert>
24
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>
34#include <o3tl/safeint.hxx>
35#include <rtl/ref.hxx>
36#include <sal/log.hxx>
37#include <sal/types.h>
38
39#include "bridge.hxx"
40#include "bridgefactory.hxx"
41
42namespace binaryurp {
43
45 css::uno::Reference< css::bridge::XBridge > const & bridge)
46{
47 assert(bridge.is());
48 OUString n(bridge->getName());
49 osl::MutexGuard g(m_aMutex);
50 if (n.isEmpty())
51 {
52 unnamed_.erase(std::remove(unnamed_.begin(), unnamed_.end(), bridge), unnamed_.end());
53 }
54 else
55 {
56 BridgeMap::iterator i(named_.find(n));
57 if (i != named_.end() && i->second == bridge)
58 named_.erase(i);
59 }
60}
61
64{
65}
66
68
70{
71 return "com.sun.star.comp.bridge.BridgeFactory";
72}
73
74sal_Bool BridgeFactory::supportsService(OUString const & ServiceName)
75{
77}
78
79css::uno::Sequence< OUString > BridgeFactory::getSupportedServiceNames()
80{
81 return { "com.sun.star.bridge.BridgeFactory" };
82}
83
84css::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 &
88 anInstanceProvider)
89{
91 {
92 osl::MutexGuard g(m_aMutex);
93 if (rBHelper.bDisposed) {
94 throw css::lang::DisposedException(
95 "BridgeFactory disposed",
96 getXWeak());
97 }
98 if (named_.find(sName) != named_.end()) {
99 throw css::bridge::BridgeExistsException(
100 sName, getXWeak());
101 }
102 if (sProtocol != "urp" || !aConnection.is()) {
103 throw css::lang::IllegalArgumentException(
104 ("BridgeFactory::createBridge: sProtocol != urp ||"
105 " aConnection == null"),
106 getXWeak(), -1);
107 }
108 b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
109 if (sName.isEmpty()) {
110 unnamed_.emplace_back(b.get());
111 } else {
112 named_[sName] = b.get();
113 }
114 }
115 b->start();
116 return b;
117}
118
119css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
120 OUString const & sName)
121{
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;
126}
127
128css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
130 osl::MutexGuard g(m_aMutex);
131 if (unnamed_.size() > SAL_MAX_INT32) {
132 throw css::uno::RuntimeException(
133 "BridgeFactory::getExistingBridges: too many",
134 getXWeak());
135 }
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",
140 getXWeak());
141 }
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);
145 sal_Int32 i = 0;
146 for (auto const& item : unnamed_)
147 r[i++] = item;
148
149 for (auto const& item : named_)
150 r[i++] = item.second;
151
152 return s;
153}
154
156 BridgeVector l1;
157 BridgeMap l2;
158 {
159 osl::MutexGuard g(m_aMutex);
160 l1.swap(unnamed_);
161 l2.swap(named_);
162 }
163 for (auto const& item : l1)
164 {
165 try {
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);
170 }
171 }
172 for (auto const& item : l2)
173 {
174 try {
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);
179 }
180 }
181}
182
183}
184
185extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
187 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
188{
189 return cppu::acquire(new binaryurp::BridgeFactory);
190}
191
192/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_bridge_BridgeFactory_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
std::map< OUString, com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > > BridgeMap
virtual com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
void SAL_CALL disposing() override
virtual com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > SAL_CALL createBridge(OUString const &sName, OUString const &sProtocol, com::sun::star::uno::Reference< com::sun::star::connection::XConnection > const &aConnection, com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider > const &anInstanceProvider) override
virtual sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
virtual com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > SAL_CALL getBridge(OUString const &sName) override
void removeBridge(com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > const &bridge)
virtual ~BridgeFactory() override
std::vector< com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > > BridgeVector
virtual com::sun::star::uno::Sequence< com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > > SAL_CALL getExistingBridges() override
mutable::osl::Mutex m_aMutex
OUString sName
std::mutex m_aMutex
sal_Int64 n
#define SAL_WARN(area, stream)
cppu::WeakComponentImplHelper< com::sun::star::lang::XServiceInfo, com::sun::star::bridge::XBridgeFactory2 > BridgeFactoryBase
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
#define SAL_MAX_INT32
unsigned char sal_Bool