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 :
21 : #include <stdio.h>
22 :
23 : #include "mdrivermanager.hxx"
24 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 : #include <com/sun/star/sdbc/XDriver.hpp>
26 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
27 : #include <com/sun/star/container/ElementExistException.hpp>
28 : #include <com/sun/star/beans/NamedValue.hpp>
29 :
30 : #include <tools/diagnose_ex.h>
31 : #include <comphelper/extract.hxx>
32 : #include <comphelper/stl_types.hxx>
33 : #include <cppuhelper/implbase1.hxx>
34 : #include <cppuhelper/weakref.hxx>
35 : #include <osl/diagnose.h>
36 :
37 : #include <algorithm>
38 : #include <iterator>
39 :
40 : #include <o3tl/compat_functional.hxx>
41 :
42 : namespace drivermanager
43 : {
44 :
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::lang;
47 : using namespace ::com::sun::star::sdbc;
48 : using namespace ::com::sun::star::beans;
49 : using namespace ::com::sun::star::container;
50 : using namespace ::com::sun::star::logging;
51 : using namespace ::osl;
52 :
53 : #define SERVICE_SDBC_DRIVER ::rtl::OUString("com.sun.star.sdbc.Driver")
54 :
55 0 : void throwNoSuchElementException() throw(NoSuchElementException)
56 : {
57 0 : throw NoSuchElementException();
58 : }
59 :
60 : //==========================================================================
61 : //= ODriverEnumeration
62 : //==========================================================================
63 : class ODriverEnumeration : public ::cppu::WeakImplHelper1< XEnumeration >
64 : {
65 : friend class OSDBCDriverManager;
66 :
67 : DECLARE_STL_VECTOR( SdbcDriver, DriverArray );
68 : DriverArray m_aDrivers;
69 : ConstDriverArrayIterator m_aPos;
70 : // order matters!
71 :
72 : protected:
73 : virtual ~ODriverEnumeration();
74 : public:
75 : ODriverEnumeration(const DriverArray& _rDriverSequence);
76 :
77 : // XEnumeration
78 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw(RuntimeException);
79 : virtual Any SAL_CALL nextElement( ) throw(NoSuchElementException, WrappedTargetException, RuntimeException);
80 : };
81 :
82 : //--------------------------------------------------------------------------
83 0 : ODriverEnumeration::ODriverEnumeration(const DriverArray& _rDriverSequence)
84 : :m_aDrivers( _rDriverSequence )
85 0 : ,m_aPos( m_aDrivers.begin() )
86 : {
87 0 : }
88 :
89 : //--------------------------------------------------------------------------
90 0 : ODriverEnumeration::~ODriverEnumeration()
91 : {
92 0 : }
93 :
94 : //--------------------------------------------------------------------------
95 0 : sal_Bool SAL_CALL ODriverEnumeration::hasMoreElements( ) throw(RuntimeException)
96 : {
97 0 : return m_aPos != m_aDrivers.end();
98 : }
99 :
100 : //--------------------------------------------------------------------------
101 0 : Any SAL_CALL ODriverEnumeration::nextElement( ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
102 : {
103 0 : if ( !hasMoreElements() )
104 0 : throwNoSuchElementException();
105 :
106 0 : return makeAny( *m_aPos++ );
107 : }
108 :
109 : //=====================================================================
110 : //= helper
111 : //=====================================================================
112 :
113 : /// an STL functor which ensures that a SdbcDriver described by a DriverAccess is loaded
114 : struct EnsureDriver : public ::std::unary_function< DriverAccess, DriverAccess >
115 : {
116 0 : const DriverAccess& operator()( const DriverAccess& _rDescriptor ) const
117 : {
118 0 : if ( !_rDescriptor.xDriver.is() )
119 : // we did not load this driver, yet
120 0 : if ( _rDescriptor.xComponentFactory.is() )
121 : // we have a factory for it
122 0 : const_cast< DriverAccess& >( _rDescriptor ).xDriver = _rDescriptor.xDriver.query( _rDescriptor.xComponentFactory->createInstance() );
123 0 : return _rDescriptor;
124 : }
125 : };
126 :
127 : /// an STL functor which extracts a SdbcDriver from a DriverAccess
128 : struct ExtractDriverFromAccess : public ::std::unary_function< DriverAccess, SdbcDriver >
129 : {
130 0 : SdbcDriver operator()( const DriverAccess& _rAccess ) const
131 : {
132 0 : return _rAccess.xDriver;
133 : }
134 : };
135 :
136 : typedef ::o3tl::unary_compose< ExtractDriverFromAccess, EnsureDriver > ExtractAfterLoad_BASE;
137 : /// an STL functor which loads a driver described by a DriverAccess, and extracts the SdbcDriver
138 : struct ExtractAfterLoad : public ExtractAfterLoad_BASE
139 : {
140 0 : ExtractAfterLoad() : ExtractAfterLoad_BASE( ExtractDriverFromAccess(), EnsureDriver() ) { }
141 : };
142 :
143 : struct ExtractDriverFromCollectionElement : public ::std::unary_function< DriverCollection::value_type, SdbcDriver >
144 : {
145 0 : SdbcDriver operator()( const DriverCollection::value_type& _rElement ) const
146 : {
147 0 : return _rElement.second;
148 : }
149 : };
150 :
151 : // predicate for checking whether or not a driver accepts a given URL
152 : class AcceptsURL : public ::std::unary_function< SdbcDriver, bool >
153 : {
154 : protected:
155 : const ::rtl::OUString& m_rURL;
156 :
157 : public:
158 : // ctor
159 0 : AcceptsURL( const ::rtl::OUString& _rURL ) : m_rURL( _rURL ) { }
160 :
161 : //.................................................................
162 0 : bool operator()( const SdbcDriver& _rDriver ) const
163 : {
164 : // ask the driver
165 0 : if ( _rDriver.is() && _rDriver->acceptsURL( m_rURL ) )
166 0 : return true;
167 :
168 : // does not accept ...
169 0 : return false;
170 : }
171 : };
172 :
173 0 : static sal_Int32 lcl_getDriverPrecedence( const ::comphelper::ComponentContext& _rContext, Sequence< ::rtl::OUString >& _rPrecedence )
174 : {
175 0 : _rPrecedence.realloc( 0 );
176 : try
177 : {
178 : // some strings we need
179 0 : const ::rtl::OUString sDriverManagerConfigLocation( "org.openoffice.Office.DataAccess/DriverManager" );
180 0 : const ::rtl::OUString sDriverPreferenceLocation( "DriverPrecedence" );
181 0 : const ::rtl::OUString sNodePathArgumentName( "nodepath" );
182 0 : const ::rtl::OUString sNodeAccessServiceName( "com.sun.star.configuration.ConfigurationAccess" );
183 :
184 : // create a configuration provider
185 : Reference< XMultiServiceFactory > xConfigurationProvider(
186 : com::sun::star::configuration::theDefaultProvider::get(
187 0 : _rContext.getUNOContext() ) );
188 :
189 : // one argument for creating the node access: the path to the configuration node
190 0 : Sequence< Any > aCreationArgs(1);
191 0 : aCreationArgs[0] <<= NamedValue( sNodePathArgumentName, makeAny( sDriverManagerConfigLocation ) );
192 :
193 : // create the node access
194 0 : Reference< XNameAccess > xDriverManagerNode(xConfigurationProvider->createInstanceWithArguments(sNodeAccessServiceName, aCreationArgs), UNO_QUERY);
195 :
196 : OSL_ENSURE(xDriverManagerNode.is(), "lcl_getDriverPrecedence: could not open my configuration node!");
197 0 : if (xDriverManagerNode.is())
198 : {
199 : // obtain the preference list
200 0 : Any aPreferences = xDriverManagerNode->getByName(sDriverPreferenceLocation);
201 : #if OSL_DEBUG_LEVEL > 0
202 : sal_Bool bSuccess =
203 : #endif
204 0 : aPreferences >>= _rPrecedence;
205 0 : OSL_ENSURE(bSuccess || !aPreferences.hasValue(), "lcl_getDriverPrecedence: invalid value for the preferences node (no string sequence but not NULL)!");
206 0 : }
207 : }
208 0 : catch( const Exception& )
209 : {
210 : DBG_UNHANDLED_EXCEPTION();
211 : }
212 :
213 0 : return _rPrecedence.getLength();
214 : }
215 :
216 : /// an STL argorithm compatible predicate comparing two DriverAccess instances by their implementation names
217 : struct CompareDriverAccessByName : public ::std::binary_function< DriverAccess, DriverAccess, bool >
218 : {
219 : //.................................................................
220 0 : bool operator()( const DriverAccess& lhs, const DriverAccess& rhs )
221 : {
222 0 : return lhs.sImplementationName < rhs.sImplementationName ? true : false;
223 : }
224 : };
225 :
226 : /// and STL argorithm compatible predicate comparing a DriverAccess' impl name to a string
227 0 : struct EqualDriverAccessToName : public ::std::binary_function< DriverAccess, ::rtl::OUString, bool >
228 : {
229 : ::rtl::OUString m_sImplName;
230 0 : EqualDriverAccessToName(const ::rtl::OUString& _sImplName) : m_sImplName(_sImplName){}
231 : //.................................................................
232 0 : bool operator()( const DriverAccess& lhs)
233 : {
234 0 : return lhs.sImplementationName.equals(m_sImplName);
235 : }
236 : };
237 :
238 : //==========================================================================
239 : //= OSDBCDriverManager
240 : //==========================================================================
241 : //--------------------------------------------------------------------------
242 0 : OSDBCDriverManager::OSDBCDriverManager( const Reference< XComponentContext >& _rxContext )
243 : :m_aContext( _rxContext )
244 : ,m_aEventLogger( _rxContext, "org.openoffice.logging.sdbc.DriverManager" )
245 : ,m_aDriverConfig(m_aContext.getLegacyServiceFactory())
246 0 : ,m_nLoginTimeout(0)
247 : {
248 : // bootstrap all objects supporting the .sdb.Driver service
249 0 : bootstrapDrivers();
250 :
251 : // initialize the drivers order
252 0 : initializeDriverPrecedence();
253 0 : }
254 :
255 : //---------------------------------------------------------------------
256 0 : OSDBCDriverManager::~OSDBCDriverManager()
257 : {
258 0 : }
259 :
260 0 : void OSDBCDriverManager::bootstrapDrivers()
261 : {
262 0 : Reference< XContentEnumerationAccess > xEnumAccess( m_aContext.getLegacyServiceFactory(), UNO_QUERY );
263 0 : Reference< XEnumeration > xEnumDrivers;
264 0 : if (xEnumAccess.is())
265 0 : xEnumDrivers = xEnumAccess->createContentEnumeration(SERVICE_SDBC_DRIVER);
266 :
267 : OSL_ENSURE( xEnumDrivers.is(), "OSDBCDriverManager::bootstrapDrivers: no enumeration for the drivers available!" );
268 0 : if (xEnumDrivers.is())
269 : {
270 0 : Reference< XSingleServiceFactory > xFactory;
271 0 : Reference< XServiceInfo > xSI;
272 0 : while (xEnumDrivers->hasMoreElements())
273 : {
274 0 : ::cppu::extractInterface( xFactory, xEnumDrivers->nextElement() );
275 : OSL_ENSURE( xFactory.is(), "OSDBCDriverManager::bootstrapDrivers: no factory extracted" );
276 :
277 0 : if ( xFactory.is() )
278 : {
279 : // we got a factory for the driver
280 0 : DriverAccess aDriverDescriptor;
281 0 : sal_Bool bValidDescriptor = sal_False;
282 :
283 : // can it tell us something about the implementation name?
284 0 : xSI = xSI.query( xFactory );
285 0 : if ( xSI.is() )
286 : { // yes -> no need to load the driver immediately (load it later when needed)
287 0 : aDriverDescriptor.sImplementationName = xSI->getImplementationName();
288 0 : aDriverDescriptor.xComponentFactory = xFactory;
289 0 : bValidDescriptor = sal_True;
290 :
291 : m_aEventLogger.log( LogLevel::CONFIG,
292 : "found SDBC driver $1$, no need to load it",
293 : aDriverDescriptor.sImplementationName
294 0 : );
295 : }
296 : else
297 : {
298 : // no -> create the driver
299 0 : Reference< XDriver > xDriver( xFactory->createInstance(), UNO_QUERY );
300 : OSL_ENSURE( xDriver.is(), "OSDBCDriverManager::bootstrapDrivers: a driver which is no driver?!" );
301 :
302 0 : if ( xDriver.is() )
303 : {
304 0 : aDriverDescriptor.xDriver = xDriver;
305 : // and obtain it's implementation name
306 0 : xSI = xSI.query( xDriver );
307 : OSL_ENSURE( xSI.is(), "OSDBCDriverManager::bootstrapDrivers: a driver without service info?" );
308 0 : if ( xSI.is() )
309 : {
310 0 : aDriverDescriptor.sImplementationName = xSI->getImplementationName();
311 0 : bValidDescriptor = sal_True;
312 :
313 : m_aEventLogger.log( LogLevel::CONFIG,
314 : "found SDBC driver $1$, needed to load it",
315 : aDriverDescriptor.sImplementationName
316 0 : );
317 : }
318 0 : }
319 : }
320 :
321 0 : if ( bValidDescriptor )
322 : {
323 0 : m_aDriversBS.push_back( aDriverDescriptor );
324 0 : }
325 : }
326 0 : }
327 0 : }
328 0 : }
329 :
330 : //--------------------------------------------------------------------------
331 0 : void OSDBCDriverManager::initializeDriverPrecedence()
332 : {
333 0 : if ( m_aDriversBS.empty() )
334 : // nothing to do
335 0 : return;
336 :
337 : try
338 : {
339 : // get the precedence of the drivers from the configuration
340 0 : Sequence< ::rtl::OUString > aDriverOrder;
341 0 : if ( 0 == lcl_getDriverPrecedence( m_aContext, aDriverOrder ) )
342 : // nothing to do
343 : return;
344 :
345 : // aDriverOrder now is the list of driver implementation names in the order they should be used
346 :
347 0 : if ( m_aEventLogger.isLoggable( LogLevel::CONFIG ) )
348 : {
349 0 : sal_Int32 nOrderedCount = aDriverOrder.getLength();
350 0 : for ( sal_Int32 i=0; i<nOrderedCount; ++i )
351 : m_aEventLogger.log( LogLevel::CONFIG,
352 : "configuration's driver order: driver $1$ of $2$: $3$",
353 0 : (sal_Int32)(i + 1), nOrderedCount, aDriverOrder[i]
354 0 : );
355 : }
356 :
357 : // sort our bootstrapped drivers
358 0 : ::std::sort( m_aDriversBS.begin(), m_aDriversBS.end(), CompareDriverAccessByName() );
359 :
360 : // loop through the names in the precedence order
361 0 : const ::rtl::OUString* pDriverOrder = aDriverOrder.getConstArray();
362 0 : const ::rtl::OUString* pDriverOrderEnd = pDriverOrder + aDriverOrder.getLength();
363 :
364 : // the first driver for which there is no preference
365 0 : DriverAccessArrayIterator aNoPrefDriversStart = m_aDriversBS.begin();
366 : // at the moment this is the first of all drivers we know
367 :
368 0 : for ( ; ( pDriverOrder < pDriverOrderEnd ) && ( aNoPrefDriversStart != m_aDriversBS.end() ); ++pDriverOrder )
369 : {
370 0 : DriverAccess driver_order;
371 0 : driver_order.sImplementationName = *pDriverOrder;
372 :
373 : // look for the impl name in the DriverAccess array
374 : ::std::pair< DriverAccessArrayIterator, DriverAccessArrayIterator > aPos =
375 0 : ::std::equal_range( aNoPrefDriversStart, m_aDriversBS.end(), driver_order, CompareDriverAccessByName() );
376 :
377 0 : if ( aPos.first != aPos.second )
378 : { // we have a DriverAccess with this impl name
379 :
380 : OSL_ENSURE( ::std::distance( aPos.first, aPos.second ) == 1,
381 : "OSDBCDriverManager::initializeDriverPrecedence: more than one driver with this impl name? How this?" );
382 : // move the DriverAccess pointed to by aPos.first to the position pointed to by aNoPrefDriversStart
383 :
384 0 : if ( aPos.first != aNoPrefDriversStart )
385 : { // if this does not hold, the DriverAccess alread has the correct position
386 :
387 : // rotate the range [aNoPrefDriversStart, aPos.second) right 1 element
388 0 : ::std::rotate( aNoPrefDriversStart, aPos.second - 1, aPos.second );
389 : }
390 :
391 : // next round we start searching and pos right
392 0 : ++aNoPrefDriversStart;
393 : }
394 0 : }
395 : }
396 0 : catch (Exception&)
397 : {
398 : OSL_FAIL("OSDBCDriverManager::initializeDriverPrecedence: caught an exception while sorting the drivers!");
399 : }
400 : }
401 :
402 : //--------------------------------------------------------------------------
403 0 : Reference< XConnection > SAL_CALL OSDBCDriverManager::getConnection( const ::rtl::OUString& _rURL ) throw(SQLException, RuntimeException)
404 : {
405 0 : MutexGuard aGuard(m_aMutex);
406 :
407 : m_aEventLogger.log( LogLevel::INFO,
408 : "connection requested for URL $1$",
409 : _rURL
410 0 : );
411 :
412 0 : Reference< XConnection > xConnection;
413 0 : Reference< XDriver > xDriver = implGetDriverForURL(_rURL);
414 0 : if (xDriver.is())
415 : {
416 : // TODO : handle the login timeout
417 0 : xConnection = xDriver->connect(_rURL, Sequence< PropertyValue >());
418 : // may throw an exception
419 : m_aEventLogger.log( LogLevel::INFO,
420 : "connection retrieved for URL $1$",
421 : _rURL
422 0 : );
423 : }
424 :
425 0 : return xConnection;
426 : }
427 :
428 : //--------------------------------------------------------------------------
429 0 : Reference< XConnection > SAL_CALL OSDBCDriverManager::getConnectionWithInfo( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException)
430 : {
431 0 : MutexGuard aGuard(m_aMutex);
432 :
433 : m_aEventLogger.log( LogLevel::INFO,
434 : "connection with info requested for URL $1$",
435 : _rURL
436 0 : );
437 :
438 0 : Reference< XConnection > xConnection;
439 0 : Reference< XDriver > xDriver = implGetDriverForURL(_rURL);
440 0 : if (xDriver.is())
441 : {
442 : // TODO : handle the login timeout
443 0 : xConnection = xDriver->connect(_rURL, _rInfo);
444 : // may throw an exception
445 : m_aEventLogger.log( LogLevel::INFO,
446 : "connection with info retrieved for URL $1$",
447 : _rURL
448 0 : );
449 : }
450 :
451 0 : return xConnection;
452 : }
453 :
454 : //--------------------------------------------------------------------------
455 0 : void SAL_CALL OSDBCDriverManager::setLoginTimeout( sal_Int32 seconds ) throw(RuntimeException)
456 : {
457 0 : MutexGuard aGuard(m_aMutex);
458 0 : m_nLoginTimeout = seconds;
459 0 : }
460 :
461 : //--------------------------------------------------------------------------
462 0 : sal_Int32 SAL_CALL OSDBCDriverManager::getLoginTimeout( ) throw(RuntimeException)
463 : {
464 0 : MutexGuard aGuard(m_aMutex);
465 0 : return m_nLoginTimeout;
466 : }
467 :
468 : //--------------------------------------------------------------------------
469 0 : Reference< XEnumeration > SAL_CALL OSDBCDriverManager::createEnumeration( ) throw(RuntimeException)
470 : {
471 0 : MutexGuard aGuard(m_aMutex);
472 :
473 0 : ODriverEnumeration::DriverArray aDrivers;
474 :
475 : // ensure that all our bootstrapped drivers are instantiated
476 0 : ::std::for_each( m_aDriversBS.begin(), m_aDriversBS.end(), EnsureDriver() );
477 :
478 : // copy the bootstrapped drivers
479 : ::std::transform(
480 : m_aDriversBS.begin(), // "copy from" start
481 : m_aDriversBS.end(), // "copy from" end
482 : ::std::back_inserter( aDrivers ), // insert into
483 : ExtractDriverFromAccess() // transformation to apply (extract a driver from a driver access)
484 0 : );
485 :
486 : // append the runtime drivers
487 : ::std::transform(
488 : m_aDriversRT.begin(), // "copy from" start
489 : m_aDriversRT.end(), // "copy from" end
490 : ::std::back_inserter( aDrivers ), // insert into
491 : ExtractDriverFromCollectionElement() // transformation to apply (extract a driver from a driver access)
492 0 : );
493 :
494 0 : return new ODriverEnumeration( aDrivers );
495 : }
496 :
497 : //--------------------------------------------------------------------------
498 0 : ::com::sun::star::uno::Type SAL_CALL OSDBCDriverManager::getElementType( ) throw(::com::sun::star::uno::RuntimeException)
499 : {
500 0 : return ::getCppuType(static_cast< Reference< XDriver >* >(NULL));
501 : }
502 :
503 : //--------------------------------------------------------------------------
504 0 : sal_Bool SAL_CALL OSDBCDriverManager::hasElements( ) throw(::com::sun::star::uno::RuntimeException)
505 : {
506 0 : MutexGuard aGuard(m_aMutex);
507 0 : return !(m_aDriversBS.empty() && m_aDriversRT.empty());
508 : }
509 :
510 : //--------------------------------------------------------------------------
511 0 : ::rtl::OUString SAL_CALL OSDBCDriverManager::getImplementationName( ) throw(RuntimeException)
512 : {
513 0 : return getImplementationName_static();
514 : }
515 :
516 : //--------------------------------------------------------------------------
517 0 : sal_Bool SAL_CALL OSDBCDriverManager::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
518 : {
519 0 : Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
520 0 : const ::rtl::OUString* pSupported = aSupported.getConstArray();
521 0 : const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
522 0 : for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
523 : ;
524 :
525 0 : return pSupported != pEnd;
526 : }
527 :
528 : //--------------------------------------------------------------------------
529 0 : Sequence< ::rtl::OUString > SAL_CALL OSDBCDriverManager::getSupportedServiceNames( ) throw(RuntimeException)
530 : {
531 0 : return getSupportedServiceNames_static();
532 : }
533 :
534 : //--------------------------------------------------------------------------
535 0 : Reference< XInterface > SAL_CALL OSDBCDriverManager::Create( const Reference< XMultiServiceFactory >& _rxFactory )
536 : {
537 0 : ::comphelper::ComponentContext aContext( _rxFactory );
538 0 : return *( new OSDBCDriverManager( aContext.getUNOContext() ) );
539 : }
540 :
541 : //--------------------------------------------------------------------------
542 0 : ::rtl::OUString SAL_CALL OSDBCDriverManager::getImplementationName_static( ) throw(RuntimeException)
543 : {
544 0 : return ::rtl::OUString("com.sun.star.comp.sdbc.OSDBCDriverManager");
545 : }
546 :
547 : //--------------------------------------------------------------------------
548 0 : Sequence< ::rtl::OUString > SAL_CALL OSDBCDriverManager::getSupportedServiceNames_static( ) throw(RuntimeException)
549 : {
550 0 : Sequence< ::rtl::OUString > aSupported(1);
551 0 : aSupported[0] = getSingletonName_static();
552 0 : return aSupported;
553 : }
554 :
555 : //--------------------------------------------------------------------------
556 0 : ::rtl::OUString SAL_CALL OSDBCDriverManager::getSingletonName_static( ) throw(RuntimeException)
557 : {
558 0 : return ::rtl::OUString( "com.sun.star.sdbc.DriverManager" );
559 : }
560 :
561 : //--------------------------------------------------------------------------
562 0 : Reference< XInterface > SAL_CALL OSDBCDriverManager::getRegisteredObject( const ::rtl::OUString& _rName ) throw(Exception, RuntimeException)
563 : {
564 0 : MutexGuard aGuard(m_aMutex);
565 0 : ConstDriverCollectionIterator aSearch = m_aDriversRT.find(_rName);
566 0 : if (aSearch == m_aDriversRT.end())
567 0 : throwNoSuchElementException();
568 :
569 0 : return aSearch->second.get();
570 : }
571 :
572 : //--------------------------------------------------------------------------
573 0 : void SAL_CALL OSDBCDriverManager::registerObject( const ::rtl::OUString& _rName, const Reference< XInterface >& _rxObject ) throw(Exception, RuntimeException)
574 : {
575 0 : MutexGuard aGuard(m_aMutex);
576 :
577 : m_aEventLogger.log( LogLevel::INFO,
578 : "attempt to register new driver for name $1$",
579 : _rName
580 0 : );
581 :
582 0 : ConstDriverCollectionIterator aSearch = m_aDriversRT.find(_rName);
583 0 : if (aSearch == m_aDriversRT.end())
584 : {
585 0 : Reference< XDriver > xNewDriver(_rxObject, UNO_QUERY);
586 0 : if (xNewDriver.is())
587 0 : m_aDriversRT.insert(DriverCollection::value_type(_rName, xNewDriver));
588 : else
589 0 : throw IllegalArgumentException();
590 : }
591 : else
592 0 : throw ElementExistException();
593 :
594 : m_aEventLogger.log( LogLevel::INFO,
595 : "new driver registered for name $1$",
596 : _rName
597 0 : );
598 0 : }
599 :
600 : //--------------------------------------------------------------------------
601 0 : void SAL_CALL OSDBCDriverManager::revokeObject( const ::rtl::OUString& _rName ) throw(Exception, RuntimeException)
602 : {
603 0 : MutexGuard aGuard(m_aMutex);
604 :
605 : m_aEventLogger.log( LogLevel::INFO,
606 : "attempt to revoke driver for name $1$",
607 : _rName
608 0 : );
609 :
610 0 : DriverCollectionIterator aSearch = m_aDriversRT.find(_rName);
611 0 : if (aSearch == m_aDriversRT.end())
612 0 : throwNoSuchElementException();
613 :
614 0 : m_aDriversRT.erase(aSearch); // we already have the iterator so we could use it
615 :
616 : m_aEventLogger.log( LogLevel::INFO,
617 : "driver revoked for name $1$",
618 : _rName
619 0 : );
620 0 : }
621 :
622 : //--------------------------------------------------------------------------
623 0 : Reference< XDriver > SAL_CALL OSDBCDriverManager::getDriverByURL( const ::rtl::OUString& _rURL ) throw(RuntimeException)
624 : {
625 : m_aEventLogger.log( LogLevel::INFO,
626 : "driver requested for URL $1$",
627 : _rURL
628 0 : );
629 :
630 0 : Reference< XDriver > xDriver( implGetDriverForURL( _rURL ) );
631 :
632 0 : if ( xDriver.is() )
633 : m_aEventLogger.log( LogLevel::INFO,
634 : "driver obtained for URL $1$",
635 : _rURL
636 0 : );
637 :
638 0 : return xDriver;
639 : }
640 :
641 : //--------------------------------------------------------------------------
642 0 : Reference< XDriver > OSDBCDriverManager::implGetDriverForURL(const ::rtl::OUString& _rURL)
643 : {
644 0 : Reference< XDriver > xReturn;
645 :
646 : {
647 0 : const ::rtl::OUString sDriverFactoryName = m_aDriverConfig.getDriverFactoryName(_rURL);
648 :
649 0 : EqualDriverAccessToName aEqual(sDriverFactoryName);
650 0 : DriverAccessArray::iterator aFind = ::std::find_if(m_aDriversBS.begin(),m_aDriversBS.end(),aEqual);
651 0 : if ( aFind == m_aDriversBS.end() )
652 : {
653 : // search all bootstrapped drivers
654 : aFind = ::std::find_if(
655 : m_aDriversBS.begin(), // begin of search range
656 : m_aDriversBS.end(), // end of search range
657 : o3tl::unary_compose< AcceptsURL, ExtractAfterLoad >( AcceptsURL( _rURL ), ExtractAfterLoad() )
658 : // compose two functors: extract the driver from the access, then ask the resulting driver for acceptance
659 0 : );
660 : } // if ( m_aDriversBS.find(sDriverFactoryName ) == m_aDriversBS.end() )
661 : else
662 : {
663 : EnsureDriver aEnsure;
664 0 : aEnsure(*aFind);
665 : }
666 :
667 : // found something?
668 0 : if ( m_aDriversBS.end() != aFind && aFind->xDriver.is() && aFind->xDriver->acceptsURL(_rURL) )
669 0 : xReturn = aFind->xDriver;
670 : }
671 :
672 0 : if ( !xReturn.is() )
673 : {
674 : // no -> search the runtime drivers
675 : DriverCollectionIterator aPos = ::std::find_if(
676 : m_aDriversRT.begin(), // begin of search range
677 : m_aDriversRT.end(), // end of search range
678 : o3tl::unary_compose< AcceptsURL, ExtractDriverFromCollectionElement >( AcceptsURL( _rURL ), ExtractDriverFromCollectionElement() )
679 : // compose two functors: extract the driver from the access, then ask the resulting driver for acceptance
680 0 : );
681 :
682 0 : if ( m_aDriversRT.end() != aPos )
683 0 : xReturn = aPos->second;
684 : }
685 :
686 0 : return xReturn;
687 : }
688 :
689 : } // namespace drivermanager
690 :
691 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|