bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / cpool / ZPoolCollection.cxx
blobae32b27c38e2492d93ac71e77d4ad949aac0ea89
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 <comphelper/processfactory.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <osl/diagnose.h>
34 #include <sal/log.hxx>
35 #include <tools/diagnose_ex.h>
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::sdbc;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::reflection;
43 using namespace ::osl;
44 using namespace connectivity;
47 static OUString getConnectionPoolNodeName()
49 return OUString( "org.openoffice.Office.DataAccess/ConnectionPool" );
52 static OUString getEnablePoolingNodeName()
54 return OUString( "EnablePooling" );
57 static OUString getDriverNameNodeName()
59 return OUString( "DriverName" );
62 static OUString getDriverSettingsNodeName()
64 return OUString( "DriverSettings" );
67 static OUString getEnableNodeName()
69 return OUString( "Enable" );
73 OPoolCollection::OPoolCollection(const Reference< XComponentContext >& _rxContext)
74 :m_xContext(_rxContext)
76 // bootstrap all objects supporting the .sdb.Driver service
77 m_xManager = DriverManager::create( m_xContext );
79 m_xProxyFactory = ProxyFactory::create( m_xContext );
81 Reference<XPropertySet> xProp(getConfigPoolRoot(),UNO_QUERY);
82 if ( xProp.is() )
83 xProp->addPropertyChangeListener(getEnablePoolingNodeName(),this);
84 // attach as desktop listener to know when we have to release our pools
85 osl_atomic_increment( &m_refCount );
88 m_xDesktop = css::frame::Desktop::create( m_xContext );
89 m_xDesktop->addTerminateListener(this);
92 osl_atomic_decrement( &m_refCount );
95 OPoolCollection::~OPoolCollection()
97 clearConnectionPools(false);
100 Reference< XConnection > SAL_CALL OPoolCollection::getConnection( const OUString& _rURL )
102 return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
105 Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo )
107 MutexGuard aGuard(m_aMutex);
108 Reference< XConnection > xConnection;
109 Reference< XDriver > xDriver;
110 Reference< XInterface > xDriverNode;
111 OUString sImplName;
112 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode) && xDriver.is())
114 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
116 if(pConnectionPool)
117 xConnection = pConnectionPool->getConnectionWithInfo(_rURL,_rInfo);
119 else if(xDriver.is())
120 xConnection = xDriver->connect(_rURL,_rInfo);
122 return xConnection;
125 void SAL_CALL OPoolCollection::setLoginTimeout( sal_Int32 seconds )
127 MutexGuard aGuard(m_aMutex);
128 m_xManager->setLoginTimeout(seconds);
131 sal_Int32 SAL_CALL OPoolCollection::getLoginTimeout( )
133 MutexGuard aGuard(m_aMutex);
134 return m_xManager->getLoginTimeout();
137 OUString SAL_CALL OPoolCollection::getImplementationName( )
139 return getImplementationName_Static();
142 sal_Bool SAL_CALL OPoolCollection::supportsService( const OUString& _rServiceName )
144 return cppu::supportsService(this, _rServiceName);
148 Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames( )
150 return getSupportedServiceNames_Static();
153 //---------------------------------------OPoolCollection----------------------------------
154 Reference< XInterface > OPoolCollection::CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
156 return static_cast<XDriverManager*>(new OPoolCollection(comphelper::getComponentContext(_rxFactory)));
160 OUString OPoolCollection::getImplementationName_Static( )
162 return OUString("com.sun.star.sdbc.OConnectionPool");
166 Sequence< OUString > OPoolCollection::getSupportedServiceNames_Static( )
168 Sequence< OUString > aSupported { "com.sun.star.sdbc.ConnectionPool" };
169 return aSupported;
172 Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const OUString& _rURL )
174 // returns the original driver when no connection pooling is enabled else it returns the proxy
175 MutexGuard aGuard(m_aMutex);
177 Reference< XDriver > xDriver;
178 Reference< XInterface > xDriverNode;
179 OUString sImplName;
180 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
182 Reference< XDriver > xExistentProxy;
183 // look if we already have a proxy for this driver
184 for (const auto& [rxDriver, rxDriverRef] : m_aDriverProxies)
186 // hold the proxy alive as long as we're in this loop round
187 xExistentProxy = rxDriverRef;
189 if (xExistentProxy.is() && (rxDriver.get() == xDriver.get()))
190 // already created a proxy for this
191 break;
193 if (xExistentProxy.is())
195 xDriver = xExistentProxy;
197 else
198 { // create a new proxy for the driver
199 // this allows us to control the connections created by it
200 Reference< XAggregation > xDriverProxy = m_xProxyFactory->createProxy(xDriver.get());
201 OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
203 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
204 xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
208 return xDriver;
211 bool OPoolCollection::isDriverPoolingEnabled(const OUString& _sDriverImplName,
212 Reference< XInterface >& _rxDriverNode)
214 bool bEnabled = false;
215 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
216 // then look for which of them settings are stored in the configuration
217 Reference< XNameAccess > xDirectAccess(openNode(getDriverSettingsNodeName(),xConnectionPoolRoot),UNO_QUERY);
219 if(xDirectAccess.is())
221 Sequence< OUString > aDriverKeys = xDirectAccess->getElementNames();
222 const OUString* pDriverKeys = aDriverKeys.getConstArray();
223 const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
224 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
226 // the name of the driver in this round
227 if(_sDriverImplName == *pDriverKeys)
229 _rxDriverNode = openNode(*pDriverKeys,xDirectAccess);
230 if(_rxDriverNode.is())
231 getNodeValue(getEnableNodeName(),_rxDriverNode) >>= bEnabled;
232 break;
236 return bEnabled;
239 bool OPoolCollection::isPoolingEnabled()
241 // the config node where all pooling relevant info are stored under
242 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
244 // the global "enabled" flag
245 bool bEnabled = false;
246 if(xConnectionPoolRoot.is())
247 getNodeValue(getEnablePoolingNodeName(),xConnectionPoolRoot) >>= bEnabled;
248 return bEnabled;
251 Reference<XInterface> const & OPoolCollection::getConfigPoolRoot()
253 if(!m_xConfigNode.is())
254 m_xConfigNode = createWithProvider(
255 css::configuration::theDefaultProvider::get(m_xContext),
256 getConnectionPoolNodeName());
257 return m_xConfigNode;
260 bool OPoolCollection::isPoolingEnabledByUrl(const OUString& _sUrl,
261 Reference< XDriver >& _rxDriver,
262 OUString& _rsImplName,
263 Reference< XInterface >& _rxDriverNode)
265 bool bEnabled = false;
266 _rxDriver = m_xManager->getDriverByURL(_sUrl);
267 if (_rxDriver.is() && isPoolingEnabled())
269 Reference< XServiceInfo > xSerivceInfo(_rxDriver,UNO_QUERY);
270 OSL_ENSURE(xSerivceInfo.is(),"Each driver should have a XServiceInfo interface!");
272 if(xSerivceInfo.is())
274 // look for the implementation name of the driver
275 _rsImplName = xSerivceInfo->getImplementationName();
276 bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
279 return bEnabled;
282 void OPoolCollection::clearConnectionPools(bool _bDispose)
284 for(auto& rEntry : m_aPools)
286 rEntry.second->clear(_bDispose);
288 m_aPools.clear();
291 OConnectionPool* OPoolCollection::getConnectionPool(const OUString& _sImplName,
292 const Reference< XDriver >& _xDriver,
293 const Reference< XInterface >& _xDriverNode)
295 OConnectionPool *pRet = nullptr;
296 OConnectionPools::const_iterator aFind = m_aPools.find(_sImplName);
297 if (aFind != m_aPools.end())
298 pRet = aFind->second.get();
299 else if (_xDriver.is() && _xDriverNode.is())
301 Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
302 if(xProp.is())
303 xProp->addPropertyChangeListener(getEnableNodeName(),this);
304 OConnectionPool* pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
305 aFind = m_aPools.emplace(_sImplName,pConnectionPool).first;
306 pRet = aFind->second.get();
309 OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
311 return pRet;
314 Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
315 const OUString& _rPath)
317 OSL_ASSERT(_rxConfProvider.is());
318 Sequence< Any > args(1);
319 args[0] <<= NamedValue( "nodepath", makeAny(_rPath));
320 Reference< XInterface > xInterface(
321 _rxConfProvider->createInstanceWithArguments(
322 "com.sun.star.configuration.ConfigurationAccess",
323 args));
324 OSL_ENSURE(
325 xInterface.is(),
326 "::createWithProvider: could not create the node access!");
327 return xInterface;
330 Reference<XInterface> OPoolCollection::openNode(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
332 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
333 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
334 Reference< XInterface > xNode;
338 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
340 xNode.set(xDirectAccess->getByName(_rPath), css::uno::UNO_QUERY);
341 SAL_WARN_IF(
342 !xNode.is(), "connectivity.cpool",
343 "OConfigurationNode::openNode: could not open the node!");
345 else if (xHierarchyAccess.is())
347 xNode.set(
348 xHierarchyAccess->getByHierarchicalName(_rPath),
349 css::uno::UNO_QUERY);
350 SAL_WARN_IF(
351 !xNode.is(), "connectivity.cpool",
352 "OConfigurationNode::openNode: could not open the node!");
356 catch(const NoSuchElementException&)
358 SAL_WARN("connectivity.cpool", "::openNode: there is no element named " <<
359 _rPath << "!");
361 catch(Exception&)
363 css::uno::Any ex( cppu::getCaughtException() );
364 SAL_WARN("connectivity.cpool", "OConfigurationNode::openNode: caught an exception while retrieving the node! " << exceptionToString(ex));
366 return xNode;
369 Any OPoolCollection::getNodeValue(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
371 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
372 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
373 Any aReturn;
376 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
378 aReturn = xDirectAccess->getByName(_rPath);
380 else if (xHierarchyAccess.is())
382 aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
385 catch(NoSuchElementException& e)
387 SAL_WARN("connectivity.cpool", e );
389 return aReturn;
392 void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ )
396 void SAL_CALL OPoolCollection::notifyTermination( const EventObject& /*Event*/ )
398 clearDesktop();
401 void SAL_CALL OPoolCollection::disposing( const EventObject& Source )
403 MutexGuard aGuard(m_aMutex);
404 if ( m_xDesktop == Source.Source )
406 clearDesktop();
408 else
412 Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
413 if(Source.Source == m_xConfigNode)
415 if ( xProp.is() )
416 xProp->removePropertyChangeListener(getEnablePoolingNodeName(),this);
417 m_xConfigNode.clear();
419 else if ( xProp.is() )
420 xProp->removePropertyChangeListener(getEnableNodeName(),this);
422 catch(const Exception&)
424 SAL_WARN("connectivity.cpool", "Exception caught");
429 void SAL_CALL OPoolCollection::propertyChange( const css::beans::PropertyChangeEvent& evt )
431 MutexGuard aGuard(m_aMutex);
432 if(evt.Source == m_xConfigNode)
434 bool bEnabled = true;
435 evt.NewValue >>= bEnabled;
436 if(!bEnabled )
438 m_aDriverProxies.clear();
439 m_aDriverProxies = MapDriver2DriverRef();
440 clearConnectionPools(false);
443 else if(evt.Source.is())
445 bool bEnabled = true;
446 evt.NewValue >>= bEnabled;
447 if(!bEnabled)
449 OUString sThisDriverName;
450 getNodeValue(getDriverNameNodeName(),evt.Source) >>= sThisDriverName;
451 // 1st release the driver
452 // look if we already have a proxy for this driver
453 MapDriver2DriverRef::iterator aLookup = m_aDriverProxies.begin();
454 while( aLookup != m_aDriverProxies.end())
456 MapDriver2DriverRef::iterator aFind = aLookup;
457 Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
458 ++aLookup;
459 if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
460 m_aDriverProxies.erase(aFind);
463 // 2nd clear the connectionpool
464 OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
465 if(aFind != m_aPools.end())
467 aFind->second->clear(false);
468 m_aPools.erase(aFind);
474 void OPoolCollection::clearDesktop()
476 clearConnectionPools(true);
477 if ( m_xDesktop.is() )
478 m_xDesktop->removeTerminateListener(this);
479 m_xDesktop.clear();
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */