bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / cpool / ZPoolCollection.cxx
blobdf677dc6de5e885fcad4880da40bc1234eb0007f
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 "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 OUString( "org.openoffice.Office.DataAccess/ConnectionPool" );
51 static OUString getEnablePoolingNodeName()
53 return OUString( "EnablePooling" );
56 static OUString getDriverNameNodeName()
58 return OUString( "DriverName" );
61 static OUString getDriverSettingsNodeName()
63 return OUString( "DriverSettings" );
66 static OUString getEnableNodeName()
68 return OUString( "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 = ::com::sun::star::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 ) throw(SQLException, RuntimeException, std::exception)
101 return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
104 Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException, std::exception)
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 ) throw(RuntimeException, std::exception)
126 MutexGuard aGuard(m_aMutex);
127 m_xManager->setLoginTimeout(seconds);
130 sal_Int32 SAL_CALL OPoolCollection::getLoginTimeout( ) throw(RuntimeException, std::exception)
132 MutexGuard aGuard(m_aMutex);
133 return m_xManager->getLoginTimeout();
136 OUString SAL_CALL OPoolCollection::getImplementationName( ) throw(RuntimeException, std::exception)
138 MutexGuard aGuard(m_aMutex);
139 return getImplementationName_Static();
142 sal_Bool SAL_CALL OPoolCollection::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
144 return cppu::supportsService(this, _rServiceName);
148 Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames( ) throw(RuntimeException, std::exception)
150 return getSupportedServiceNames_Static();
153 //---------------------------------------OPoolCollection----------------------------------
154 Reference< XInterface > SAL_CALL OPoolCollection::CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
156 return static_cast<XDriverManager*>(new OPoolCollection(comphelper::getComponentContext(_rxFactory)));
160 OUString SAL_CALL OPoolCollection::getImplementationName_Static( ) throw(RuntimeException)
162 return OUString("com.sun.star.sdbc.OConnectionPool");
166 Sequence< OUString > SAL_CALL OPoolCollection::getSupportedServiceNames_Static( ) throw(RuntimeException)
168 Sequence< OUString > aSupported(1);
169 aSupported[0] = "com.sun.star.sdbc.ConnectionPool";
170 return aSupported;
173 Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const OUString& _rURL ) throw(RuntimeException, std::exception)
175 // returns the original driver when no connection pooling is enabled else it returns the proxy
176 MutexGuard aGuard(m_aMutex);
178 Reference< XDriver > xDriver;
179 Reference< XInterface > xDriverNode;
180 OUString sImplName;
181 if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
183 Reference< XDriver > xExistentProxy;
184 // look if we already have a proxy for this driver
185 for ( MapDriver2DriverRef::const_iterator aLookup = m_aDriverProxies.begin();
186 aLookup != m_aDriverProxies.end();
187 ++aLookup
190 // hold the proxy alive as long as we're in this loop round
191 xExistentProxy = aLookup->second;
193 if (xExistentProxy.is() && (aLookup->first.get() == xDriver.get()))
194 // already created a proxy for this
195 break;
197 if (xExistentProxy.is())
199 xDriver = xExistentProxy;
201 else
202 { // create a new proxy for the driver
203 // this allows us to control the connections created by it
204 Reference< XAggregation > xDriverProxy = m_xProxyFactory->createProxy(xDriver.get());
205 OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
207 OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
208 xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
212 return xDriver;
215 bool OPoolCollection::isDriverPoolingEnabled(const OUString& _sDriverImplName,
216 Reference< XInterface >& _rxDriverNode)
218 bool bEnabled = false;
219 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
220 // then look for which of them settings are stored in the configuration
221 Reference< XNameAccess > xDirectAccess(openNode(getDriverSettingsNodeName(),xConnectionPoolRoot),UNO_QUERY);
223 if(xDirectAccess.is())
225 Sequence< OUString > aDriverKeys = xDirectAccess->getElementNames();
226 const OUString* pDriverKeys = aDriverKeys.getConstArray();
227 const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
228 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
230 // the name of the driver in this round
231 if(_sDriverImplName == *pDriverKeys)
233 _rxDriverNode = openNode(*pDriverKeys,xDirectAccess);
234 if(_rxDriverNode.is())
235 getNodeValue(getEnableNodeName(),_rxDriverNode) >>= bEnabled;
236 break;
240 return bEnabled;
243 bool OPoolCollection::isPoolingEnabled()
245 // the config node where all pooling relevant info are stored under
246 Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
248 // the global "enabled" flag
249 bool bEnabled = false;
250 if(xConnectionPoolRoot.is())
251 getNodeValue(getEnablePoolingNodeName(),xConnectionPoolRoot) >>= bEnabled;
252 return bEnabled;
255 Reference<XInterface> OPoolCollection::getConfigPoolRoot()
257 if(!m_xConfigNode.is())
258 m_xConfigNode = createWithServiceFactory(getConnectionPoolNodeName());
259 return m_xConfigNode;
262 bool OPoolCollection::isPoolingEnabledByUrl(const OUString& _sUrl,
263 Reference< XDriver >& _rxDriver,
264 OUString& _rsImplName,
265 Reference< XInterface >& _rxDriverNode)
267 bool bEnabled = false;
268 _rxDriver = m_xManager->getDriverByURL(_sUrl);
269 if (_rxDriver.is() && isPoolingEnabled())
271 Reference< XServiceInfo > xSerivceInfo(_rxDriver,UNO_QUERY);
272 OSL_ENSURE(xSerivceInfo.is(),"Each driver should have a XServiceInfo interface!");
274 if(xSerivceInfo.is())
276 // look for the implementation name of the driver
277 _rsImplName = xSerivceInfo->getImplementationName();
278 bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
281 return bEnabled;
284 void OPoolCollection::clearConnectionPools(bool _bDispose)
286 OConnectionPools::const_iterator aIter = m_aPools.begin();
287 while(aIter != m_aPools.end())
289 aIter->second->clear(_bDispose);
290 aIter->second->release();
291 OUString sKeyValue = aIter->first;
292 ++aIter;
293 m_aPools.erase(sKeyValue);
297 OConnectionPool* OPoolCollection::getConnectionPool(const OUString& _sImplName,
298 const Reference< XDriver >& _xDriver,
299 const Reference< XInterface >& _xDriverNode)
301 OConnectionPool *pRet = 0;
302 OConnectionPools::const_iterator aFind = m_aPools.find(_sImplName);
303 if (aFind != m_aPools.end())
304 pRet = aFind->second;
305 else if (_xDriver.is() && _xDriverNode.is())
307 Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
308 if(xProp.is())
309 xProp->addPropertyChangeListener(getEnableNodeName(),this);
310 OConnectionPool* pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
311 pConnectionPool->acquire();
312 aFind = m_aPools.insert(OConnectionPools::value_type(_sImplName,pConnectionPool)).first;
313 pRet = aFind->second;
316 OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
318 return pRet;
321 Reference< XInterface > OPoolCollection::createWithServiceFactory(const OUString& _rPath) const
323 return createWithProvider(
324 com::sun::star::configuration::theDefaultProvider::get(m_xContext),
325 _rPath);
328 Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
329 const OUString& _rPath)
331 OSL_ASSERT(_rxConfProvider.is());
332 Sequence< Any > args(1);
333 args[0] = makeAny(
334 NamedValue(
335 OUString("nodepath"),
336 makeAny(_rPath)));
337 Reference< XInterface > xInterface(
338 _rxConfProvider->createInstanceWithArguments(
339 OUString( "com.sun.star.configuration.ConfigurationAccess"),
340 args));
341 OSL_ENSURE(
342 xInterface.is(),
343 "::createWithProvider: could not create the node access!");
344 return xInterface;
347 Reference<XInterface> OPoolCollection::openNode(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
349 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
350 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
351 Reference< XInterface > xNode;
355 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
357 xNode.set(xDirectAccess->getByName(_rPath), css::uno::UNO_QUERY);
358 SAL_WARN_IF(
359 !xNode.is(), "connectivity.cpool",
360 "OConfigurationNode::openNode: could not open the node!");
362 else if (xHierarchyAccess.is())
364 xNode.set(
365 xHierarchyAccess->getByHierarchicalName(_rPath),
366 css::uno::UNO_QUERY);
367 SAL_WARN_IF(
368 !xNode.is(), "connectivity.cpool",
369 "OConfigurationNode::openNode: could not open the node!");
373 catch(const NoSuchElementException&)
375 SAL_WARN("connectivity.cpool", "::openNode: there is no element named " <<
376 _rPath << "!");
378 catch(Exception&)
380 SAL_WARN("connectivity.cpool", "OConfigurationNode::openNode: caught an exception while retrieving the node!");
382 return xNode;
385 Any OPoolCollection::getNodeValue(const OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
387 Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
388 Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
389 Any aReturn;
392 if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
394 aReturn = xDirectAccess->getByName(_rPath);
396 else if (xHierarchyAccess.is())
398 aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
401 catch(NoSuchElementException& e)
403 SAL_WARN("connectivity.cpool", "::getNodeValue: caught a "
404 "NoSuchElementException while trying to open " <<
405 e.Message << "!" );
407 return aReturn;
410 void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ ) throw (::com::sun::star::frame::TerminationVetoException, RuntimeException, std::exception)
414 void SAL_CALL OPoolCollection::notifyTermination( const EventObject& /*Event*/ ) throw (RuntimeException, std::exception)
416 clearDesktop();
419 void SAL_CALL OPoolCollection::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
421 MutexGuard aGuard(m_aMutex);
422 if ( m_xDesktop == Source.Source )
424 clearDesktop();
426 else
430 Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
431 if(Source.Source == m_xConfigNode)
433 if ( xProp.is() )
434 xProp->removePropertyChangeListener(getEnablePoolingNodeName(),this);
435 m_xConfigNode.clear();
437 else if ( xProp.is() )
438 xProp->removePropertyChangeListener(getEnableNodeName(),this);
440 catch(const Exception&)
442 SAL_WARN("connectivity.cpool", "Exception caught");
447 void SAL_CALL OPoolCollection::propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (RuntimeException, std::exception)
449 MutexGuard aGuard(m_aMutex);
450 if(evt.Source == m_xConfigNode)
452 bool bEnabled = true;
453 evt.NewValue >>= bEnabled;
454 if(!bEnabled )
456 m_aDriverProxies.clear();
457 m_aDriverProxies = MapDriver2DriverRef();
458 OConnectionPools::iterator aIter = m_aPools.begin();
459 for(;aIter != m_aPools.end();++aIter)
461 aIter->second->clear(false);
462 aIter->second->release();
464 m_aPools.clear();
465 m_aPools = OConnectionPools();
468 else if(evt.Source.is())
470 bool bEnabled = true;
471 evt.NewValue >>= bEnabled;
472 if(!bEnabled)
474 OUString sThisDriverName;
475 getNodeValue(getDriverNameNodeName(),evt.Source) >>= sThisDriverName;
476 // 1nd relase the driver
477 // look if we already have a proxy for this driver
478 MapDriver2DriverRef::iterator aLookup = m_aDriverProxies.begin();
479 while( aLookup != m_aDriverProxies.end())
481 MapDriver2DriverRef::iterator aFind = aLookup;
482 Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
483 ++aLookup;
484 if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
485 m_aDriverProxies.erase(aFind);
488 // 2nd clear the connectionpool
489 OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
490 if(aFind != m_aPools.end() && aFind->second)
492 aFind->second->clear(false);
493 aFind->second->release();
494 m_aPools.erase(aFind);
500 void OPoolCollection::clearDesktop()
502 clearConnectionPools(true);
503 if ( m_xDesktop.is() )
504 m_xDesktop->removeTerminateListener(this);
505 m_xDesktop.clear();
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */