Update git submodules
[LibreOffice.git] / connectivity / source / cpool / ZPoolCollection.cxx
blobe6ef3182520d2ad3aaae286de1a3b1c924a8408f
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 "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>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <osl/diagnose.h>
33 #include <sal/log.hxx>
34 #include <comphelper/diagnose_ex.hxx>
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::reflection;
42 using namespace ::osl;
43 using namespace connectivity;
46 constexpr OUString CONNECTIONPOOL_NODENAME = u"org.openoffice.Office.DataAccess/ConnectionPool"_ustr;
47 constexpr OUString ENABLE_POOLING = u"EnablePooling"_ustr;
48 constexpr OUString DRIVER_NAME = u"DriverName"_ustr;
49 constexpr OUString DRIVER_SETTINGS = u"DriverSettings"_ustr;
50 constexpr OUString ENABLE = u"Enable"_ustr;
53 OPoolCollection::OPoolCollection(const Reference< XComponentContext >& _rxContext)
54 :m_xContext(_rxContext)
56 // bootstrap all objects supporting the .sdb.Driver service
57 m_xManager = DriverManager::create( m_xContext );
59 m_xProxyFactory = ProxyFactory::create( m_xContext );
60 Reference<XPropertySet> xProp(getConfigPoolRoot(),UNO_QUERY);
61 if ( xProp.is() )
62 xProp->addPropertyChangeListener(ENABLE_POOLING,this);
63 // attach as desktop listener to know when we have to release our pools
64 osl_atomic_increment( &m_refCount );
67 m_xDesktop = css::frame::Desktop::create( m_xContext );
68 m_xDesktop->addTerminateListener(this);
71 osl_atomic_decrement( &m_refCount );
74 OPoolCollection::~OPoolCollection()
76 clearConnectionPools(false);
79 Reference< XConnection > SAL_CALL OPoolCollection::getConnection( const OUString& _rURL )
81 return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
84 Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo )
86 MutexGuard aGuard(m_aMutex);
87 Reference< XConnection > xConnection;
88 Reference< XDriver > xDriver;
89 Reference< XInterface > xDriverNode;
90 OUString sImplName;
91 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode) && xDriver.is())
93 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
95 if(pConnectionPool)
96 xConnection = pConnectionPool->getConnectionWithInfo(_rURL,_rInfo);
98 else if(xDriver.is())
99 xConnection = xDriver->connect(_rURL,_rInfo);
101 return xConnection;
104 void SAL_CALL OPoolCollection::setLoginTimeout( sal_Int32 seconds )
106 MutexGuard aGuard(m_aMutex);
107 m_xManager->setLoginTimeout(seconds);
110 sal_Int32 SAL_CALL OPoolCollection::getLoginTimeout( )
112 MutexGuard aGuard(m_aMutex);
113 return m_xManager->getLoginTimeout();
116 OUString SAL_CALL OPoolCollection::getImplementationName( )
118 return u"com.sun.star.sdbc.OConnectionPool"_ustr;
121 sal_Bool SAL_CALL OPoolCollection::supportsService( const OUString& _rServiceName )
123 return cppu::supportsService(this, _rServiceName);
127 Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames( )
129 return { u"com.sun.star.sdbc.ConnectionPool"_ustr };
132 Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const OUString& _rURL )
134 // returns the original driver when no connection pooling is enabled else it returns the proxy
135 MutexGuard aGuard(m_aMutex);
137 Reference< XDriver > xDriver;
138 Reference< XInterface > xDriverNode;
139 OUString sImplName;
140 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
142 Reference< XDriver > xExistentProxy;
143 // look if we already have a proxy for this driver
144 for (const auto& [rxDriver, rxDriverRef] : m_aDriverProxies)
146 // hold the proxy alive as long as we're in this loop round
147 xExistentProxy = rxDriverRef;
149 if (xExistentProxy.is() && (rxDriver.get() == xDriver.get()))
150 // already created a proxy for this
151 break;
153 if (xExistentProxy.is())
155 xDriver = std::move(xExistentProxy);
157 else
158 { // create a new proxy for the driver
159 // this allows us to control the connections created by it
160 Reference< XAggregation > xDriverProxy = m_xProxyFactory->createProxy(xDriver);
161 OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
163 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
164 xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
168 return xDriver;
171 bool OPoolCollection::isDriverPoolingEnabled(std::u16string_view _sDriverImplName,
172 Reference< XInterface >& _rxDriverNode)
174 bool bEnabled = false;
175 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
176 // then look for which of them settings are stored in the configuration
177 Reference< XNameAccess > xDirectAccess(openNode(DRIVER_SETTINGS,xConnectionPoolRoot),UNO_QUERY);
179 if(xDirectAccess.is())
181 for (auto& driverKey : xDirectAccess->getElementNames())
183 // the name of the driver in this round
184 if (_sDriverImplName == driverKey)
186 _rxDriverNode = openNode(driverKey, xDirectAccess);
187 if(_rxDriverNode.is())
188 getNodeValue(ENABLE,_rxDriverNode) >>= bEnabled;
189 break;
193 return bEnabled;
196 bool OPoolCollection::isPoolingEnabled()
198 // the config node where all pooling relevant info are stored under
199 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
201 // the global "enabled" flag
202 bool bEnabled = false;
203 if(xConnectionPoolRoot.is())
204 getNodeValue(ENABLE_POOLING,xConnectionPoolRoot) >>= bEnabled;
205 return bEnabled;
208 Reference<XInterface> const & OPoolCollection::getConfigPoolRoot()
210 if(!m_xConfigNode.is())
211 m_xConfigNode = createWithProvider(
212 css::configuration::theDefaultProvider::get(m_xContext),
213 CONNECTIONPOOL_NODENAME);
214 return m_xConfigNode;
217 bool OPoolCollection::isPoolingEnabledByUrl(const OUString& _sUrl,
218 Reference< XDriver >& _rxDriver,
219 OUString& _rsImplName,
220 Reference< XInterface >& _rxDriverNode)
222 bool bEnabled = false;
223 _rxDriver = m_xManager->getDriverByURL(_sUrl);
224 if (_rxDriver.is() && isPoolingEnabled())
226 Reference< XServiceInfo > xServiceInfo(_rxDriver,UNO_QUERY);
227 OSL_ENSURE(xServiceInfo.is(),"Each driver should have a XServiceInfo interface!");
229 if(xServiceInfo.is())
231 // look for the implementation name of the driver
232 _rsImplName = xServiceInfo->getImplementationName();
233 bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
236 return bEnabled;
239 void OPoolCollection::clearConnectionPools(bool _bDispose)
241 for(auto& rEntry : m_aPools)
243 rEntry.second->clear(_bDispose);
245 m_aPools.clear();
248 OConnectionPool* OPoolCollection::getConnectionPool(const OUString& _sImplName,
249 const Reference< XDriver >& _xDriver,
250 const Reference< XInterface >& _xDriverNode)
252 OConnectionPool *pRet = nullptr;
253 OConnectionPools::const_iterator aFind = m_aPools.find(_sImplName);
254 if (aFind != m_aPools.end())
255 pRet = aFind->second.get();
256 else if (_xDriver.is() && _xDriverNode.is())
258 Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
259 if(xProp.is())
260 xProp->addPropertyChangeListener(ENABLE,this);
261 rtl::Reference<OConnectionPool> pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
262 m_aPools.emplace(_sImplName,pConnectionPool);
263 pRet = pConnectionPool.get();
266 OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
268 return pRet;
271 Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
272 const OUString& _rPath)
274 OSL_ASSERT(_rxConfProvider.is());
275 Sequence< Any > args{ Any(NamedValue( u"nodepath"_ustr, Any(_rPath))) };
276 Reference< XInterface > xInterface(
277 _rxConfProvider->createInstanceWithArguments(
278 u"com.sun.star.configuration.ConfigurationAccess"_ustr,
279 args));
280 OSL_ENSURE(
281 xInterface.is(),
282 "::createWithProvider: could not create the node access!");
283 return xInterface;
286 Reference<XInterface> OPoolCollection::openNode(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) noexcept
288 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
289 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
290 Reference< XInterface > xNode;
294 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
296 xNode.set(xDirectAccess->getByName(_rPath), css::uno::UNO_QUERY);
297 SAL_WARN_IF(
298 !xNode.is(), "connectivity.cpool",
299 "OConfigurationNode::openNode: could not open the node!");
301 else if (xHierarchyAccess.is())
303 xNode.set(
304 xHierarchyAccess->getByHierarchicalName(_rPath),
305 css::uno::UNO_QUERY);
306 SAL_WARN_IF(
307 !xNode.is(), "connectivity.cpool",
308 "OConfigurationNode::openNode: could not open the node!");
312 catch(const NoSuchElementException&)
314 SAL_WARN("connectivity.cpool", "::openNode: there is no element named " <<
315 _rPath << "!");
317 catch(const Exception&)
319 TOOLS_WARN_EXCEPTION("connectivity.cpool", "OConfigurationNode::openNode: caught an exception while retrieving the node");
321 return xNode;
324 Any OPoolCollection::getNodeValue(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) noexcept
326 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
327 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
328 Any aReturn;
331 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
333 aReturn = xDirectAccess->getByName(_rPath);
335 else if (xHierarchyAccess.is())
337 aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
340 catch(const NoSuchElementException&)
342 TOOLS_WARN_EXCEPTION("connectivity.cpool", "" );
344 return aReturn;
347 void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ )
351 void SAL_CALL OPoolCollection::notifyTermination( const EventObject& Event )
353 disposing(Event);
356 void SAL_CALL OPoolCollection::disposing( const EventObject& Source )
358 MutexGuard aGuard(m_aMutex);
359 if ( m_xDesktop == Source.Source )
361 clearDesktop();
363 else
367 Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
368 if(Source.Source == m_xConfigNode)
370 if ( xProp.is() )
371 xProp->removePropertyChangeListener(ENABLE_POOLING,this);
372 m_xConfigNode.clear();
374 else if ( xProp.is() )
375 xProp->removePropertyChangeListener(ENABLE,this);
377 catch(const Exception&)
379 TOOLS_WARN_EXCEPTION("connectivity.cpool", "");
382 m_xConfigNode.clear();
383 m_xProxyFactory.clear();
384 m_xManager.clear();
385 m_xContext.clear();
388 void SAL_CALL OPoolCollection::propertyChange( const css::beans::PropertyChangeEvent& evt )
390 MutexGuard aGuard(m_aMutex);
391 if(evt.Source == m_xConfigNode)
393 bool bEnabled = true;
394 evt.NewValue >>= bEnabled;
395 if(!bEnabled )
397 m_aDriverProxies.clear();
398 m_aDriverProxies = MapDriver2DriverRef();
399 clearConnectionPools(false);
402 else if(evt.Source.is())
404 bool bEnabled = true;
405 evt.NewValue >>= bEnabled;
406 if(!bEnabled)
408 OUString sThisDriverName;
409 getNodeValue(DRIVER_NAME,evt.Source) >>= sThisDriverName;
410 // 1st release the driver
411 // look if we already have a proxy for this driver
412 MapDriver2DriverRef::iterator aLookup = m_aDriverProxies.begin();
413 while( aLookup != m_aDriverProxies.end())
415 MapDriver2DriverRef::iterator aFind = aLookup;
416 Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
417 ++aLookup;
418 if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
419 m_aDriverProxies.erase(aFind);
422 // 2nd clear the connectionpool
423 OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
424 if(aFind != m_aPools.end())
426 aFind->second->clear(false);
427 m_aPools.erase(aFind);
433 void OPoolCollection::clearDesktop()
435 clearConnectionPools(true);
436 if ( m_xDesktop.is() )
437 m_xDesktop->removeTerminateListener(this);
438 m_xDesktop.clear();
441 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
442 connectivity_OPoolCollection_get_implementation(
443 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
445 return cppu::acquire(new OPoolCollection(context));
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */