Bump version to 4.3-4
[LibreOffice.git] / binaryurp / source / bridgefactory.cxx
blobc3972553c6be1f71d8b83118ac91d77c3611af25
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>
24 #include <exception>
26 #include "com/sun/star/connection/XConnection.hpp"
27 #include "com/sun/star/uno/Exception.hpp"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "com/sun/star/uno/RuntimeException.hpp"
30 #include "com/sun/star/uno/XComponentContext.hpp"
31 #include "com/sun/star/uno/XInterface.hpp"
32 #include "cppuhelper/factory.hxx"
33 #include "cppuhelper/implementationentry.hxx"
34 #include <cppuhelper/supportsservice.hxx>
35 #include "rtl/ref.hxx"
36 #include "sal/types.h"
38 #include "bridge.hxx"
39 #include "bridgefactory.hxx"
41 namespace binaryurp {
43 css::uno::Reference< css::uno::XInterface > BridgeFactory::static_create(
44 css::uno::Reference< css::uno::XComponentContext > const & xContext)
45 SAL_THROW((css::uno::Exception))
47 return static_cast< cppu::OWeakObject * >(new BridgeFactory(xContext));
50 OUString BridgeFactory::static_getImplementationName() {
51 return OUString("com.sun.star.comp.bridge.BridgeFactory");
54 css::uno::Sequence< OUString >
55 BridgeFactory::static_getSupportedServiceNames() {
56 OUString name("com.sun.star.bridge.BridgeFactory");
57 return css::uno::Sequence< OUString >(&name, 1);
60 void BridgeFactory::removeBridge(
61 css::uno::Reference< css::bridge::XBridge > const & bridge)
63 assert(bridge.is());
64 OUString n(bridge->getName());
65 osl::MutexGuard g(m_aMutex);
66 if (n.isEmpty()) {
67 BridgeList::iterator i(
68 std::find(unnamed_.begin(), unnamed_.end(), bridge));
69 if (i != unnamed_.end()) {
70 unnamed_.erase(i);
72 } else {
73 BridgeMap::iterator i(named_.find(n));
74 if (i != named_.end() && i->second == bridge) {
75 named_.erase(i);
80 BridgeFactory::BridgeFactory(
81 css::uno::Reference< css::uno::XComponentContext > const & context):
82 BridgeFactoryBase(m_aMutex), context_(context)
84 assert(context.is());
87 BridgeFactory::~BridgeFactory() {}
89 OUString BridgeFactory::getImplementationName()
90 throw (css::uno::RuntimeException, std::exception)
92 return static_getImplementationName();
95 sal_Bool BridgeFactory::supportsService(OUString const & ServiceName)
96 throw (css::uno::RuntimeException, std::exception)
98 return cppu::supportsService(this, ServiceName);
101 css::uno::Sequence< OUString > BridgeFactory::getSupportedServiceNames()
102 throw (css::uno::RuntimeException, std::exception)
104 return static_getSupportedServiceNames();
107 css::uno::Reference< css::bridge::XBridge > BridgeFactory::createBridge(
108 OUString const & sName, OUString const & sProtocol,
109 css::uno::Reference< css::connection::XConnection > const & aConnection,
110 css::uno::Reference< css::bridge::XInstanceProvider > const &
111 anInstanceProvider)
112 throw (
113 css::bridge::BridgeExistsException, css::lang::IllegalArgumentException,
114 css::uno::RuntimeException, std::exception)
116 rtl::Reference< Bridge > b;
118 osl::MutexGuard g(m_aMutex);
119 if (rBHelper.bDisposed) {
120 throw css::lang::DisposedException(
121 "BridgeFactory disposed",
122 static_cast< cppu::OWeakObject * >(this));
124 if (named_.find(sName) != named_.end()) {
125 throw css::bridge::BridgeExistsException(
126 sName, static_cast< cppu::OWeakObject * >(this));
128 if (sProtocol != "urp" || !aConnection.is()) {
129 throw css::lang::IllegalArgumentException(
130 ("BridgeFactory::createBridge: sProtocol != urp ||"
131 " aConnection == null"),
132 static_cast< cppu::OWeakObject * >(this), -1);
134 b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
135 if (sName.isEmpty()) {
136 unnamed_.push_back(
137 css::uno::Reference< css::bridge::XBridge >(b.get()));
138 } else {
139 named_[sName] = b.get();
142 b->start();
143 return css::uno::Reference< css::bridge::XBridge >(b.get());
146 css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
147 OUString const & sName) throw (css::uno::RuntimeException, std::exception)
149 osl::MutexGuard g(m_aMutex);
150 BridgeMap::iterator i(named_.find(sName));
151 return i == named_.end()
152 ? css::uno::Reference< css::bridge::XBridge >() : i->second;
155 css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
156 BridgeFactory::getExistingBridges() throw (css::uno::RuntimeException, std::exception) {
157 osl::MutexGuard g(m_aMutex);
158 if (unnamed_.size() > SAL_MAX_INT32) {
159 throw css::uno::RuntimeException(
160 "BridgeFactory::getExistingBridges: too many",
161 static_cast< cppu::OWeakObject * >(this));
163 sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
164 if (named_.size() > static_cast< sal_uInt32 >(SAL_MAX_INT32 - n)) {
165 throw css::uno::RuntimeException(
166 "BridgeFactory::getExistingBridges: too many",
167 static_cast< cppu::OWeakObject * >(this));
169 n = static_cast< sal_Int32 >(n + named_.size());
170 css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
171 sal_Int32 i = 0;
172 for (BridgeList::iterator j(unnamed_.begin()); j != unnamed_.end(); ++j) {
173 s[i++] = *j;
175 for (BridgeMap::iterator j(named_.begin()); j != named_.end(); ++j) {
176 s[i++] = j->second;
178 return s;
181 void BridgeFactory::disposing() {
182 BridgeList l1;
183 BridgeMap l2;
185 osl::MutexGuard g(m_aMutex);
186 l1.swap(unnamed_);
187 l2.swap(named_);
189 for (BridgeList::iterator i(l1.begin()); i != l1.end(); ++i) {
190 try {
191 css::uno::Reference<css::lang::XComponent>(
192 *i, css::uno::UNO_QUERY_THROW)->dispose();
193 } catch (css::uno::Exception & e) {
194 SAL_WARN("binaryurp", "ignoring Exception " << e.Message);
197 for (BridgeMap::iterator i(l2.begin()); i != l2.end(); ++i) {
198 try {
199 css::uno::Reference<css::lang::XComponent>(
200 i->second, css::uno::UNO_QUERY_THROW)->dispose();
201 } catch (css::uno::Exception & e) {
202 SAL_WARN("binaryurp", "ignoring Exception " << e.Message);
209 namespace {
211 static cppu::ImplementationEntry const services[] = {
212 { &binaryurp::BridgeFactory::static_create,
213 &binaryurp::BridgeFactory::static_getImplementationName,
214 &binaryurp::BridgeFactory::static_getSupportedServiceNames,
215 &cppu::createOneInstanceComponentFactory, 0, 0 },
216 { 0, 0, 0, 0, 0, 0 }
221 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL binaryurp_component_getFactory(
222 char const * pImplName, void * pServiceManager, void * pRegistryKey)
224 return cppu::component_getFactoryHelper(
225 pImplName, pServiceManager, pRegistryKey, services);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */