1 /*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2011 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27 
28 #include "sal/config.h"
29 
30 #include <algorithm>
31 
32 #include "com/sun/star/connection/XConnection.hpp"
33 #include "com/sun/star/uno/Exception.hpp"
34 #include "com/sun/star/uno/Reference.hxx"
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include "com/sun/star/uno/XComponentContext.hpp"
37 #include "com/sun/star/uno/XInterface.hpp"
38 #include "cppuhelper/factory.hxx"
39 #include "cppuhelper/implementationentry.hxx"
40 #include "osl/diagnose.h"
41 #include "rtl/ref.hxx"
42 #include "sal/types.h"
43 #include "uno/lbnames.h"
44 
45 #include "bridge.hxx"
46 #include "bridgefactory.hxx"
47 
48 namespace binaryurp {
49 
50 namespace {
51 
52 namespace css = com::sun::star;
53 
54 }
55 
56 css::uno::Reference< css::uno::XInterface > BridgeFactory::static_create(
57     css::uno::Reference< css::uno::XComponentContext > const & xContext)
58     SAL_THROW((css::uno::Exception))
59 {
60     return static_cast< cppu::OWeakObject * >(new BridgeFactory(xContext));
61 }
62 
63 rtl::OUString BridgeFactory::static_getImplementationName() {
64     return rtl::OUString(
65         RTL_CONSTASCII_USTRINGPARAM(
66             "com.sun.star.comp.bridge.BridgeFactory"));
67 }
68 
69 css::uno::Sequence< rtl::OUString >
70 BridgeFactory::static_getSupportedServiceNames() {
71     rtl::OUString name(
72         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory"));
73     return css::uno::Sequence< rtl::OUString >(&name, 1);
74 }
75 
76 void BridgeFactory::removeBridge(
77     css::uno::Reference< css::bridge::XBridge > const & bridge)
78 {
79     OSL_ASSERT(bridge.is());
80     rtl::OUString n(bridge->getName());
81     osl::MutexGuard g(*this);
82     if (n.getLength() == 0) {
83         BridgeList::iterator i(
84             std::find(unnamed_.begin(), unnamed_.end(), bridge));
85         if (i != unnamed_.end()) {
86             unnamed_.erase(i);
87         }
88     } else {
89         BridgeMap::iterator i(named_.find(n));
90         if (i != named_.end() && i->second == bridge) {
91             named_.erase(i);
92         }
93     }
94 }
95 
96 BridgeFactory::BridgeFactory(
97     css::uno::Reference< css::uno::XComponentContext > const & context):
98     BridgeFactoryBase(*static_cast< osl::Mutex * >(this)), context_(context)
99 {
100     OSL_ASSERT(context.is());
101 }
102 
103 BridgeFactory::~BridgeFactory() {}
104 
105 rtl::OUString BridgeFactory::getImplementationName()
106     throw (css::uno::RuntimeException)
107 {
108     return static_getImplementationName();
109 }
110 
111 sal_Bool BridgeFactory::supportsService(rtl::OUString const & ServiceName)
112     throw (css::uno::RuntimeException)
113 {
114     css::uno::Sequence< rtl::OUString > s(getSupportedServiceNames());
115     for (sal_Int32 i = 0; i != s.getLength(); ++i) {
116         if (ServiceName == s[i]) {
117             return true;
118         }
119     }
120     return false;
121 }
122 
123 css::uno::Sequence< rtl::OUString > BridgeFactory::getSupportedServiceNames()
124     throw (css::uno::RuntimeException)
125 {
126     return static_getSupportedServiceNames();
127 }
128 
129 css::uno::Reference< css::bridge::XBridge > BridgeFactory::createBridge(
130     rtl::OUString const & sName, rtl::OUString const & sProtocol,
131     css::uno::Reference< css::connection::XConnection > const & aConnection,
132     css::uno::Reference< css::bridge::XInstanceProvider > const &
133         anInstanceProvider)
134     throw (
135         css::bridge::BridgeExistsException, css::lang::IllegalArgumentException,
136         css::uno::RuntimeException)
137 {
138     rtl::Reference< Bridge > b;
139     {
140         osl::MutexGuard g(*this);
141         if (named_.find(sName) != named_.end()) {
142             throw css::bridge::BridgeExistsException(
143                 sName, static_cast< cppu::OWeakObject * >(this));
144         }
145         if (!(sProtocol.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("urp")) &&
146               aConnection.is()))
147         {
148             throw css::lang::IllegalArgumentException(
149                 rtl::OUString(
150                     RTL_CONSTASCII_USTRINGPARAM(
151                         "BridgeFactory::createBridge: sProtocol != urp ||"
152                         " aConnection == null")),
153                 static_cast< cppu::OWeakObject * >(this), -1);
154         }
155         b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
156         if (sName.getLength() == 0) {
157             unnamed_.push_back(
158                 css::uno::Reference< css::bridge::XBridge >(b.get()));
159         } else {
160             named_[sName] = b.get();
161         }
162     }
163     b->start();
164     return css::uno::Reference< css::bridge::XBridge >(b.get());
165 }
166 
167 css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
168     rtl::OUString const & sName) throw (css::uno::RuntimeException)
169 {
170     osl::MutexGuard g(*this);
171     BridgeMap::iterator i(named_.find(sName));
172     return i == named_.end()
173         ? css::uno::Reference< css::bridge::XBridge >() : i->second;
174 }
175 
176 css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
177 BridgeFactory::getExistingBridges() throw (css::uno::RuntimeException) {
178     osl::MutexGuard g(*this);
179     if (unnamed_.size() > SAL_MAX_INT32) {
180         throw css::uno::RuntimeException(
181             rtl::OUString(
182                 RTL_CONSTASCII_USTRINGPARAM(
183                     "BridgeFactory::getExistingBridges: too many")),
184             static_cast< cppu::OWeakObject * >(this));
185     }
186     sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
187     if (named_.size() > static_cast< sal_uInt32 >(SAL_MAX_INT32 - n)) {
188         throw css::uno::RuntimeException(
189             rtl::OUString(
190                 RTL_CONSTASCII_USTRINGPARAM(
191                     "BridgeFactory::getExistingBridges: too many")),
192             static_cast< cppu::OWeakObject * >(this));
193     }
194     n = static_cast< sal_Int32 >(n + named_.size());
195     css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
196     sal_Int32 i = 0;
197     for (BridgeList::iterator j(unnamed_.begin()); j != unnamed_.end(); ++j) {
198         s[i++] = *j;
199     }
200     for (BridgeMap::iterator j(named_.begin()); j != named_.end(); ++j) {
201         s[i++] = j->second;
202     }
203     return s;
204 }
205 
206 }
207 
208 namespace {
209 
210 static cppu::ImplementationEntry const services[] = {
211     { &binaryurp::BridgeFactory::static_create,
212       &binaryurp::BridgeFactory::static_getImplementationName,
213       &binaryurp::BridgeFactory::static_getSupportedServiceNames,
214       &cppu::createSingleComponentFactory, 0, 0 },
215     { 0, 0, 0, 0, 0, 0 }
216 };
217 
218 }
219 
220 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
221     char const * pImplName, void * pServiceManager, void * pRegistryKey)
222 {
223     return cppu::component_getFactoryHelper(
224         pImplName, pServiceManager, pRegistryKey, services);
225 }
226 
227 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL
228 component_getImplementationEnvironment(
229     char const ** ppEnvTypeName, uno_Environment **)
230 {
231     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
232 }
233