1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include "ZPoolCollection.hxx"
31*cdf0e10cSrcweir #include "ZDriverWrapper.hxx"
32*cdf0e10cSrcweir #include "ZConnectionPool.hxx"
33*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
35*cdf0e10cSrcweir #include <comphelper/extract.hxx>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37*cdf0e10cSrcweir #include "diagnose_ex.h"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
41*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
42*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
43*cdf0e10cSrcweir using namespace ::com::sun::star::container;
44*cdf0e10cSrcweir using namespace ::com::sun::star::reflection;
45*cdf0e10cSrcweir using namespace ::osl;
46*cdf0e10cSrcweir using namespace connectivity;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir //--------------------------------------------------------------------
49*cdf0e10cSrcweir static const ::rtl::OUString& getConnectionPoolNodeName()
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("org.openoffice.Office.DataAccess/ConnectionPool");
52*cdf0e10cSrcweir 	return s_sNodeName;
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir //--------------------------------------------------------------------
55*cdf0e10cSrcweir static const ::rtl::OUString& getEnablePoolingNodeName()
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("EnablePooling");
58*cdf0e10cSrcweir 	return s_sNodeName;
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir //--------------------------------------------------------------------
61*cdf0e10cSrcweir static const ::rtl::OUString& getDriverNameNodeName()
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverName");
64*cdf0e10cSrcweir 	return s_sNodeName;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir // -----------------------------------------------------------------------------
67*cdf0e10cSrcweir static const ::rtl::OUString& getDriverSettingsNodeName()
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverSettings");
70*cdf0e10cSrcweir 	return s_sNodeName;
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir //--------------------------------------------------------------------------
73*cdf0e10cSrcweir static const ::rtl::OUString& getEnableNodeName()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Enable");
76*cdf0e10cSrcweir 	return s_sNodeName;
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir //--------------------------------------------------------------------
80*cdf0e10cSrcweir OPoolCollection::OPoolCollection(const Reference< XMultiServiceFactory >&	_rxFactory)
81*cdf0e10cSrcweir 	:m_xServiceFactory(_rxFactory)
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	// bootstrap all objects supporting the .sdb.Driver service
84*cdf0e10cSrcweir 	m_xManager = Reference< XDriverManager >(m_xServiceFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.sdbc.DriverManager") ), UNO_QUERY);
85*cdf0e10cSrcweir 	m_xDriverAccess = Reference< XDriverAccess >(m_xManager, UNO_QUERY);
86*cdf0e10cSrcweir 	OSL_ENSURE(m_xDriverAccess.is(), "have no (or an invalid) driver manager!");
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	m_xProxyFactory = Reference< XProxyFactory >(
89*cdf0e10cSrcweir 		m_xServiceFactory->createInstance(
90*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii("com.sun.star.reflection.ProxyFactory")),
91*cdf0e10cSrcweir 		UNO_QUERY);
92*cdf0e10cSrcweir 	OSL_ENSURE(m_xProxyFactory.is(), "OConnectionPool::OConnectionPool: could not create a proxy factory!");
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	Reference<XPropertySet> xProp(getConfigPoolRoot(),UNO_QUERY);
95*cdf0e10cSrcweir 	if ( xProp.is() )
96*cdf0e10cSrcweir 		xProp->addPropertyChangeListener(getEnablePoolingNodeName(),this);
97*cdf0e10cSrcweir 	// attach as desktop listener to know when we have to release our pools
98*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_refCount );
99*cdf0e10cSrcweir 	{
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 		m_xDesktop = Reference< ::com::sun::star::frame::XDesktop>( m_xServiceFactory->createInstance(::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop") ), UNO_QUERY);
102*cdf0e10cSrcweir 		if ( m_xDesktop.is() )
103*cdf0e10cSrcweir 			m_xDesktop->addTerminateListener(this);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 	osl_decrementInterlockedCount( &m_refCount );
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir // -----------------------------------------------------------------------------
109*cdf0e10cSrcweir OPoolCollection::~OPoolCollection()
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	clearConnectionPools(sal_False);
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir // -----------------------------------------------------------------------------
114*cdf0e10cSrcweir Reference< XConnection > SAL_CALL OPoolCollection::getConnection( const ::rtl::OUString& _rURL ) throw(SQLException, RuntimeException)
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir 	return getConnectionWithInfo(_rURL,Sequence< PropertyValue >());
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir // -----------------------------------------------------------------------------
119*cdf0e10cSrcweir Reference< XConnection > SAL_CALL OPoolCollection::getConnectionWithInfo( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException)
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
122*cdf0e10cSrcweir 	Reference< XConnection > xConnection;
123*cdf0e10cSrcweir 	Reference< XDriver > xDriver;
124*cdf0e10cSrcweir 	Reference< XInterface > xDriverNode;
125*cdf0e10cSrcweir 	::rtl::OUString sImplName;
126*cdf0e10cSrcweir 	if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode) && xDriver.is())
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		if(pConnectionPool)
131*cdf0e10cSrcweir 			xConnection = pConnectionPool->getConnectionWithInfo(_rURL,_rInfo);
132*cdf0e10cSrcweir 	}
133*cdf0e10cSrcweir 	else if(xDriver.is())
134*cdf0e10cSrcweir 		xConnection = xDriver->connect(_rURL,_rInfo);
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	return xConnection;
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir // -----------------------------------------------------------------------------
139*cdf0e10cSrcweir void SAL_CALL OPoolCollection::setLoginTimeout( sal_Int32 seconds ) throw(RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
142*cdf0e10cSrcweir 	m_xManager->setLoginTimeout(seconds);
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir // -----------------------------------------------------------------------------
145*cdf0e10cSrcweir sal_Int32 SAL_CALL OPoolCollection::getLoginTimeout(  ) throw(RuntimeException)
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
148*cdf0e10cSrcweir 	return m_xManager->getLoginTimeout();
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir // -----------------------------------------------------------------------------
151*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OPoolCollection::getImplementationName(  ) throw(RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
154*cdf0e10cSrcweir 	return getImplementationName_Static();
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir //--------------------------------------------------------------------------
158*cdf0e10cSrcweir sal_Bool SAL_CALL OPoolCollection::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
161*cdf0e10cSrcweir 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
162*cdf0e10cSrcweir 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
163*cdf0e10cSrcweir 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
164*cdf0e10cSrcweir 		;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	return pSupported != pEnd;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir //--------------------------------------------------------------------------
170*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL OPoolCollection::getSupportedServiceNames(  ) throw(RuntimeException)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir //---------------------------------------OPoolCollection----------------------------------
176*cdf0e10cSrcweir Reference< XInterface > SAL_CALL OPoolCollection::CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	return static_cast<XDriverManager*>(new OPoolCollection(_rxFactory));
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir //--------------------------------------------------------------------------
182*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OPoolCollection::getImplementationName_Static(  ) throw(RuntimeException)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	return ::rtl::OUString::createFromAscii("com.sun.star.sdbc.OConnectionPool");
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir //--------------------------------------------------------------------------
188*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL OPoolCollection::getSupportedServiceNames_Static(  ) throw(RuntimeException)
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSupported(1);
191*cdf0e10cSrcweir 	aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.ConnectionPool");
192*cdf0e10cSrcweir 	return aSupported;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir // -----------------------------------------------------------------------------
195*cdf0e10cSrcweir Reference< XDriver > SAL_CALL OPoolCollection::getDriverByURL( const ::rtl::OUString& _rURL ) throw(RuntimeException)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir 	// returns the original driver when no connection pooling is enabled else it returns the proxy
198*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 	Reference< XDriver > xDriver;
201*cdf0e10cSrcweir 	Reference< XInterface > xDriverNode;
202*cdf0e10cSrcweir 	::rtl::OUString sImplName;
203*cdf0e10cSrcweir 	if(isPoolingEnabledByUrl(_rURL,xDriver,sImplName,xDriverNode))
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 		Reference< XDriver > xExistentProxy;
206*cdf0e10cSrcweir 		// look if we already have a proxy for this driver
207*cdf0e10cSrcweir 		for	(	ConstMapDriver2DriverRefIterator aLookup = m_aDriverProxies.begin();
208*cdf0e10cSrcweir 				aLookup != m_aDriverProxies.end();
209*cdf0e10cSrcweir 				++aLookup
210*cdf0e10cSrcweir 			)
211*cdf0e10cSrcweir 		{
212*cdf0e10cSrcweir 			// hold the proxy alive as long as we're in this loop round
213*cdf0e10cSrcweir 			xExistentProxy = aLookup->second;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 			if (xExistentProxy.is() && (aLookup->first.get() == xDriver.get()))
216*cdf0e10cSrcweir 				// already created a proxy for this
217*cdf0e10cSrcweir 				break;
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 		if (xExistentProxy.is())
220*cdf0e10cSrcweir 		{
221*cdf0e10cSrcweir 			xDriver = xExistentProxy;
222*cdf0e10cSrcweir 		}
223*cdf0e10cSrcweir 		else
224*cdf0e10cSrcweir 		{	// create a new proxy for the driver
225*cdf0e10cSrcweir 			// this allows us to control the connections created by it
226*cdf0e10cSrcweir 			if (m_xProxyFactory.is())
227*cdf0e10cSrcweir 			{
228*cdf0e10cSrcweir 				Reference< XAggregation > xDriverProxy = m_xProxyFactory->createProxy(xDriver.get());
229*cdf0e10cSrcweir 				OSL_ENSURE(xDriverProxy.is(), "OConnectionPool::getDriverByURL: invalid proxy returned by the proxy factory!");
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 				OConnectionPool* pConnectionPool = getConnectionPool(sImplName,xDriver,xDriverNode);
232*cdf0e10cSrcweir 				xDriver = new ODriverWrapper(xDriverProxy, pConnectionPool);
233*cdf0e10cSrcweir 			}
234*cdf0e10cSrcweir 			else
235*cdf0e10cSrcweir 				OSL_ENSURE(sal_False, "OConnectionPool::getDriverByURL: could not instantiate a proxy factory!");
236*cdf0e10cSrcweir 		}
237*cdf0e10cSrcweir 	}
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 	return xDriver;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir // -----------------------------------------------------------------------------
242*cdf0e10cSrcweir sal_Bool OPoolCollection::isDriverPoolingEnabled(const ::rtl::OUString& _sDriverImplName,
243*cdf0e10cSrcweir 												 Reference< XInterface >& _rxDriverNode)
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	sal_Bool bEnabled = sal_False;
246*cdf0e10cSrcweir 	Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
247*cdf0e10cSrcweir 	// then look for which of them settings are stored in the configuration
248*cdf0e10cSrcweir 	Reference< XNameAccess > xDirectAccess(openNode(getDriverSettingsNodeName(),xConnectionPoolRoot),UNO_QUERY);
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	if(xDirectAccess.is())
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aDriverKeys = xDirectAccess->getElementNames();
253*cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
254*cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
255*cdf0e10cSrcweir 		for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
256*cdf0e10cSrcweir 		{
257*cdf0e10cSrcweir 			// the name of the driver in this round
258*cdf0e10cSrcweir 			if(_sDriverImplName == *pDriverKeys)
259*cdf0e10cSrcweir 			{
260*cdf0e10cSrcweir 				_rxDriverNode = openNode(*pDriverKeys,xDirectAccess);
261*cdf0e10cSrcweir 				if(_rxDriverNode.is())
262*cdf0e10cSrcweir 					getNodeValue(getEnableNodeName(),_rxDriverNode) >>= bEnabled;
263*cdf0e10cSrcweir 				break;
264*cdf0e10cSrcweir 			}
265*cdf0e10cSrcweir 		}
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 	return bEnabled;
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir // -----------------------------------------------------------------------------
270*cdf0e10cSrcweir sal_Bool OPoolCollection::isPoolingEnabled()
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir 	// the config node where all pooling relevant info are stored under
273*cdf0e10cSrcweir 	Reference<XInterface> xConnectionPoolRoot = getConfigPoolRoot();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	// the global "enabled" flag
276*cdf0e10cSrcweir 	sal_Bool bEnabled = sal_False;
277*cdf0e10cSrcweir 	if(xConnectionPoolRoot.is())
278*cdf0e10cSrcweir 		getNodeValue(getEnablePoolingNodeName(),xConnectionPoolRoot) >>= bEnabled;
279*cdf0e10cSrcweir 	return bEnabled;
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir // -----------------------------------------------------------------------------
282*cdf0e10cSrcweir Reference<XInterface> OPoolCollection::getConfigPoolRoot()
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	if(!m_xConfigNode.is())
285*cdf0e10cSrcweir 		m_xConfigNode = createWithServiceFactory(getConnectionPoolNodeName());
286*cdf0e10cSrcweir 	return m_xConfigNode;
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir // -----------------------------------------------------------------------------
289*cdf0e10cSrcweir sal_Bool OPoolCollection::isPoolingEnabledByUrl(const ::rtl::OUString& _sUrl,
290*cdf0e10cSrcweir 												Reference< XDriver >& _rxDriver,
291*cdf0e10cSrcweir 												::rtl::OUString& _rsImplName,
292*cdf0e10cSrcweir 												Reference< XInterface >& _rxDriverNode)
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir 	sal_Bool bEnabled = sal_False;
295*cdf0e10cSrcweir 	if (m_xDriverAccess.is())
296*cdf0e10cSrcweir 	{
297*cdf0e10cSrcweir 		_rxDriver = m_xDriverAccess->getDriverByURL(_sUrl);
298*cdf0e10cSrcweir 		if (_rxDriver.is() && isPoolingEnabled())
299*cdf0e10cSrcweir 		{
300*cdf0e10cSrcweir 			Reference< XServiceInfo > xSerivceInfo(_rxDriver,UNO_QUERY);
301*cdf0e10cSrcweir 			OSL_ENSURE(xSerivceInfo.is(),"Each driver should have a XServiceInfo interface!");
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 			if(xSerivceInfo.is())
304*cdf0e10cSrcweir 			{
305*cdf0e10cSrcweir 				// look for the implementation name of the driver
306*cdf0e10cSrcweir 				_rsImplName = xSerivceInfo->getImplementationName();
307*cdf0e10cSrcweir 				bEnabled = isDriverPoolingEnabled(_rsImplName,_rxDriverNode);
308*cdf0e10cSrcweir 			}
309*cdf0e10cSrcweir 		}
310*cdf0e10cSrcweir 	}
311*cdf0e10cSrcweir 	return bEnabled;
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir // -----------------------------------------------------------------------------
314*cdf0e10cSrcweir void OPoolCollection::clearConnectionPools(sal_Bool _bDispose)
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir 	OConnectionPools::const_iterator aIter = m_aPools.begin();
317*cdf0e10cSrcweir 	while(aIter != m_aPools.end())
318*cdf0e10cSrcweir 	{
319*cdf0e10cSrcweir 		aIter->second->clear(_bDispose);
320*cdf0e10cSrcweir 		aIter->second->release();
321*cdf0e10cSrcweir 		::rtl::OUString sKeyValue = aIter->first;
322*cdf0e10cSrcweir 		++aIter;
323*cdf0e10cSrcweir 		m_aPools.erase(sKeyValue);
324*cdf0e10cSrcweir 	}
325*cdf0e10cSrcweir }
326*cdf0e10cSrcweir // -----------------------------------------------------------------------------
327*cdf0e10cSrcweir OConnectionPool* OPoolCollection::getConnectionPool(const ::rtl::OUString& _sImplName,
328*cdf0e10cSrcweir 													const Reference< XDriver >& _xDriver,
329*cdf0e10cSrcweir 													const Reference< XInterface >& _xDriverNode)
330*cdf0e10cSrcweir {
331*cdf0e10cSrcweir 	OConnectionPool *pRet = 0;
332*cdf0e10cSrcweir 	OConnectionPools::const_iterator aFind = m_aPools.find(_sImplName);
333*cdf0e10cSrcweir 	if (aFind != m_aPools.end())
334*cdf0e10cSrcweir 		pRet = aFind->second;
335*cdf0e10cSrcweir 	else if (_xDriver.is() && _xDriverNode.is())
336*cdf0e10cSrcweir 	{
337*cdf0e10cSrcweir 		Reference<XPropertySet> xProp(_xDriverNode,UNO_QUERY);
338*cdf0e10cSrcweir 		if(xProp.is())
339*cdf0e10cSrcweir 			xProp->addPropertyChangeListener(getEnableNodeName(),this);
340*cdf0e10cSrcweir 		OConnectionPool* pConnectionPool = new OConnectionPool(_xDriver,_xDriverNode,m_xProxyFactory);
341*cdf0e10cSrcweir 		pConnectionPool->acquire();
342*cdf0e10cSrcweir 		aFind = m_aPools.insert(OConnectionPools::value_type(_sImplName,pConnectionPool)).first;
343*cdf0e10cSrcweir 		pRet = aFind->second;
344*cdf0e10cSrcweir 	}
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 	OSL_ENSURE(pRet, "Could not query DriverManager from ConnectionPool!");
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 	return pRet;
349*cdf0e10cSrcweir }
350*cdf0e10cSrcweir // -----------------------------------------------------------------------------
351*cdf0e10cSrcweir Reference< XInterface > OPoolCollection::createWithServiceFactory(const ::rtl::OUString& _rPath) const
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir 	Reference< XInterface > xInterface;
354*cdf0e10cSrcweir 	try
355*cdf0e10cSrcweir 	{
356*cdf0e10cSrcweir 		Reference< XInterface > xProvider = m_xServiceFactory->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider")));
357*cdf0e10cSrcweir 		OSL_ENSURE(xProvider.is(), "OConfigurationTreeRoot::createWithServiceFactory: could not instantiate the config provider service!");
358*cdf0e10cSrcweir 		Reference< XMultiServiceFactory > xProviderAsFac(xProvider, UNO_QUERY);
359*cdf0e10cSrcweir 		OSL_ENSURE(xProviderAsFac.is() || !xProvider.is(), "OConfigurationTreeRoot::createWithServiceFactory: the provider is missing an interface!");
360*cdf0e10cSrcweir 		if (xProviderAsFac.is())
361*cdf0e10cSrcweir 			xInterface = createWithProvider(xProviderAsFac, _rPath);
362*cdf0e10cSrcweir 	}
363*cdf0e10cSrcweir 	catch(const Exception&)
364*cdf0e10cSrcweir 	{
365*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "createWithServiceFactory: error while instantiating the provider service!");
366*cdf0e10cSrcweir 	}
367*cdf0e10cSrcweir 	return xInterface;
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir //------------------------------------------------------------------------
370*cdf0e10cSrcweir Reference< XInterface > OPoolCollection::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider,
371*cdf0e10cSrcweir 							const ::rtl::OUString& _rPath) const
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir 	OSL_ENSURE(_rxConfProvider.is(), "createWithProvider: invalid provider!");
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir 	Reference< XInterface > xInterface;
376*cdf0e10cSrcweir #ifdef DBG_UTIL
377*cdf0e10cSrcweir 	if (_rxConfProvider.is())
378*cdf0e10cSrcweir 	{
379*cdf0e10cSrcweir 		try
380*cdf0e10cSrcweir 		{
381*cdf0e10cSrcweir 			Reference< XServiceInfo > xSI(_rxConfProvider, UNO_QUERY);
382*cdf0e10cSrcweir 			if (!xSI.is())
383*cdf0e10cSrcweir 			{
384*cdf0e10cSrcweir 				OSL_ENSURE(sal_False, "::createWithProvider: no XServiceInfo interface on the provider!");
385*cdf0e10cSrcweir 			}
386*cdf0e10cSrcweir 			else
387*cdf0e10cSrcweir 			{
388*cdf0e10cSrcweir 				OSL_ENSURE(xSI->supportsService(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
389*cdf0e10cSrcweir 					"::createWithProvider: sure this is a provider? Missing the ConfigurationProvider service!");
390*cdf0e10cSrcweir 			}
391*cdf0e10cSrcweir 		}
392*cdf0e10cSrcweir 		catch(const Exception&)
393*cdf0e10cSrcweir 		{
394*cdf0e10cSrcweir 			OSL_ENSURE(sal_False, "::createWithProvider: unable to check the service conformance of the provider given!");
395*cdf0e10cSrcweir 		}
396*cdf0e10cSrcweir 	}
397*cdf0e10cSrcweir #endif
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir 	if (_rxConfProvider.is())
400*cdf0e10cSrcweir 	{
401*cdf0e10cSrcweir 		try
402*cdf0e10cSrcweir 		{
403*cdf0e10cSrcweir 			Sequence< Any > aCreationArgs(3);
404*cdf0e10cSrcweir 			aCreationArgs[0] = makeAny(PropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), 0, makeAny(_rPath), PropertyState_DIRECT_VALUE));
405*cdf0e10cSrcweir 			aCreationArgs[1] = makeAny(PropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("depth")), 0, makeAny((sal_Int32)-1), PropertyState_DIRECT_VALUE));
406*cdf0e10cSrcweir 			aCreationArgs[2] = makeAny(PropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")), 0, makeAny(sal_True), PropertyState_DIRECT_VALUE));
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir 			static ::rtl::OUString sAccessService = ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess");
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 			xInterface = _rxConfProvider->createInstanceWithArguments(sAccessService, aCreationArgs);
411*cdf0e10cSrcweir 			OSL_ENSURE(xInterface.is(), "::createWithProvider: could not create the node access!");
412*cdf0e10cSrcweir 		}
413*cdf0e10cSrcweir 		catch(Exception&)
414*cdf0e10cSrcweir 		{
415*cdf0e10cSrcweir 			OSL_ENSURE(sal_False, "OConfigurationTreeRoot::createWithProvider: caught an exception while creating the access object!");
416*cdf0e10cSrcweir 		}
417*cdf0e10cSrcweir 	}
418*cdf0e10cSrcweir 	return xInterface;
419*cdf0e10cSrcweir }
420*cdf0e10cSrcweir // -----------------------------------------------------------------------------
421*cdf0e10cSrcweir Reference<XInterface> OPoolCollection::openNode(const ::rtl::OUString& _rPath,const Reference<XInterface>& _xTreeNode) const throw()
422*cdf0e10cSrcweir {
423*cdf0e10cSrcweir 	Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
424*cdf0e10cSrcweir 	Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
425*cdf0e10cSrcweir 	Reference< XInterface > xNode;
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir 	try
428*cdf0e10cSrcweir 	{
429*cdf0e10cSrcweir 		if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath))
430*cdf0e10cSrcweir 		{
431*cdf0e10cSrcweir 			if (!::cppu::extractInterface(xNode, xDirectAccess->getByName(_rPath)))
432*cdf0e10cSrcweir 				OSL_ENSURE(sal_False, "OConfigurationNode::openNode: could not open the node!");
433*cdf0e10cSrcweir 		}
434*cdf0e10cSrcweir 		else if (xHierarchyAccess.is())
435*cdf0e10cSrcweir 		{
436*cdf0e10cSrcweir 			if (!::cppu::extractInterface(xNode, xHierarchyAccess->getByHierarchicalName(_rPath)))
437*cdf0e10cSrcweir 				OSL_ENSURE(sal_False, "OConfigurationNode::openNode: could not open the node!");
438*cdf0e10cSrcweir 		}
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir 	}
441*cdf0e10cSrcweir 	catch(const NoSuchElementException&)
442*cdf0e10cSrcweir 	{
443*cdf0e10cSrcweir 		OSL_ENSURE(sal_False,
444*cdf0e10cSrcweir 					::rtl::OString("::openNode: there is no element named ")
445*cdf0e10cSrcweir 				+=	::rtl::OString(_rPath.getStr(), _rPath.getLength(), RTL_TEXTENCODING_ASCII_US)
446*cdf0e10cSrcweir 				+=	::rtl::OString("!"));
447*cdf0e10cSrcweir 	}
448*cdf0e10cSrcweir 	catch(Exception&)
449*cdf0e10cSrcweir 	{
450*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "OConfigurationNode::openNode: caught an exception while retrieving the node!");
451*cdf0e10cSrcweir 	}
452*cdf0e10cSrcweir 	return xNode;
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir // -----------------------------------------------------------------------------
455*cdf0e10cSrcweir Any OPoolCollection::getNodeValue(const ::rtl::OUString& _rPath,const Reference<XInterface>& _xTreeNode) throw()
456*cdf0e10cSrcweir {
457*cdf0e10cSrcweir 	Reference< XHierarchicalNameAccess > xHierarchyAccess(_xTreeNode, UNO_QUERY);
458*cdf0e10cSrcweir 	Reference< XNameAccess > xDirectAccess(_xTreeNode, UNO_QUERY);
459*cdf0e10cSrcweir 	Any aReturn;
460*cdf0e10cSrcweir 	try
461*cdf0e10cSrcweir 	{
462*cdf0e10cSrcweir 		if (xDirectAccess.is() && xDirectAccess->hasByName(_rPath) )
463*cdf0e10cSrcweir 		{
464*cdf0e10cSrcweir 			aReturn = xDirectAccess->getByName(_rPath);
465*cdf0e10cSrcweir         }
466*cdf0e10cSrcweir 		else if (xHierarchyAccess.is())
467*cdf0e10cSrcweir 		{
468*cdf0e10cSrcweir 			aReturn = xHierarchyAccess->getByHierarchicalName(_rPath);
469*cdf0e10cSrcweir 		}
470*cdf0e10cSrcweir 	}
471*cdf0e10cSrcweir 	catch(NoSuchElementException& e)
472*cdf0e10cSrcweir 	{
473*cdf0e10cSrcweir 		OSL_UNUSED( e );    // make compiler happy
474*cdf0e10cSrcweir 		OSL_ENSURE(sal_False,
475*cdf0e10cSrcweir 			::rtl::OString("::getNodeValue: caught a NoSuchElementException while trying to open ")
476*cdf0e10cSrcweir 		+=	::rtl::OString(e.Message.getStr(), e.Message.getLength(), RTL_TEXTENCODING_ASCII_US)
477*cdf0e10cSrcweir 		+=	::rtl::OString("!"));
478*cdf0e10cSrcweir 	}
479*cdf0e10cSrcweir 	return aReturn;
480*cdf0e10cSrcweir }
481*cdf0e10cSrcweir // -----------------------------------------------------------------------------
482*cdf0e10cSrcweir void SAL_CALL OPoolCollection::queryTermination( const EventObject& /*Event*/ ) throw (::com::sun::star::frame::TerminationVetoException, RuntimeException)
483*cdf0e10cSrcweir {
484*cdf0e10cSrcweir }
485*cdf0e10cSrcweir // -----------------------------------------------------------------------------
486*cdf0e10cSrcweir void SAL_CALL OPoolCollection::notifyTermination( const EventObject& /*Event*/ ) throw (RuntimeException)
487*cdf0e10cSrcweir {
488*cdf0e10cSrcweir 	clearDesktop();
489*cdf0e10cSrcweir }
490*cdf0e10cSrcweir // -----------------------------------------------------------------------------
491*cdf0e10cSrcweir void SAL_CALL OPoolCollection::disposing( const EventObject& Source ) throw (RuntimeException)
492*cdf0e10cSrcweir {
493*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
494*cdf0e10cSrcweir 	if ( m_xDesktop == Source.Source )
495*cdf0e10cSrcweir 	{
496*cdf0e10cSrcweir 		clearDesktop();
497*cdf0e10cSrcweir 	}
498*cdf0e10cSrcweir 	else
499*cdf0e10cSrcweir 	{
500*cdf0e10cSrcweir 		try
501*cdf0e10cSrcweir 		{
502*cdf0e10cSrcweir 			Reference<XPropertySet> xProp(Source.Source,UNO_QUERY);
503*cdf0e10cSrcweir 			if(Source.Source == m_xConfigNode)
504*cdf0e10cSrcweir 			{
505*cdf0e10cSrcweir 				if ( xProp.is() )
506*cdf0e10cSrcweir 					xProp->removePropertyChangeListener(getEnablePoolingNodeName(),this);
507*cdf0e10cSrcweir 			m_xConfigNode.clear();
508*cdf0e10cSrcweir 			}
509*cdf0e10cSrcweir 			else if ( xProp.is() )
510*cdf0e10cSrcweir 				xProp->removePropertyChangeListener(getEnableNodeName(),this);
511*cdf0e10cSrcweir 		}
512*cdf0e10cSrcweir 		catch(const Exception&)
513*cdf0e10cSrcweir 		{
514*cdf0e10cSrcweir 			OSL_ENSURE(0,"Exception caught");
515*cdf0e10cSrcweir 		}
516*cdf0e10cSrcweir 	}
517*cdf0e10cSrcweir }
518*cdf0e10cSrcweir // -----------------------------------------------------------------------------
519*cdf0e10cSrcweir void SAL_CALL OPoolCollection::propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (RuntimeException)
520*cdf0e10cSrcweir {
521*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
522*cdf0e10cSrcweir 	if(evt.Source == m_xConfigNode)
523*cdf0e10cSrcweir 	{
524*cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True;
525*cdf0e10cSrcweir 		evt.NewValue >>= bEnabled;
526*cdf0e10cSrcweir 		if(!bEnabled )
527*cdf0e10cSrcweir 		{
528*cdf0e10cSrcweir 			m_aDriverProxies.clear();
529*cdf0e10cSrcweir 			m_aDriverProxies = MapDriver2DriverRef();
530*cdf0e10cSrcweir 			OConnectionPools::iterator aIter = m_aPools.begin();
531*cdf0e10cSrcweir 			for(;aIter != m_aPools.end();++aIter)
532*cdf0e10cSrcweir 			{
533*cdf0e10cSrcweir 				aIter->second->clear(sal_False);
534*cdf0e10cSrcweir 				aIter->second->release();
535*cdf0e10cSrcweir 			}
536*cdf0e10cSrcweir 			m_aPools.clear();
537*cdf0e10cSrcweir 			m_aPools		 = OConnectionPools();
538*cdf0e10cSrcweir 		}
539*cdf0e10cSrcweir 	}
540*cdf0e10cSrcweir 	else if(evt.Source.is())
541*cdf0e10cSrcweir 	{
542*cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True;
543*cdf0e10cSrcweir 		evt.NewValue >>= bEnabled;
544*cdf0e10cSrcweir 		if(!bEnabled)
545*cdf0e10cSrcweir 		{
546*cdf0e10cSrcweir 			::rtl::OUString sThisDriverName;
547*cdf0e10cSrcweir 			getNodeValue(getDriverNameNodeName(),evt.Source) >>= sThisDriverName;
548*cdf0e10cSrcweir 			// 1nd relase the driver
549*cdf0e10cSrcweir 			// look if we already have a proxy for this driver
550*cdf0e10cSrcweir 			MapDriver2DriverRefIterator aLookup = m_aDriverProxies.begin();
551*cdf0e10cSrcweir 			while(	aLookup != m_aDriverProxies.end())
552*cdf0e10cSrcweir 			{
553*cdf0e10cSrcweir 				MapDriver2DriverRefIterator aFind = aLookup;
554*cdf0e10cSrcweir 				Reference<XServiceInfo> xInfo(aLookup->first,UNO_QUERY);
555*cdf0e10cSrcweir 				++aLookup;
556*cdf0e10cSrcweir 				if(xInfo.is() && xInfo->getImplementationName() == sThisDriverName)
557*cdf0e10cSrcweir 					m_aDriverProxies.erase(aFind);
558*cdf0e10cSrcweir 			}
559*cdf0e10cSrcweir 
560*cdf0e10cSrcweir 			// 2nd clear the connectionpool
561*cdf0e10cSrcweir 			OConnectionPools::iterator aFind = m_aPools.find(sThisDriverName);
562*cdf0e10cSrcweir 			if(aFind != m_aPools.end() && aFind->second)
563*cdf0e10cSrcweir 			{
564*cdf0e10cSrcweir 				aFind->second->clear(sal_False);
565*cdf0e10cSrcweir 				aFind->second->release();
566*cdf0e10cSrcweir 				m_aPools.erase(aFind);
567*cdf0e10cSrcweir 			}
568*cdf0e10cSrcweir 		}
569*cdf0e10cSrcweir 	}
570*cdf0e10cSrcweir }
571*cdf0e10cSrcweir // -----------------------------------------------------------------------------
572*cdf0e10cSrcweir void OPoolCollection::clearDesktop()
573*cdf0e10cSrcweir {
574*cdf0e10cSrcweir 	clearConnectionPools(sal_True);
575*cdf0e10cSrcweir 	if ( m_xDesktop.is() )
576*cdf0e10cSrcweir 		m_xDesktop->removeTerminateListener(this);
577*cdf0e10cSrcweir m_xDesktop.clear();
578*cdf0e10cSrcweir }
579*cdf0e10cSrcweir // -----------------------------------------------------------------------------
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir 
582