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