turn gb_CustomTarget_get_workdir into error and remove old _repo_targets
[LibreOffice.git] / binaryurp / source / bridgefactory.cxx
blobcd43fe592b0e62d7b86e7878b35c6c7a47360619
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <algorithm>
23 #include <cassert>
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>
39 #include "bridge.hxx"
40 #include "bridgefactory.hxx"
42 namespace binaryurp {
44 void BridgeFactory::removeBridge(
45 css::uno::Reference< css::bridge::XBridge > const & bridge)
47 assert(bridge.is());
48 OUString n(bridge->getName());
49 osl::MutexGuard g(m_aMutex);
50 if (n.isEmpty())
52 std::erase(unnamed_, bridge);
54 else
56 BridgeMap::iterator i(named_.find(n));
57 if (i != named_.end() && i->second == bridge)
58 named_.erase(i);
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 &
88 anInstanceProvider)
90 rtl::Reference< Bridge > b;
92 osl::MutexGuard g(m_aMutex);
93 if (rBHelper.bDisposed) {
94 throw css::lang::DisposedException(
95 "BridgeFactory disposed",
96 getXWeak());
98 if (named_.find(sName) != named_.end()) {
99 throw css::bridge::BridgeExistsException(
100 sName, getXWeak());
102 if (sProtocol != "urp" || !aConnection.is()) {
103 throw css::lang::IllegalArgumentException(
104 ("BridgeFactory::createBridge: sProtocol != urp ||"
105 " aConnection == null"),
106 getXWeak(), -1);
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();
115 b->start();
116 return b;
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",
134 getXWeak());
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());
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;
149 for (auto const& item : named_)
150 r[i++] = item.second;
152 return s;
155 void BridgeFactory::disposing() {
156 BridgeVector l1;
157 BridgeMap l2;
159 osl::MutexGuard g(m_aMutex);
160 l1.swap(unnamed_);
161 l2.swap(named_);
163 for (auto const& item : l1)
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);
172 for (auto const& item : l2)
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);
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: */