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