Line data Source code
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 : #include <exception>
25 :
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"
37 :
38 : #include "bridge.hxx"
39 : #include "bridgefactory.hxx"
40 :
41 : namespace binaryurp {
42 :
43 0 : css::uno::Reference< css::uno::XInterface > BridgeFactory::static_create(
44 : css::uno::Reference< css::uno::XComponentContext > const & xContext)
45 : SAL_THROW((css::uno::Exception))
46 : {
47 0 : return static_cast< cppu::OWeakObject * >(new BridgeFactory(xContext));
48 : }
49 :
50 0 : OUString BridgeFactory::static_getImplementationName() {
51 0 : return OUString("com.sun.star.comp.bridge.BridgeFactory");
52 : }
53 :
54 : css::uno::Sequence< OUString >
55 0 : BridgeFactory::static_getSupportedServiceNames() {
56 0 : OUString name("com.sun.star.bridge.BridgeFactory");
57 0 : return css::uno::Sequence< OUString >(&name, 1);
58 : }
59 :
60 0 : void BridgeFactory::removeBridge(
61 : css::uno::Reference< css::bridge::XBridge > const & bridge)
62 : {
63 : assert(bridge.is());
64 0 : OUString n(bridge->getName());
65 0 : osl::MutexGuard g(m_aMutex);
66 0 : if (n.isEmpty()) {
67 : BridgeList::iterator i(
68 0 : std::find(unnamed_.begin(), unnamed_.end(), bridge));
69 0 : if (i != unnamed_.end()) {
70 0 : unnamed_.erase(i);
71 : }
72 : } else {
73 0 : BridgeMap::iterator i(named_.find(n));
74 0 : if (i != named_.end() && i->second == bridge) {
75 0 : named_.erase(i);
76 : }
77 0 : }
78 0 : }
79 :
80 0 : BridgeFactory::BridgeFactory(
81 : css::uno::Reference< css::uno::XComponentContext > const & context):
82 0 : BridgeFactoryBase(m_aMutex), context_(context)
83 : {
84 : assert(context.is());
85 0 : }
86 :
87 0 : BridgeFactory::~BridgeFactory() {}
88 :
89 0 : OUString BridgeFactory::getImplementationName()
90 : throw (css::uno::RuntimeException, std::exception)
91 : {
92 0 : return static_getImplementationName();
93 : }
94 :
95 0 : sal_Bool BridgeFactory::supportsService(OUString const & ServiceName)
96 : throw (css::uno::RuntimeException, std::exception)
97 : {
98 0 : return cppu::supportsService(this, ServiceName);
99 : }
100 :
101 0 : css::uno::Sequence< OUString > BridgeFactory::getSupportedServiceNames()
102 : throw (css::uno::RuntimeException, std::exception)
103 : {
104 0 : return static_getSupportedServiceNames();
105 : }
106 :
107 0 : 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)
115 : {
116 0 : rtl::Reference< Bridge > b;
117 : {
118 0 : osl::MutexGuard g(m_aMutex);
119 0 : if (named_.find(sName) != named_.end()) {
120 : throw css::bridge::BridgeExistsException(
121 0 : sName, static_cast< cppu::OWeakObject * >(this));
122 : }
123 0 : if (sProtocol != "urp" || !aConnection.is()) {
124 : throw css::lang::IllegalArgumentException(
125 : ("BridgeFactory::createBridge: sProtocol != urp ||"
126 : " aConnection == null"),
127 0 : static_cast< cppu::OWeakObject * >(this), -1);
128 : }
129 0 : b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
130 0 : if (sName.isEmpty()) {
131 : unnamed_.push_back(
132 0 : css::uno::Reference< css::bridge::XBridge >(b.get()));
133 : } else {
134 0 : named_[sName] = b.get();
135 0 : }
136 : }
137 0 : b->start();
138 0 : return css::uno::Reference< css::bridge::XBridge >(b.get());
139 : }
140 :
141 0 : css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
142 : OUString const & sName) throw (css::uno::RuntimeException, std::exception)
143 : {
144 0 : osl::MutexGuard g(m_aMutex);
145 0 : BridgeMap::iterator i(named_.find(sName));
146 0 : return i == named_.end()
147 0 : ? css::uno::Reference< css::bridge::XBridge >() : i->second;
148 : }
149 :
150 : css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
151 0 : BridgeFactory::getExistingBridges() throw (css::uno::RuntimeException, std::exception) {
152 0 : osl::MutexGuard g(m_aMutex);
153 0 : if (unnamed_.size() > SAL_MAX_INT32) {
154 : throw css::uno::RuntimeException(
155 : "BridgeFactory::getExistingBridges: too many",
156 0 : static_cast< cppu::OWeakObject * >(this));
157 : }
158 0 : sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
159 0 : if (named_.size() > static_cast< sal_uInt32 >(SAL_MAX_INT32 - n)) {
160 : throw css::uno::RuntimeException(
161 : "BridgeFactory::getExistingBridges: too many",
162 0 : static_cast< cppu::OWeakObject * >(this));
163 : }
164 0 : n = static_cast< sal_Int32 >(n + named_.size());
165 0 : css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
166 0 : sal_Int32 i = 0;
167 0 : for (BridgeList::iterator j(unnamed_.begin()); j != unnamed_.end(); ++j) {
168 0 : s[i++] = *j;
169 : }
170 0 : for (BridgeMap::iterator j(named_.begin()); j != named_.end(); ++j) {
171 0 : s[i++] = j->second;
172 : }
173 0 : return s;
174 : }
175 :
176 : }
177 :
178 : namespace {
179 :
180 : static cppu::ImplementationEntry const services[] = {
181 : { &binaryurp::BridgeFactory::static_create,
182 : &binaryurp::BridgeFactory::static_getImplementationName,
183 : &binaryurp::BridgeFactory::static_getSupportedServiceNames,
184 : &cppu::createOneInstanceComponentFactory, 0, 0 },
185 : { 0, 0, 0, 0, 0, 0 }
186 : };
187 :
188 : }
189 :
190 0 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL binaryurp_component_getFactory(
191 : char const * pImplName, void * pServiceManager, void * pRegistryKey)
192 : {
193 : return cppu::component_getFactoryHelper(
194 0 : pImplName, pServiceManager, pRegistryKey, services);
195 : }
196 :
197 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|