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