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