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