nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / cpool / ZPoolCollection.cxx
blob53c1cea0d3c393c716d2128b067281f4b27a27e8
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 <tools/diagnose_ex.h>
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 static OUString getConnectionPoolNodeName()
48 return "org.openoffice.Office.DataAccess/ConnectionPool";
51 static OUString getEnablePoolingNodeName()
53 return "EnablePooling";
56 static OUString getDriverNameNodeName()
58 return "DriverName";
61 static OUString getDriverSettingsNodeName()
63 return "DriverSettings";
66 static OUString getEnableNodeName()
68 return "Enable";
72 OPoolCollection::OPoolCollection(const Reference< XComponentContext >& _rxContext)
73 :m_xContext(_rxContext)
75 // bootstrap all objects supporting the .sdb.Driver service
76 m_xManager = DriverManager::create( m_xContext );
78 m_xProxyFactory = ProxyFactory::create( m_xContext );
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 );
87 m_xDesktop = css::frame::Desktop::create( m_xContext );
88 m_xDesktop->addTerminateListener(this);
91 osl_atomic_decrement( &m_refCount );
94 OPoolCollection::~OPoolCollection()
96 clearConnectionPools(false);
99 Reference< XConnection > SAL_CALL OPoolCollection::getConnection( const OUString& _rURL )
101 return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
104 Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo )
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())
113 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
115 if(pConnectionPool)
116 xConnection = pConnectionPool->getConnectionWithInfo(_rURL,_rInfo);
118 else if(xDriver.is())
119 xConnection = xDriver->connect(_rURL,_rInfo);
121 return xConnection;
124 void SAL_CALL OPoolCollection::setLoginTimeout( sal_Int32 seconds )
126 MutexGuard aGuard(m_aMutex);
127 m_xManager->setLoginTimeout(seconds);
130 sal_Int32 SAL_CALL OPoolCollection::getLoginTimeout( )
132 MutexGuard aGuard(m_aMutex);
133 return m_xManager->getLoginTimeout();
136 OUString SAL_CALL OPoolCollection::getImplementationName( )
138 return "com.sun.star.sdbc.OConnectionPool";
141 sal_Bool SAL_CALL OPoolCollection::supportsService( const OUString& _rServiceName )
143 return cppu::supportsService(this, _rServiceName);
147 Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames( )
149 return { "com.sun.star.sdbc.ConnectionPool" };
152 Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const OUString& _rURL )
154 // returns the original driver when no connection pooling is enabled else it returns the proxy
155 MutexGuard aGuard(m_aMutex);
157 Reference< XDriver > xDriver;
158 Reference< XInterface > xDriverNode;
159 OUString sImplName;
160 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
162 Reference< XDriver > xExistentProxy;
163 // look if we already have a proxy for this driver
164 for (const auto& [rxDriver, rxDriverRef] : m_aDriverProxies)
166 // hold the proxy alive as long as we're in this loop round
167 xExistentProxy = rxDriverRef;
169 if (xExistentProxy.is() && (rxDriver.get() == xDriver.get()))
170 // already created a proxy for this
171 break;
173 if (xExistentProxy.is())
175 xDriver = xExistentProxy;
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.get());
181 OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
183 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
184 xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
188 return xDriver;
191 bool OPoolCollection::isDriverPoolingEnabled(const OUString& _sDriverImplName,
192 Reference< XInterface >& _rxDriverNode)
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);
199 if(xDirectAccess.is())
201 Sequence< OUString > aDriverKeys = xDirectAccess->getElementNames();
202 const OUString* pDriverKeys = aDriverKeys.getConstArray();
203 const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
204 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
206 // the name of the driver in this round
207 if(_sDriverImplName == *pDriverKeys)
209 _rxDriverNode = openNode(*pDriverKeys,xDirectAccess);
210 if(_rxDriverNode.is())
211 getNodeValue(getEnableNodeName(),_rxDriverNode) >>= bEnabled;
212 break;
216 return bEnabled;
219 bool OPoolCollection::isPoolingEnabled()
221 // the config node where all pooling relevant info are stored under
222 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
224 // the global "enabled" flag
225 bool bEnabled = false;
226 if(xConnectionPoolRoot.is())
227 getNodeValue(getEnablePoolingNodeName(),xConnectionPoolRoot) >>= bEnabled;
228 return bEnabled;
231 Reference<XInterface> const & OPoolCollection::getConfigPoolRoot()
233 if(!m_xConfigNode.is())
234 m_xConfigNode = createWithProvider(
235 css::configuration::theDefaultProvider::get(m_xContext),
236 getConnectionPoolNodeName());
237 return m_xConfigNode;
240 bool OPoolCollection::isPoolingEnabledByUrl(const OUString& _sUrl,
241 Reference< XDriver >& _rxDriver,
242 OUString& _rsImplName,
243 Reference< XInterface >& _rxDriverNode)
245 bool bEnabled = false;
246 _rxDriver = m_xManager->getDriverByURL(_sUrl);
247 if (_rxDriver.is() && isPoolingEnabled())
249 Reference< XServiceInfo > xServiceInfo(_rxDriver,UNO_QUERY);
250 OSL_ENSURE(xServiceInfo.is(),"Each driver should have a XServiceInfo interface!");
252 if(xServiceInfo.is())
254 // look for the implementation name of the driver
255 _rsImplName = xServiceInfo->getImplementationName();
256 bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
259 return bEnabled;
262 void OPoolCollection::clearConnectionPools(bool _bDispose)
264 for(auto& rEntry : m_aPools)
266 rEntry.second->clear(_bDispose);
268 m_aPools.clear();
271 OConnectionPool* OPoolCollection::getConnectionPool(const OUString& _sImplName,
272 const Reference< XDriver >& _xDriver,
273 const Reference< XInterface >& _xDriverNode)
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())
281 Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
282 if(xProp.is())
283 xProp->addPropertyChangeListener(getEnableNodeName(),this);
284 OConnectionPool* pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
285 aFind = m_aPools.emplace(_sImplName,pConnectionPool).first;
286 pRet = aFind->second.get();
289 OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
291 return pRet;
294 Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
295 const OUString& _rPath)
297 OSL_ASSERT(_rxConfProvider.is());
298 Sequence< Any > args(1);
299 args[0] <<= NamedValue( "nodepath", makeAny(_rPath));
300 Reference< XInterface > xInterface(
301 _rxConfProvider->createInstanceWithArguments(
302 "com.sun.star.configuration.ConfigurationAccess",
303 args));
304 OSL_ENSURE(
305 xInterface.is(),
306 "::createWithProvider: could not create the node access!");
307 return xInterface;
310 Reference<XInterface> OPoolCollection::openNode(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
312 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
313 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
314 Reference< XInterface > xNode;
318 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
320 xNode.set(xDirectAccess->getByName(_rPath), css::uno::UNO_QUERY);
321 SAL_WARN_IF(
322 !xNode.is(), "connectivity.cpool",
323 "OConfigurationNode::openNode: could not open the node!");
325 else if (xHierarchyAccess.is())
327 xNode.set(
328 xHierarchyAccess->getByHierarchicalName(_rPath),
329 css::uno::UNO_QUERY);
330 SAL_WARN_IF(
331 !xNode.is(), "connectivity.cpool",
332 "OConfigurationNode::openNode: could not open the node!");
336 catch(const NoSuchElementException&)
338 SAL_WARN("connectivity.cpool", "::openNode: there is no element named " <<
339 _rPath << "!");
341 catch(const Exception&)
343 TOOLS_WARN_EXCEPTION("connectivity.cpool", "OConfigurationNode::openNode: caught an exception while retrieving the node");
345 return xNode;
348 Any OPoolCollection::getNodeValue(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
350 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
351 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
352 Any aReturn;
355 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
357 aReturn = xDirectAccess->getByName(_rPath);
359 else if (xHierarchyAccess.is())
361 aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
364 catch(const NoSuchElementException&)
366 TOOLS_WARN_EXCEPTION("connectivity.cpool", "" );
368 return aReturn;
371 void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ )
375 void SAL_CALL OPoolCollection::notifyTermination( const EventObject& /*Event*/ )
377 clearDesktop();
380 void SAL_CALL OPoolCollection::disposing( const EventObject& Source )
382 MutexGuard aGuard(m_aMutex);
383 if ( m_xDesktop == Source.Source )
385 clearDesktop();
387 else
391 Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
392 if(Source.Source == m_xConfigNode)
394 if ( xProp.is() )
395 xProp->removePropertyChangeListener(getEnablePoolingNodeName(),this);
396 m_xConfigNode.clear();
398 else if ( xProp.is() )
399 xProp->removePropertyChangeListener(getEnableNodeName(),this);
401 catch(const Exception&)
403 TOOLS_WARN_EXCEPTION("connectivity.cpool", "");
408 void SAL_CALL OPoolCollection::propertyChange( const css::beans::PropertyChangeEvent& evt )
410 MutexGuard aGuard(m_aMutex);
411 if(evt.Source == m_xConfigNode)
413 bool bEnabled = true;
414 evt.NewValue >>= bEnabled;
415 if(!bEnabled )
417 m_aDriverProxies.clear();
418 m_aDriverProxies = MapDriver2DriverRef();
419 clearConnectionPools(false);
422 else if(evt.Source.is())
424 bool bEnabled = true;
425 evt.NewValue >>= bEnabled;
426 if(!bEnabled)
428 OUString sThisDriverName;
429 getNodeValue(getDriverNameNodeName(),evt.Source) >>= sThisDriverName;
430 // 1st release the driver
431 // look if we already have a proxy for this driver
432 MapDriver2DriverRef::iterator aLookup = m_aDriverProxies.begin();
433 while( aLookup != m_aDriverProxies.end())
435 MapDriver2DriverRef::iterator aFind = aLookup;
436 Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
437 ++aLookup;
438 if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
439 m_aDriverProxies.erase(aFind);
442 // 2nd clear the connectionpool
443 OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
444 if(aFind != m_aPools.end())
446 aFind->second->clear(false);
447 m_aPools.erase(aFind);
453 void OPoolCollection::clearDesktop()
455 clearConnectionPools(true);
456 if ( m_xDesktop.is() )
457 m_xDesktop->removeTerminateListener(this);
458 m_xDesktop.clear();
461 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
462 connectivity_OPoolCollection_get_implementation(
463 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
465 return cppu::acquire(new OPoolCollection(context));
469 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */