Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / binaryurp / source / bridgefactory.cxx
blob24105f30ff9d78b203a1abd37d430107a04630fc
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/connection/XConnection.hpp"
26 #include "com/sun/star/uno/Exception.hpp"
27 #include "com/sun/star/uno/Reference.hxx"
28 #include "com/sun/star/uno/RuntimeException.hpp"
29 #include "com/sun/star/uno/XComponentContext.hpp"
30 #include "com/sun/star/uno/XInterface.hpp"
31 #include "cppuhelper/factory.hxx"
32 #include "cppuhelper/implementationentry.hxx"
33 #include "rtl/ref.hxx"
34 #include "sal/types.h"
36 #include "bridge.hxx"
37 #include "bridgefactory.hxx"
39 namespace binaryurp {
41 css::uno::Reference< css::uno::XInterface > BridgeFactory::static_create(
42 css::uno::Reference< css::uno::XComponentContext > const & xContext)
43 SAL_THROW((css::uno::Exception))
45 return static_cast< cppu::OWeakObject * >(new BridgeFactory(xContext));
48 OUString BridgeFactory::static_getImplementationName() {
49 return OUString("com.sun.star.comp.bridge.BridgeFactory");
52 css::uno::Sequence< OUString >
53 BridgeFactory::static_getSupportedServiceNames() {
54 OUString name("com.sun.star.bridge.BridgeFactory");
55 return css::uno::Sequence< OUString >(&name, 1);
58 void BridgeFactory::removeBridge(
59 css::uno::Reference< css::bridge::XBridge > const & bridge)
61 assert(bridge.is());
62 OUString n(bridge->getName());
63 osl::MutexGuard g(*this);
64 if (n.isEmpty()) {
65 BridgeList::iterator i(
66 std::find(unnamed_.begin(), unnamed_.end(), bridge));
67 if (i != unnamed_.end()) {
68 unnamed_.erase(i);
70 } else {
71 BridgeMap::iterator i(named_.find(n));
72 if (i != named_.end() && i->second == bridge) {
73 named_.erase(i);
78 BridgeFactory::BridgeFactory(
79 css::uno::Reference< css::uno::XComponentContext > const & context):
80 BridgeFactoryBase(*static_cast< osl::Mutex * >(this)), context_(context)
82 assert(context.is());
85 BridgeFactory::~BridgeFactory() {}
87 OUString BridgeFactory::getImplementationName()
88 throw (css::uno::RuntimeException)
90 return static_getImplementationName();
93 sal_Bool BridgeFactory::supportsService(OUString const & ServiceName)
94 throw (css::uno::RuntimeException)
96 css::uno::Sequence< OUString > s(getSupportedServiceNames());
97 for (sal_Int32 i = 0; i != s.getLength(); ++i) {
98 if (ServiceName == s[i]) {
99 return true;
102 return false;
105 css::uno::Sequence< OUString > BridgeFactory::getSupportedServiceNames()
106 throw (css::uno::RuntimeException)
108 return static_getSupportedServiceNames();
111 css::uno::Reference< css::bridge::XBridge > BridgeFactory::createBridge(
112 OUString const & sName, OUString const & sProtocol,
113 css::uno::Reference< css::connection::XConnection > const & aConnection,
114 css::uno::Reference< css::bridge::XInstanceProvider > const &
115 anInstanceProvider)
116 throw (
117 css::bridge::BridgeExistsException, css::lang::IllegalArgumentException,
118 css::uno::RuntimeException)
120 rtl::Reference< Bridge > b;
122 osl::MutexGuard g(*this);
123 if (named_.find(sName) != named_.end()) {
124 throw css::bridge::BridgeExistsException(
125 sName, static_cast< cppu::OWeakObject * >(this));
127 if (sProtocol != "urp" || !aConnection.is()) {
128 throw css::lang::IllegalArgumentException(
129 ("BridgeFactory::createBridge: sProtocol != urp ||"
130 " aConnection == null"),
131 static_cast< cppu::OWeakObject * >(this), -1);
133 b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
134 if (sName.isEmpty()) {
135 unnamed_.push_back(
136 css::uno::Reference< css::bridge::XBridge >(b.get()));
137 } else {
138 named_[sName] = b.get();
141 b->start();
142 return css::uno::Reference< css::bridge::XBridge >(b.get());
145 css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
146 OUString const & sName) throw (css::uno::RuntimeException)
148 osl::MutexGuard g(*this);
149 BridgeMap::iterator i(named_.find(sName));
150 return i == named_.end()
151 ? css::uno::Reference< css::bridge::XBridge >() : i->second;
154 css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
155 BridgeFactory::getExistingBridges() throw (css::uno::RuntimeException) {
156 osl::MutexGuard g(*this);
157 if (unnamed_.size() > SAL_MAX_INT32) {
158 throw css::uno::RuntimeException(
159 "BridgeFactory::getExistingBridges: too many",
160 static_cast< cppu::OWeakObject * >(this));
162 sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
163 if (named_.size() > static_cast< sal_uInt32 >(SAL_MAX_INT32 - n)) {
164 throw css::uno::RuntimeException(
165 "BridgeFactory::getExistingBridges: too many",
166 static_cast< cppu::OWeakObject * >(this));
168 n = static_cast< sal_Int32 >(n + named_.size());
169 css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
170 sal_Int32 i = 0;
171 for (BridgeList::iterator j(unnamed_.begin()); j != unnamed_.end(); ++j) {
172 s[i++] = *j;
174 for (BridgeMap::iterator j(named_.begin()); j != named_.end(); ++j) {
175 s[i++] = j->second;
177 return s;
182 namespace {
184 static cppu::ImplementationEntry const services[] = {
185 { &binaryurp::BridgeFactory::static_create,
186 &binaryurp::BridgeFactory::static_getImplementationName,
187 &binaryurp::BridgeFactory::static_getSupportedServiceNames,
188 &cppu::createOneInstanceComponentFactory, 0, 0 },
189 { 0, 0, 0, 0, 0, 0 }
194 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL binaryurp_component_getFactory(
195 char const * pImplName, void * pServiceManager, void * pRegistryKey)
197 return cppu::component_getFactoryHelper(
198 pImplName, pServiceManager, pRegistryKey, services);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */