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>
26 #include <com/sun/star/bridge/BridgeExistsException.hpp>
27 #include <com/sun/star/connection/XConnection.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/uno/Exception.hpp>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/uno/RuntimeException.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/uno/XInterface.hpp>
34 #include <cppuhelper/factory.hxx>
35 #include <cppuhelper/implementationentry.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <rtl/ref.hxx>
38 #include <sal/log.hxx>
39 #include <sal/types.h>
42 #include "bridgefactory.hxx"
46 css::uno::Reference
< css::uno::XInterface
> BridgeFactory::static_create(
47 css::uno::Reference
< css::uno::XComponentContext
> const & /*xContext*/)
49 return static_cast< cppu::OWeakObject
* >(new BridgeFactory
);
52 OUString
BridgeFactory::static_getImplementationName() {
53 return OUString("com.sun.star.comp.bridge.BridgeFactory");
56 css::uno::Sequence
< OUString
>
57 BridgeFactory::static_getSupportedServiceNames() {
58 return css::uno::Sequence
<OUString
>{ "com.sun.star.bridge.BridgeFactory" };
61 void BridgeFactory::removeBridge(
62 css::uno::Reference
< css::bridge::XBridge
> const & bridge
)
65 OUString
n(bridge
->getName());
66 osl::MutexGuard
g(m_aMutex
);
69 unnamed_
.erase(std::remove(unnamed_
.begin(), unnamed_
.end(), bridge
), unnamed_
.end());
73 BridgeMap::iterator
i(named_
.find(n
));
74 if (i
!= named_
.end() && i
->second
== bridge
)
79 BridgeFactory::BridgeFactory():
80 BridgeFactoryBase(m_aMutex
)
84 BridgeFactory::~BridgeFactory() {}
86 OUString
BridgeFactory::getImplementationName()
88 return static_getImplementationName();
91 sal_Bool
BridgeFactory::supportsService(OUString
const & ServiceName
)
93 return cppu::supportsService(this, ServiceName
);
96 css::uno::Sequence
< OUString
> BridgeFactory::getSupportedServiceNames()
98 return static_getSupportedServiceNames();
101 css::uno::Reference
< css::bridge::XBridge
> BridgeFactory::createBridge(
102 OUString
const & sName
, OUString
const & sProtocol
,
103 css::uno::Reference
< css::connection::XConnection
> const & aConnection
,
104 css::uno::Reference
< css::bridge::XInstanceProvider
> const &
107 rtl::Reference
< Bridge
> b
;
109 osl::MutexGuard
g(m_aMutex
);
110 if (rBHelper
.bDisposed
) {
111 throw css::lang::DisposedException(
112 "BridgeFactory disposed",
113 static_cast< cppu::OWeakObject
* >(this));
115 if (named_
.find(sName
) != named_
.end()) {
116 throw css::bridge::BridgeExistsException(
117 sName
, static_cast< cppu::OWeakObject
* >(this));
119 if (sProtocol
!= "urp" || !aConnection
.is()) {
120 throw css::lang::IllegalArgumentException(
121 ("BridgeFactory::createBridge: sProtocol != urp ||"
122 " aConnection == null"),
123 static_cast< cppu::OWeakObject
* >(this), -1);
125 b
.set(new Bridge(this, sName
, aConnection
, anInstanceProvider
));
126 if (sName
.isEmpty()) {
127 unnamed_
.emplace_back(b
.get());
129 named_
[sName
] = b
.get();
133 return css::uno::Reference
< css::bridge::XBridge
>(b
.get());
136 css::uno::Reference
< css::bridge::XBridge
> BridgeFactory::getBridge(
137 OUString
const & sName
)
139 osl::MutexGuard
g(m_aMutex
);
140 BridgeMap::iterator
i(named_
.find(sName
));
141 return i
== named_
.end()
142 ? css::uno::Reference
< css::bridge::XBridge
>() : i
->second
;
145 css::uno::Sequence
< css::uno::Reference
< css::bridge::XBridge
> >
146 BridgeFactory::getExistingBridges() {
147 osl::MutexGuard
g(m_aMutex
);
148 if (unnamed_
.size() > SAL_MAX_INT32
) {
149 throw css::uno::RuntimeException(
150 "BridgeFactory::getExistingBridges: too many",
151 static_cast< cppu::OWeakObject
* >(this));
153 sal_Int32 n
= static_cast< sal_Int32
>(unnamed_
.size());
154 if (named_
.size() > static_cast< sal_uInt32
>(SAL_MAX_INT32
- n
)) {
155 throw css::uno::RuntimeException(
156 "BridgeFactory::getExistingBridges: too many",
157 static_cast< cppu::OWeakObject
* >(this));
159 n
= static_cast< sal_Int32
>(n
+ named_
.size());
160 css::uno::Sequence
< css::uno::Reference
< css::bridge::XBridge
> > s(n
);
162 for (auto const& item
: unnamed_
)
165 for (auto const& item
: named_
)
166 s
[i
++] = item
.second
;
171 void BridgeFactory::disposing() {
175 osl::MutexGuard
g(m_aMutex
);
179 for (auto const& item
: l1
)
182 css::uno::Reference
<css::lang::XComponent
>(
183 item
, css::uno::UNO_QUERY_THROW
)->dispose();
184 } catch (css::uno::Exception
& e
) {
185 SAL_WARN("binaryurp", "ignoring " << e
);
188 for (auto const& item
: l2
)
191 css::uno::Reference
<css::lang::XComponent
>(
192 item
.second
, css::uno::UNO_QUERY_THROW
)->dispose();
193 } catch (css::uno::Exception
& e
) {
194 SAL_WARN("binaryurp", "ignoring " << e
);
203 static cppu::ImplementationEntry
const services
[] = {
204 { &binaryurp::BridgeFactory::static_create
,
205 &binaryurp::BridgeFactory::static_getImplementationName
,
206 &binaryurp::BridgeFactory::static_getSupportedServiceNames
,
207 &cppu::createOneInstanceComponentFactory
, nullptr, 0 },
208 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
213 extern "C" SAL_DLLPUBLIC_EXPORT
void * binaryurp_component_getFactory(
214 char const * pImplName
, void * pServiceManager
, void * pRegistryKey
)
216 return cppu::component_getFactoryHelper(
217 pImplName
, pServiceManager
, pRegistryKey
, services
);
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */