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