LibreOffice Module connectivity (master) 1
ZPoolCollection.cxx
Go to the documentation of this file.
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 "ZPoolCollection.hxx"
21#include "ZDriverWrapper.hxx"
22#include "ZConnectionPool.hxx"
23#include <com/sun/star/configuration/theDefaultProvider.hpp>
24#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
25#include <com/sun/star/beans/NamedValue.hpp>
26#include <com/sun/star/beans/PropertyValue.hpp>
27#include <com/sun/star/frame/Desktop.hpp>
28#include <com/sun/star/reflection/ProxyFactory.hpp>
29#include <com/sun/star/sdbc/DriverManager.hpp>
31#include <com/sun/star/beans/XPropertySet.hpp>
32#include <osl/diagnose.h>
33#include <sal/log.hxx>
35
36using namespace ::com::sun::star::uno;
37using namespace ::com::sun::star::lang;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::beans;
40using namespace ::com::sun::star::container;
41using namespace ::com::sun::star::reflection;
42using namespace ::osl;
43using namespace connectivity;
44
45
47{
48 return "org.openoffice.Office.DataAccess/ConnectionPool";
49}
50
51static OUString getEnablePoolingNodeName()
52{
53 return "EnablePooling";
54}
55
56static OUString getDriverNameNodeName()
57{
58 return "DriverName";
59}
60
62{
63 return "DriverSettings";
64}
65
66static OUString getEnableNodeName()
67{
68 return "Enable";
69}
70
71
72OPoolCollection::OPoolCollection(const Reference< XComponentContext >& _rxContext)
73 :m_xContext(_rxContext)
74{
75 // bootstrap all objects supporting the .sdb.Driver service
76 m_xManager = DriverManager::create( m_xContext );
77
78 m_xProxyFactory = ProxyFactory::create( m_xContext );
79
80 Reference<XPropertySet> xProp(getConfigPoolRoot(),UNO_QUERY);
81 if ( xProp.is() )
82 xProp->addPropertyChangeListener(getEnablePoolingNodeName(),this);
83 // attach as desktop listener to know when we have to release our pools
84 osl_atomic_increment( &m_refCount );
85 {
86
87 m_xDesktop = css::frame::Desktop::create( m_xContext );
88 m_xDesktop->addTerminateListener(this);
89
90 }
91 osl_atomic_decrement( &m_refCount );
92}
93
94OPoolCollection::~OPoolCollection()
95{
97}
98
99Reference< XConnection > SAL_CALL OPoolCollection::getConnection( const OUString& _rURL )
100{
101 return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
102}
103
104Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo )
105{
106 MutexGuard aGuard(m_aMutex);
107 Reference< XConnection > xConnection;
108 Reference< XDriver > xDriver;
109 Reference< XInterface > xDriverNode;
110 OUString sImplName;
111 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode) && xDriver.is())
112 {
113 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
114
115 if(pConnectionPool)
116 xConnection = pConnectionPool->getConnectionWithInfo(_rURL,_rInfo);
117 }
118 else if(xDriver.is())
119 xConnection = xDriver->connect(_rURL,_rInfo);
120
121 return xConnection;
122}
123
124void SAL_CALL OPoolCollection::setLoginTimeout( sal_Int32 seconds )
125{
126 MutexGuard aGuard(m_aMutex);
127 m_xManager->setLoginTimeout(seconds);
128}
129
131{
132 MutexGuard aGuard(m_aMutex);
133 return m_xManager->getLoginTimeout();
134}
135
137{
138 return "com.sun.star.sdbc.OConnectionPool";
139}
140
141sal_Bool SAL_CALL OPoolCollection::supportsService( const OUString& _rServiceName )
142{
143 return cppu::supportsService(this, _rServiceName);
144}
145
146
147Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames( )
148{
149 return { "com.sun.star.sdbc.ConnectionPool" };
150}
151
152Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const OUString& _rURL )
153{
154 // returns the original driver when no connection pooling is enabled else it returns the proxy
155 MutexGuard aGuard(m_aMutex);
156
157 Reference< XDriver > xDriver;
158 Reference< XInterface > xDriverNode;
159 OUString sImplName;
160 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
161 {
162 Reference< XDriver > xExistentProxy;
163 // look if we already have a proxy for this driver
164 for (const auto& [rxDriver, rxDriverRef] : m_aDriverProxies)
165 {
166 // hold the proxy alive as long as we're in this loop round
167 xExistentProxy = rxDriverRef;
168
169 if (xExistentProxy.is() && (rxDriver.get() == xDriver.get()))
170 // already created a proxy for this
171 break;
172 }
173 if (xExistentProxy.is())
174 {
175 xDriver = xExistentProxy;
176 }
177 else
178 { // create a new proxy for the driver
179 // this allows us to control the connections created by it
180 Reference< XAggregation > xDriverProxy = m_xProxyFactory->createProxy(xDriver);
181 OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
182
183 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
184 xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
185 }
186 }
187
188 return xDriver;
189}
190
191bool OPoolCollection::isDriverPoolingEnabled(std::u16string_view _sDriverImplName,
192 Reference< XInterface >& _rxDriverNode)
193{
194 bool bEnabled = false;
195 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
196 // then look for which of them settings are stored in the configuration
197 Reference< XNameAccess > xDirectAccess(openNode(getDriverSettingsNodeName(),xConnectionPoolRoot),UNO_QUERY);
198
199 if(xDirectAccess.is())
200 {
201 Sequence< OUString > aDriverKeys = xDirectAccess->getElementNames();
202 const OUString* pDriverKeys = aDriverKeys.getConstArray();
203 const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
204 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
205 {
206 // the name of the driver in this round
207 if(_sDriverImplName == *pDriverKeys)
208 {
209 _rxDriverNode = openNode(*pDriverKeys,xDirectAccess);
210 if(_rxDriverNode.is())
211 getNodeValue(getEnableNodeName(),_rxDriverNode) >>= bEnabled;
212 break;
213 }
214 }
215 }
216 return bEnabled;
217}
218
220{
221 // the config node where all pooling relevant info are stored under
222 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
223
224 // the global "enabled" flag
225 bool bEnabled = false;
226 if(xConnectionPoolRoot.is())
227 getNodeValue(getEnablePoolingNodeName(),xConnectionPoolRoot) >>= bEnabled;
228 return bEnabled;
229}
230
231Reference<XInterface> const & OPoolCollection::getConfigPoolRoot()
232{
233 if(!m_xConfigNode.is())
235 css::configuration::theDefaultProvider::get(m_xContext),
237 return m_xConfigNode;
238}
239
240bool OPoolCollection::isPoolingEnabledByUrl(const OUString& _sUrl,
241 Reference< XDriver >& _rxDriver,
242 OUString& _rsImplName,
243 Reference< XInterface >& _rxDriverNode)
244{
245 bool bEnabled = false;
246 _rxDriver = m_xManager->getDriverByURL(_sUrl);
247 if (_rxDriver.is() && isPoolingEnabled())
248 {
249 Reference< XServiceInfo > xServiceInfo(_rxDriver,UNO_QUERY);
250 OSL_ENSURE(xServiceInfo.is(),"Each driver should have a XServiceInfo interface!");
251
252 if(xServiceInfo.is())
253 {
254 // look for the implementation name of the driver
255 _rsImplName = xServiceInfo->getImplementationName();
256 bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
257 }
258 }
259 return bEnabled;
260}
261
263{
264 for(auto& rEntry : m_aPools)
265 {
266 rEntry.second->clear(_bDispose);
267 }
268 m_aPools.clear();
269}
270
272 const Reference< XDriver >& _xDriver,
273 const Reference< XInterface >& _xDriverNode)
274{
275 OConnectionPool *pRet = nullptr;
276 OConnectionPools::const_iterator aFind = m_aPools.find(_sImplName);
277 if (aFind != m_aPools.end())
278 pRet = aFind->second.get();
279 else if (_xDriver.is() && _xDriverNode.is())
280 {
281 Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
282 if(xProp.is())
283 xProp->addPropertyChangeListener(getEnableNodeName(),this);
284 rtl::Reference<OConnectionPool> pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
285 m_aPools.emplace(_sImplName,pConnectionPool);
286 pRet = pConnectionPool.get();
287 }
288
289 OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
290
291 return pRet;
292}
293
294Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
295 const OUString& _rPath)
296{
297 OSL_ASSERT(_rxConfProvider.is());
298 Sequence< Any > args{ Any(NamedValue( "nodepath", Any(_rPath))) };
299 Reference< XInterface > xInterface(
300 _rxConfProvider->createInstanceWithArguments(
301 "com.sun.star.configuration.ConfigurationAccess",
302 args));
303 OSL_ENSURE(
304 xInterface.is(),
305 "::createWithProvider: could not create the node access!");
306 return xInterface;
307}
308
309Reference<XInterface> OPoolCollection::openNode(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) noexcept
310{
311 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
312 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
313 Reference< XInterface > xNode;
314
315 try
316 {
317 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
318 {
319 xNode.set(xDirectAccess->getByName(_rPath), css::uno::UNO_QUERY);
321 !xNode.is(), "connectivity.cpool",
322 "OConfigurationNode::openNode: could not open the node!");
323 }
324 else if (xHierarchyAccess.is())
325 {
326 xNode.set(
327 xHierarchyAccess->getByHierarchicalName(_rPath),
328 css::uno::UNO_QUERY);
330 !xNode.is(), "connectivity.cpool",
331 "OConfigurationNode::openNode: could not open the node!");
332 }
333
334 }
335 catch(const NoSuchElementException&)
336 {
337 SAL_WARN("connectivity.cpool", "::openNode: there is no element named " <<
338 _rPath << "!");
339 }
340 catch(const Exception&)
341 {
342 TOOLS_WARN_EXCEPTION("connectivity.cpool", "OConfigurationNode::openNode: caught an exception while retrieving the node");
343 }
344 return xNode;
345}
346
347Any OPoolCollection::getNodeValue(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) noexcept
348{
349 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
350 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
351 Any aReturn;
352 try
353 {
354 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
355 {
356 aReturn = xDirectAccess->getByName(_rPath);
357 }
358 else if (xHierarchyAccess.is())
359 {
360 aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
361 }
362 }
363 catch(const NoSuchElementException&)
364 {
365 TOOLS_WARN_EXCEPTION("connectivity.cpool", "" );
366 }
367 return aReturn;
368}
369
370void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ )
371{
372}
373
374void SAL_CALL OPoolCollection::notifyTermination( const EventObject& /*Event*/ )
375{
376 clearDesktop();
377}
378
379void SAL_CALL OPoolCollection::disposing( const EventObject& Source )
380{
381 MutexGuard aGuard(m_aMutex);
382 if ( m_xDesktop == Source.Source )
383 {
384 clearDesktop();
385 }
386 else
387 {
388 try
389 {
390 Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
391 if(Source.Source == m_xConfigNode)
392 {
393 if ( xProp.is() )
394 xProp->removePropertyChangeListener(getEnablePoolingNodeName(),this);
395 m_xConfigNode.clear();
396 }
397 else if ( xProp.is() )
398 xProp->removePropertyChangeListener(getEnableNodeName(),this);
399 }
400 catch(const Exception&)
401 {
402 TOOLS_WARN_EXCEPTION("connectivity.cpool", "");
403 }
404 }
405}
406
407void SAL_CALL OPoolCollection::propertyChange( const css::beans::PropertyChangeEvent& evt )
408{
409 MutexGuard aGuard(m_aMutex);
410 if(evt.Source == m_xConfigNode)
411 {
412 bool bEnabled = true;
413 evt.NewValue >>= bEnabled;
414 if(!bEnabled )
415 {
416 m_aDriverProxies.clear();
419 }
420 }
421 else if(evt.Source.is())
422 {
423 bool bEnabled = true;
424 evt.NewValue >>= bEnabled;
425 if(!bEnabled)
426 {
427 OUString sThisDriverName;
428 getNodeValue(getDriverNameNodeName(),evt.Source) >>= sThisDriverName;
429 // 1st release the driver
430 // look if we already have a proxy for this driver
431 MapDriver2DriverRef::iterator aLookup = m_aDriverProxies.begin();
432 while( aLookup != m_aDriverProxies.end())
433 {
434 MapDriver2DriverRef::iterator aFind = aLookup;
435 Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
436 ++aLookup;
437 if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
438 m_aDriverProxies.erase(aFind);
439 }
440
441 // 2nd clear the connectionpool
442 OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
443 if(aFind != m_aPools.end())
444 {
445 aFind->second->clear(false);
446 m_aPools.erase(aFind);
447 }
448 }
449 }
450}
451
453{
455 if ( m_xDesktop.is() )
456 m_xDesktop->removeTerminateListener(this);
457 m_xDesktop.clear();
458}
459
460extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
462 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
463{
464 return cppu::acquire(new OPoolCollection(context));
465}
466
467
468/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
static OUString getEnableNodeName()
static OUString getDriverSettingsNodeName()
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * connectivity_OPoolCollection_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static OUString getEnablePoolingNodeName()
static OUString getConnectionPoolNodeName()
static OUString getDriverNameNodeName()
css::uno::Reference< css::sdbc::XConnection > getConnectionWithInfo(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info)
OPoolCollection: control the whole connection pooling for oo.
css::uno::Reference< css::uno::XInterface > m_xConfigNode
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &evt) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static css::uno::Any getNodeValue(const OUString &_rPath, const css::uno::Reference< css::uno::XInterface > &_xTreeNode) noexcept
virtual void SAL_CALL notifyTermination(const css::lang::EventObject &Event) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void SAL_CALL setLoginTimeout(sal_Int32 seconds) override
virtual sal_Int32 SAL_CALL getLoginTimeout() override
MapDriver2DriverRef m_aDriverProxies
OConnectionPool * getConnectionPool(const OUString &_sImplName, const css::uno::Reference< css::sdbc::XDriver > &_xDriver, const css::uno::Reference< css::uno::XInterface > &_rxDriverNode)
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnection(const OUString &url) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
std::map< css::uno::Reference< css::sdbc::XDriver >, css::uno::WeakReference< css::sdbc::XDriver > > MapDriver2DriverRef
bool isPoolingEnabledByUrl(const OUString &_sUrl, css::uno::Reference< css::sdbc::XDriver > &_rxDriver, OUString &_rsImplName, css::uno::Reference< css::uno::XInterface > &_rxDriverNode)
bool isDriverPoolingEnabled(std::u16string_view _sDriverImplName, css::uno::Reference< css::uno::XInterface > &_rxDriverNode)
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL queryTermination(const css::lang::EventObject &Event) override
static css::uno::Reference< css::uno::XInterface > openNode(const OUString &_rPath, const css::uno::Reference< css::uno::XInterface > &_xTreeNode) noexcept
virtual css::uno::Reference< css::sdbc::XDriver > SAL_CALL getDriverByURL(const OUString &url) override
css::uno::Reference< css::sdbc::XDriverManager2 > m_xManager
void clearConnectionPools(bool _bDispose)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
static css::uno::Reference< css::uno::XInterface > createWithProvider(const css::uno::Reference< css::lang::XMultiServiceFactory > &_rxConfProvider, const OUString &_rPath)
css::uno::Reference< css::uno::XInterface > const & getConfigPoolRoot()
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnectionWithInfo(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
css::uno::Reference< css::reflection::XProxyFactory > m_xProxyFactory
css::uno::Reference< css::frame::XDesktop2 > m_xDesktop
Reference< XDesktop2 > m_xDesktop
#define TOOLS_WARN_EXCEPTION(area, stream)
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
@ Exception
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
args
unsigned char sal_Bool