1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26
27 #include "apitools.hxx"
28 #include "core_resource.hrc"
29 #include "core_resource.hxx"
30 #include "databasecontext.hxx"
31 #include "databasedocument.hxx"
32 #include "databaseregistrations.hxx"
33 #include "datasource.hxx"
34 #include "dbastrings.hrc"
35 #include "module_dba.hxx"
36
37 /** === being UNO includes === **/
38 #include <com/sun/star/beans/NamedValue.hpp>
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/document/MacroExecMode.hpp>
42 #include <com/sun/star/document/XFilter.hpp>
43 #include <com/sun/star/document/XImporter.hpp>
44 #include <com/sun/star/frame/XDesktop.hpp>
45 #include <com/sun/star/frame/XModel.hpp>
46 #include <com/sun/star/frame/XModel2.hpp>
47 #include <com/sun/star/frame/XTerminateListener.hpp>
48 #include <com/sun/star/lang/DisposedException.hpp>
49 #include <com/sun/star/registry/InvalidRegistryException.hpp>
50 #include <com/sun/star/sdbc/XDataSource.hpp>
51 #include <com/sun/star/task/InteractionClassification.hpp>
52 #include <com/sun/star/ucb/InteractiveIOException.hpp>
53 #include <com/sun/star/ucb/IOErrorCode.hpp>
54 #include <com/sun/star/util/XCloseable.hpp>
55 /** === end UNO includes === **/
56
57 #include <basic/basmgr.hxx>
58 #include <comphelper/enumhelper.hxx>
59 #include <comphelper/evtlistenerhlp.hxx>
60 #include <comphelper/namedvaluecollection.hxx>
61 #include <comphelper/processfactory.hxx>
62 #include <comphelper/sequence.hxx>
63 #include <cppuhelper/implbase1.hxx>
64 #include <cppuhelper/typeprovider.hxx>
65 #include <cppuhelper/exc_hlp.hxx>
66 #include <svl/filenotation.hxx>
67 #include <tools/debug.hxx>
68 #include <tools/diagnose_ex.h>
69 #include <tools/fsys.hxx>
70 #include <tools/urlobj.hxx>
71 #include <ucbhelper/content.hxx>
72 #include <unotools/confignode.hxx>
73 #include <unotools/pathoptions.hxx>
74 #include <unotools/sharedunocomponent.hxx>
75 #include <list>
76 #include <boost/bind.hpp>
77
78 using namespace ::com::sun::star::sdbc;
79 using namespace ::com::sun::star::sdb;
80 using namespace ::com::sun::star::beans;
81 using namespace ::com::sun::star::uno;
82 using namespace ::com::sun::star::document;
83 using namespace ::com::sun::star::frame;
84 using namespace ::com::sun::star::lang;
85 using namespace ::com::sun::star::container;
86 using namespace ::com::sun::star::util;
87 using namespace ::com::sun::star::registry;
88 using namespace ::com::sun::star;
89 using namespace ::cppu;
90 using namespace ::osl;
91 using namespace ::utl;
92
93 using ::com::sun::star::task::InteractionClassification_ERROR;
94 using ::com::sun::star::ucb::IOErrorCode_NO_FILE;
95 using ::com::sun::star::ucb::InteractiveIOException;
96 using ::com::sun::star::ucb::IOErrorCode_NOT_EXISTING;
97 using ::com::sun::star::ucb::IOErrorCode_NOT_EXISTING_PATH;
98
99 //==========================================================================
100
createRegistryInfo_ODatabaseContext()101 extern "C" void SAL_CALL createRegistryInfo_ODatabaseContext()
102 {
103 static ::dba::OLegacySingletonRegistration< ::dbaccess::ODatabaseContext > aODatabaseContext_AutoRegistration;
104 }
105
106 //........................................................................
107 namespace dbaccess
108 {
109 //........................................................................
110
111 // .............................................................................
112 typedef ::cppu::WeakImplHelper1 < XTerminateListener
113 > DatabaseDocumentLoader_Base;
114 class DatabaseDocumentLoader : public DatabaseDocumentLoader_Base
115 {
116 private:
117 Reference< XDesktop > m_xDesktop;
118 ::std::list< const ODatabaseModelImpl* > m_aDatabaseDocuments;
119
120 public:
121 DatabaseDocumentLoader( const comphelper::ComponentContext& _aContext);
122
append(const ODatabaseModelImpl & _rModelImpl)123 inline void append(const ODatabaseModelImpl& _rModelImpl )
124 {
125 m_aDatabaseDocuments.push_back(&_rModelImpl);
126 }
remove(const ODatabaseModelImpl & _rModelImpl)127 inline void remove(const ODatabaseModelImpl& _rModelImpl) { m_aDatabaseDocuments.remove(&_rModelImpl); }
128
129 private:
130 // XTerminateListener
131 virtual void SAL_CALL queryTermination( const lang::EventObject& Event );
132 virtual void SAL_CALL notifyTermination( const lang::EventObject& Event );
133 // XEventListener
134 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source );
135 };
136
137 // .............................................................................
DatabaseDocumentLoader(const comphelper::ComponentContext & _aContext)138 DatabaseDocumentLoader::DatabaseDocumentLoader( const comphelper::ComponentContext& _aContext )
139 {
140 acquire();
141 try
142 {
143 m_xDesktop.set( _aContext.createComponent( (rtl::OUString)SERVICE_FRAME_DESKTOP ), UNO_QUERY_THROW );
144 m_xDesktop->addTerminateListener( this );
145 }
146 catch( const Exception& )
147 {
148 DBG_UNHANDLED_EXCEPTION();
149 }
150 }
151
152 struct TerminateFunctor
153 {
154 typedef ODatabaseModelImpl* argument_type;
155 typedef void result_type;
operator ()dbaccess::TerminateFunctor156 void operator()( const ODatabaseModelImpl* _pModelImpl ) const
157 {
158 try
159 {
160 const Reference< XModel2> xModel( _pModelImpl ->getModel_noCreate(),UNO_QUERY_THROW );
161 if ( !xModel->getControllers()->hasMoreElements() )
162 {
163 Reference<util::XCloseable> xCloseable(xModel,UNO_QUERY_THROW);
164 xCloseable->close(sal_False);
165 } // if ( !xModel->getControllers()->hasMoreElements() )
166 }
167 catch(const CloseVetoException&)
168 {
169 throw TerminationVetoException();
170 }
171 }
172 };
173 // .............................................................................
queryTermination(const lang::EventObject &)174 void SAL_CALL DatabaseDocumentLoader::queryTermination( const lang::EventObject& /*Event*/ )
175 {
176 ::std::list< const ODatabaseModelImpl* > aCopy(m_aDatabaseDocuments);
177 ::std::for_each(aCopy.begin(),aCopy.end(),TerminateFunctor());
178 }
179
180 // .............................................................................
notifyTermination(const lang::EventObject &)181 void SAL_CALL DatabaseDocumentLoader::notifyTermination( const lang::EventObject& /*Event*/ )
182 {
183 }
184 // .............................................................................
disposing(const lang::EventObject &)185 void SAL_CALL DatabaseDocumentLoader::disposing( const lang::EventObject& /*Source*/ )
186 {
187 }
188
189 //= ODatabaseContext
190 //==========================================================================
191 //--------------------------------------------------------------------------
ODatabaseContext(const Reference<XComponentContext> & _rxContext)192 ODatabaseContext::ODatabaseContext( const Reference< XComponentContext >& _rxContext )
193 :DatabaseAccessContext_Base(m_aMutex)
194 ,m_aContext( _rxContext )
195 ,m_aContainerListeners(m_aMutex)
196 {
197 m_pDatabaseDocumentLoader = new DatabaseDocumentLoader( m_aContext );
198 ::basic::BasicManagerRepository::registerCreationListener( *this );
199
200 osl_incrementInterlockedCount( &m_refCount );
201 {
202 m_xDBRegistrationAggregate.set( createDataSourceRegistrations( m_aContext ), UNO_SET_THROW );
203 m_xDatabaseRegistrations.set( m_xDBRegistrationAggregate, UNO_QUERY_THROW );
204
205 m_xDBRegistrationAggregate->setDelegator( *this );
206 }
207 osl_decrementInterlockedCount( &m_refCount );
208 }
209
210 //--------------------------------------------------------------------------
~ODatabaseContext()211 ODatabaseContext::~ODatabaseContext()
212 {
213 ::basic::BasicManagerRepository::revokeCreationListener( *this );
214 if ( m_pDatabaseDocumentLoader )
215 m_pDatabaseDocumentLoader->release();
216
217 m_xDBRegistrationAggregate->setDelegator( NULL );
218 m_xDBRegistrationAggregate.clear();
219 m_xDatabaseRegistrations.clear();
220 }
221
222 // Helper
223 //------------------------------------------------------------------------------
getImplementationName_static()224 rtl::OUString ODatabaseContext::getImplementationName_static()
225
226 {
227 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.dba.ODatabaseContext"));
228 }
229
230 //------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)231 Reference< XInterface > ODatabaseContext::Create(const Reference< XComponentContext >& _rxContext)
232 {
233 return *( new ODatabaseContext( _rxContext ) );
234 }
235
236 //------------------------------------------------------------------------------
getSupportedServiceNames_static(void)237 Sequence< rtl::OUString > ODatabaseContext::getSupportedServiceNames_static(void)
238 {
239 Sequence< ::rtl::OUString > aSNS( 1 );
240 aSNS[0] = SERVICE_SDB_DATABASECONTEXT;
241 return aSNS;
242 }
243
244 // XServiceInfo
245 //------------------------------------------------------------------------------
getImplementationName()246 rtl::OUString ODatabaseContext::getImplementationName( )
247 {
248 return getImplementationName_static();
249 }
250
251 //------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)252 sal_Bool ODatabaseContext::supportsService( const ::rtl::OUString& _rServiceName )
253 {
254 return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
255 }
256
257 //------------------------------------------------------------------------------
getSupportedServiceNames()258 Sequence< ::rtl::OUString > ODatabaseContext::getSupportedServiceNames( )
259 {
260 return getSupportedServiceNames_static();
261 }
262
263 //--------------------------------------------------------------------------
impl_createNewDataSource()264 Reference< XInterface > ODatabaseContext::impl_createNewDataSource()
265 {
266 ::rtl::Reference<ODatabaseModelImpl> pImpl( new ODatabaseModelImpl( m_aContext.getLegacyServiceFactory(), *this ) );
267 Reference< XDataSource > xDataSource( pImpl->getOrCreateDataSource() );
268
269 return xDataSource.get();
270 }
271
272 //--------------------------------------------------------------------------
createInstance()273 Reference< XInterface > SAL_CALL ODatabaseContext::createInstance( )
274 {
275 // for convenience of the API user, we ensure the document is fully initialized (effectively: XLoadable::initNew
276 // has been called at the DatabaseDocument).
277 return impl_createNewDataSource();
278 }
279
280 //--------------------------------------------------------------------------
createInstanceWithArguments(const Sequence<Any> & _rArguments)281 Reference< XInterface > SAL_CALL ODatabaseContext::createInstanceWithArguments( const Sequence< Any >& _rArguments )
282 {
283 ::comphelper::NamedValueCollection aArgs( _rArguments );
284 ::rtl::OUString sURL = aArgs.getOrDefault( (::rtl::OUString)INFO_POOLURL, ::rtl::OUString() );
285
286 Reference< XInterface > xDataSource;
287 if ( sURL.getLength() )
288 xDataSource = getObject( sURL );
289
290 if ( !xDataSource.is() )
291 xDataSource = impl_createNewDataSource();
292
293 return xDataSource;
294 }
295 // DatabaseAccessContext_Base
296 //------------------------------------------------------------------------------
disposing()297 void ODatabaseContext::disposing()
298 {
299 // notify our listener
300 com::sun::star::lang::EventObject aDisposeEvent(static_cast< XContainer* >(this));
301 m_aContainerListeners.disposeAndClear(aDisposeEvent);
302
303 // dispose the data sources
304 ObjectCache::iterator aEnd = m_aDatabaseObjects.end();
305 for ( ObjectCache::iterator aIter = m_aDatabaseObjects.begin();
306 aIter != aEnd;
307 ++aIter
308 )
309 {
310 aIter->second->dispose();
311 }
312 m_aDatabaseObjects.clear();
313 }
314
315 // XNamingService
316 //------------------------------------------------------------------------------
getRegisteredObject(const rtl::OUString & _rName)317 Reference< XInterface > ODatabaseContext::getRegisteredObject(const rtl::OUString& _rName)
318 {
319 MutexGuard aGuard(m_aMutex);
320 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
321
322 ::rtl::OUString sURL( getDatabaseLocation( _rName ) );
323
324 if ( !sURL.getLength() )
325 // there is a registration for this name, but no URL
326 throw IllegalArgumentException();
327
328 // check if URL is already loaded
329 Reference< XInterface > xExistent = getObject( sURL );
330 if ( xExistent.is() )
331 return xExistent;
332
333 return loadObjectFromURL( _rName, sURL );
334 }
335 // -----------------------------------------------------------------------------
loadObjectFromURL(const::rtl::OUString & _rName,const::rtl::OUString & _sURL)336 Reference< XInterface > ODatabaseContext::loadObjectFromURL(const ::rtl::OUString& _rName,const ::rtl::OUString& _sURL)
337 {
338 INetURLObject aURL( _sURL );
339 if ( aURL.GetProtocol() == INET_PROT_NOT_VALID )
340 throw NoSuchElementException( _rName, *this );
341
342 try
343 {
344 ::ucbhelper::Content aContent( _sURL, NULL );
345 if ( !aContent.isDocument() )
346 throw InteractiveIOException(
347 _sURL, *this, InteractionClassification_ERROR, IOErrorCode_NO_FILE
348 );
349 }
350 catch ( const InteractiveIOException& e )
351 {
352 if ( ( e.Code == IOErrorCode_NO_FILE )
353 || ( e.Code == IOErrorCode_NOT_EXISTING )
354 || ( e.Code == IOErrorCode_NOT_EXISTING_PATH )
355 )
356 {
357 // #i40463# #i39187#
358 String sErrorMessage( DBACORE_RESSTRING( RID_STR_FILE_DOES_NOT_EXIST ) );
359 ::svt::OFileNotation aTransformer( _sURL );
360 sErrorMessage.SearchAndReplaceAscii( "$file$", aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
361
362 SQLException aError;
363 aError.Message = sErrorMessage;
364
365 throw WrappedTargetException( _sURL, *this, makeAny( aError ) );
366 }
367 throw WrappedTargetException( _sURL, *this, ::cppu::getCaughtException() );
368 }
369 catch( const Exception& )
370 {
371 throw WrappedTargetException( _sURL, *this, ::cppu::getCaughtException() );
372 }
373
374 OSL_ENSURE( m_aDatabaseObjects.find( _sURL ) == m_aDatabaseObjects.end(),
375 "ODatabaseContext::loadObjectFromURL: not intended for already-cached objects!" );
376
377 ::rtl::Reference< ODatabaseModelImpl > pModelImpl;
378 {
379 pModelImpl.set( new ODatabaseModelImpl( _rName, m_aContext.getLegacyServiceFactory(), *this ) );
380
381 Reference< XModel > xModel( pModelImpl->createNewModel_deliverOwnership( false ), UNO_SET_THROW );
382 Reference< XLoadable > xLoad( xModel, UNO_QUERY_THROW );
383
384 ::comphelper::NamedValueCollection aArgs;
385 aArgs.put( "URL", _sURL );
386 aArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
387 aArgs.put( "InteractionHandler", m_aContext.createComponent( "com.sun.star.task.InteractionHandler" ) );
388
389 Sequence< PropertyValue > aResource( aArgs.getPropertyValues() );
390 xLoad->load( aResource );
391 xModel->attachResource( _sURL, aResource );
392
393 ::utl::CloseableComponent aEnsureClose( xModel );
394 }
395
396 setTransientProperties( _sURL, *pModelImpl );
397
398 return pModelImpl->getOrCreateDataSource().get();
399 }
400 // -----------------------------------------------------------------------------
appendAtTerminateListener(const ODatabaseModelImpl & _rDataSourceModel)401 void ODatabaseContext::appendAtTerminateListener(const ODatabaseModelImpl& _rDataSourceModel)
402 {
403 m_pDatabaseDocumentLoader->append(_rDataSourceModel);
404 }
405 // -----------------------------------------------------------------------------
removeFromTerminateListener(const ODatabaseModelImpl & _rDataSourceModel)406 void ODatabaseContext::removeFromTerminateListener(const ODatabaseModelImpl& _rDataSourceModel)
407 {
408 m_pDatabaseDocumentLoader->remove(_rDataSourceModel);
409 }
410 // -----------------------------------------------------------------------------
setTransientProperties(const::rtl::OUString & _sURL,ODatabaseModelImpl & _rDataSourceModel)411 void ODatabaseContext::setTransientProperties(const ::rtl::OUString& _sURL, ODatabaseModelImpl& _rDataSourceModel )
412 {
413 if ( m_aDatasourceProperties.end() == m_aDatasourceProperties.find(_sURL) )
414 return;
415 try
416 {
417 ::rtl::OUString sAuthFailedPassword;
418 Reference< XPropertySet > xDSProps( _rDataSourceModel.getOrCreateDataSource(), UNO_QUERY_THROW );
419 const Sequence< PropertyValue >& rSessionPersistentProps = m_aDatasourceProperties[_sURL];
420 const PropertyValue* pProp = rSessionPersistentProps.getConstArray();
421 const PropertyValue* pPropsEnd = rSessionPersistentProps.getConstArray() + rSessionPersistentProps.getLength();
422 for ( ; pProp != pPropsEnd; ++pProp )
423 {
424 if ( pProp->Name.equalsAscii( "AuthFailedPassword" ) )
425 {
426 OSL_VERIFY( pProp->Value >>= sAuthFailedPassword );
427 }
428 else
429 {
430 xDSProps->setPropertyValue( pProp->Name, pProp->Value );
431 }
432 }
433
434 _rDataSourceModel.m_sFailedPassword = sAuthFailedPassword;
435 }
436 catch( const Exception& )
437 {
438 DBG_UNHANDLED_EXCEPTION();
439 }
440 }
441
442 //------------------------------------------------------------------------------
registerObject(const rtl::OUString & _rName,const Reference<XInterface> & _rxObject)443 void ODatabaseContext::registerObject(const rtl::OUString& _rName, const Reference< XInterface > & _rxObject)
444 {
445 MutexGuard aGuard(m_aMutex);
446 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
447
448 if ( !_rName.getLength() )
449 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
450
451 Reference< XDocumentDataSource > xDocDataSource( _rxObject, UNO_QUERY );
452 Reference< XModel > xModel( xDocDataSource.is() ? xDocDataSource->getDatabaseDocument() : Reference< XOfficeDatabaseDocument >(), UNO_QUERY );
453 if ( !xModel.is() )
454 throw IllegalArgumentException( ::rtl::OUString(), *this, 2 );
455
456 ::rtl::OUString sURL = xModel->getURL();
457 if ( !sURL.getLength() )
458 throw IllegalArgumentException( DBACORE_RESSTRING( RID_STR_DATASOURCE_NOT_STORED ), *this, 2 );
459
460 registerDatabaseLocation( _rName, sURL );
461
462 ODatabaseSource::setName( xDocDataSource, _rName, ODatabaseSource::DBContextAccess() );
463
464 // notify our container listeners
465 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rName), makeAny(_rxObject), Any());
466 m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
467 }
468
469 //------------------------------------------------------------------------------
storeTransientProperties(ODatabaseModelImpl & _rModelImpl)470 void ODatabaseContext::storeTransientProperties( ODatabaseModelImpl& _rModelImpl)
471 {
472 Reference< XPropertySet > xSource( _rModelImpl.getOrCreateDataSource(), UNO_QUERY );
473 ::comphelper::NamedValueCollection aRememberProps;
474
475 try
476 {
477 // get the info about the properties, check which ones are transient and not readonly
478 Reference< XPropertySetInfo > xSetInfo;
479 if (xSource.is())
480 xSetInfo = xSource->getPropertySetInfo();
481 Sequence< Property > aProperties;
482 if (xSetInfo.is())
483 aProperties = xSetInfo->getProperties();
484
485 if (aProperties.getLength())
486 {
487 const Property* pProperties = aProperties.getConstArray();
488 for ( sal_Int32 i=0; i<aProperties.getLength(); ++i, ++pProperties )
489 {
490 if ( ( ( pProperties->Attributes & PropertyAttribute::TRANSIENT) != 0 )
491 && ( ( pProperties->Attributes & PropertyAttribute::READONLY) == 0 )
492 )
493 {
494 // found such a property
495 aRememberProps.put( pProperties->Name, xSource->getPropertyValue( pProperties->Name ) );
496 }
497 }
498 }
499 }
500 catch ( const Exception& )
501 {
502 DBG_UNHANDLED_EXCEPTION();
503 }
504
505 // additionally, remember the "failed password", which is not available as property
506 // #i86178# / 2008-02-19 / frank.schoenheit@sun.com
507 aRememberProps.put( "AuthFailedPassword", _rModelImpl.m_sFailedPassword );
508
509 ::rtl::OUString sDocumentURL( _rModelImpl.getURL() );
510 if ( m_aDatabaseObjects.find( sDocumentURL ) != m_aDatabaseObjects.end() )
511 {
512 m_aDatasourceProperties[ sDocumentURL ] = aRememberProps.getPropertyValues();
513 }
514 else if ( m_aDatabaseObjects.find( _rModelImpl.m_sName ) != m_aDatabaseObjects.end() )
515 {
516 OSL_ENSURE( false, "ODatabaseContext::storeTransientProperties: a database document register by name? This shouldn't happen anymore!" );
517 // all the code should have been changed so that registration is by URL only
518 m_aDatasourceProperties[ _rModelImpl.m_sName ] = aRememberProps.getPropertyValues();
519 }
520 else
521 {
522 OSL_ENSURE( ( sDocumentURL.getLength() == 0 ) && ( _rModelImpl.m_sName.getLength() == 0 ),
523 "ODatabaseContext::storeTransientProperties: a non-empty data source which I do not know?!" );
524 }
525 }
526
527 //------------------------------------------------------------------------------
addContainerListener(const Reference<XContainerListener> & _rxListener)528 void SAL_CALL ODatabaseContext::addContainerListener( const Reference< XContainerListener >& _rxListener )
529 {
530 m_aContainerListeners.addInterface(_rxListener);
531 }
532
533 //------------------------------------------------------------------------------
removeContainerListener(const Reference<XContainerListener> & _rxListener)534 void SAL_CALL ODatabaseContext::removeContainerListener( const Reference< XContainerListener >& _rxListener )
535 {
536 m_aContainerListeners.removeInterface(_rxListener);
537 }
538
539 //------------------------------------------------------------------------------
revokeObject(const rtl::OUString & _rName)540 void ODatabaseContext::revokeObject(const rtl::OUString& _rName)
541 {
542 ClearableMutexGuard aGuard(m_aMutex);
543 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
544
545 ::rtl::OUString sURL = getDatabaseLocation( _rName );
546
547 revokeDatabaseLocation( _rName );
548 // will throw if something goes wrong
549
550 if ( m_aDatabaseObjects.find( _rName ) != m_aDatabaseObjects.end() )
551 {
552 m_aDatasourceProperties[ sURL ] = m_aDatasourceProperties[ _rName ];
553 }
554
555 // check if URL is already loaded
556 ObjectCacheIterator aExistent = m_aDatabaseObjects.find( sURL );
557 if ( aExistent != m_aDatabaseObjects.end() )
558 m_aDatabaseObjects.erase( aExistent );
559
560 // notify our container listeners
561 ContainerEvent aEvent( *this, makeAny( _rName ), Any(), Any() );
562 aGuard.clear();
563 m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
564 }
565
566 //------------------------------------------------------------------------------
hasRegisteredDatabase(const::rtl::OUString & _Name)567 ::sal_Bool SAL_CALL ODatabaseContext::hasRegisteredDatabase( const ::rtl::OUString& _Name )
568 {
569 return m_xDatabaseRegistrations->hasRegisteredDatabase( _Name );
570 }
571
572 //------------------------------------------------------------------------------
getRegistrationNames()573 Sequence< ::rtl::OUString > SAL_CALL ODatabaseContext::getRegistrationNames()
574 {
575 return m_xDatabaseRegistrations->getRegistrationNames();
576 }
577
578 //------------------------------------------------------------------------------
getDatabaseLocation(const::rtl::OUString & _Name)579 ::rtl::OUString SAL_CALL ODatabaseContext::getDatabaseLocation( const ::rtl::OUString& _Name )
580 {
581 return m_xDatabaseRegistrations->getDatabaseLocation( _Name );
582 }
583
584 //------------------------------------------------------------------------------
registerDatabaseLocation(const::rtl::OUString & _Name,const::rtl::OUString & _Location)585 void SAL_CALL ODatabaseContext::registerDatabaseLocation( const ::rtl::OUString& _Name, const ::rtl::OUString& _Location )
586 {
587 m_xDatabaseRegistrations->registerDatabaseLocation( _Name, _Location );
588 }
589
590 //------------------------------------------------------------------------------
revokeDatabaseLocation(const::rtl::OUString & _Name)591 void SAL_CALL ODatabaseContext::revokeDatabaseLocation( const ::rtl::OUString& _Name )
592 {
593 m_xDatabaseRegistrations->revokeDatabaseLocation( _Name );
594 }
595
596 //------------------------------------------------------------------------------
changeDatabaseLocation(const::rtl::OUString & _Name,const::rtl::OUString & _NewLocation)597 void SAL_CALL ODatabaseContext::changeDatabaseLocation( const ::rtl::OUString& _Name, const ::rtl::OUString& _NewLocation )
598 {
599 m_xDatabaseRegistrations->changeDatabaseLocation( _Name, _NewLocation );
600 }
601
602 //------------------------------------------------------------------------------
isDatabaseRegistrationReadOnly(const::rtl::OUString & _Name)603 ::sal_Bool SAL_CALL ODatabaseContext::isDatabaseRegistrationReadOnly( const ::rtl::OUString& _Name )
604 {
605 return m_xDatabaseRegistrations->isDatabaseRegistrationReadOnly( _Name );
606 }
607
608 //------------------------------------------------------------------------------
addDatabaseRegistrationsListener(const Reference<XDatabaseRegistrationsListener> & _Listener)609 void SAL_CALL ODatabaseContext::addDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& _Listener )
610 {
611 m_xDatabaseRegistrations->addDatabaseRegistrationsListener( _Listener );
612 }
613
614 //------------------------------------------------------------------------------
removeDatabaseRegistrationsListener(const Reference<XDatabaseRegistrationsListener> & _Listener)615 void SAL_CALL ODatabaseContext::removeDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& _Listener )
616 {
617 m_xDatabaseRegistrations->removeDatabaseRegistrationsListener( _Listener );
618 }
619
620 // ::com::sun::star::container::XElementAccess
621 //------------------------------------------------------------------------------
getElementType()622 Type ODatabaseContext::getElementType( )
623 {
624 return::getCppuType(static_cast<Reference<XDataSource>*>(NULL));
625 }
626
627 //------------------------------------------------------------------------------
hasElements(void)628 sal_Bool ODatabaseContext::hasElements(void)
629 {
630 MutexGuard aGuard(m_aMutex);
631 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
632
633 return 0 != getElementNames().getLength();
634 }
635
636 // ::com::sun::star::container::XEnumerationAccess
637 //------------------------------------------------------------------------------
createEnumeration(void)638 Reference< ::com::sun::star::container::XEnumeration > ODatabaseContext::createEnumeration(void)
639 {
640 MutexGuard aGuard(m_aMutex);
641 return new ::comphelper::OEnumerationByName(static_cast<XNameAccess*>(this));
642 }
643
644 // ::com::sun::star::container::XNameAccess
645 //------------------------------------------------------------------------------
getByName(const rtl::OUString & _rName)646 Any ODatabaseContext::getByName(const rtl::OUString& _rName)
647 {
648 MutexGuard aGuard(m_aMutex);
649 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
650 if ( !_rName.getLength() )
651 throw NoSuchElementException(_rName, *this);
652
653 try
654 {
655 Reference< XInterface > xExistent = getObject( _rName );
656 if ( xExistent.is() )
657 return makeAny( xExistent );
658
659 // see whether this is an registered name
660 ::rtl::OUString sURL;
661 if ( hasRegisteredDatabase( _rName ) )
662 {
663 sURL = getDatabaseLocation( _rName );
664 // is the object cached under its URL?
665 xExistent = getObject( sURL );
666 }
667 else
668 // interpret the name as URL
669 sURL = _rName;
670
671 if ( !xExistent.is() )
672 // try to load this as URL
673 xExistent = loadObjectFromURL( _rName, sURL );
674 return makeAny( xExistent );
675 }
676 catch (NoSuchElementException&)
677 { // let these exceptions through
678 throw;
679 }
680 catch (WrappedTargetException&)
681 { // let these exceptions through
682 throw;
683 }
684 catch (RuntimeException&)
685 { // let these exceptions through
686 throw;
687 }
688 catch (Exception& e)
689 { // exceptions other than the specified ones -> wrap
690 Any aError = ::cppu::getCaughtException();
691 throw WrappedTargetException(_rName, *this, aError );
692 }
693 }
694
695 //------------------------------------------------------------------------------
getElementNames(void)696 Sequence< rtl::OUString > ODatabaseContext::getElementNames(void)
697 {
698 MutexGuard aGuard(m_aMutex);
699 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
700
701 return getRegistrationNames();
702 }
703
704 //------------------------------------------------------------------------------
hasByName(const rtl::OUString & _rName)705 sal_Bool ODatabaseContext::hasByName(const rtl::OUString& _rName)
706 {
707 MutexGuard aGuard(m_aMutex);
708 ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
709
710 return hasRegisteredDatabase( _rName );
711 }
712
713 // -----------------------------------------------------------------------------
getObject(const::rtl::OUString & _rURL)714 Reference< XInterface > ODatabaseContext::getObject( const ::rtl::OUString& _rURL )
715 {
716 ObjectCacheIterator aFind = m_aDatabaseObjects.find( _rURL );
717 Reference< XInterface > xExistent;
718 if ( aFind != m_aDatabaseObjects.end() )
719 xExistent = aFind->second->getOrCreateDataSource();
720 return xExistent;
721 }
722 // -----------------------------------------------------------------------------
registerDatabaseDocument(ODatabaseModelImpl & _rModelImpl)723 void ODatabaseContext::registerDatabaseDocument( ODatabaseModelImpl& _rModelImpl )
724 {
725 ::rtl::OUString sURL( _rModelImpl.getURL() );
726 #if OSL_DEBUG_LEVEL > 1
727 OSL_TRACE( "DatabaseContext: registering %s", ::rtl::OUStringToOString( sURL, RTL_TEXTENCODING_UTF8 ).getStr() );
728 #endif
729 if ( m_aDatabaseObjects.find( sURL ) == m_aDatabaseObjects.end() )
730 {
731 m_aDatabaseObjects[ sURL ] = &_rModelImpl;
732 setTransientProperties( sURL, _rModelImpl );
733 }
734 else
735 OSL_ENSURE( false, "ODatabaseContext::registerDatabaseDocument: already have an object registered for this URL!" );
736 }
737 // -----------------------------------------------------------------------------
revokeDatabaseDocument(const ODatabaseModelImpl & _rModelImpl)738 void ODatabaseContext::revokeDatabaseDocument( const ODatabaseModelImpl& _rModelImpl )
739 {
740 ::rtl::OUString sURL( _rModelImpl.getURL() );
741 #if OSL_DEBUG_LEVEL > 1
742 OSL_TRACE( "DatabaseContext: deregistering %s", ::rtl::OUStringToOString( sURL, RTL_TEXTENCODING_UTF8 ).getStr() );
743 #endif
744 m_aDatabaseObjects.erase( sURL );
745 }
746 // -----------------------------------------------------------------------------
databaseDocumentURLChange(const::rtl::OUString & _rOldURL,const::rtl::OUString & _rNewURL)747 void ODatabaseContext::databaseDocumentURLChange( const ::rtl::OUString& _rOldURL, const ::rtl::OUString& _rNewURL )
748 {
749 #if OSL_DEBUG_LEVEL > 1
750 OSL_TRACE( "DatabaseContext: changing registration from %s to %s",
751 ::rtl::OUStringToOString( _rOldURL, RTL_TEXTENCODING_UTF8 ).getStr(),
752 ::rtl::OUStringToOString( _rNewURL, RTL_TEXTENCODING_UTF8 ).getStr() );
753 #endif
754 ObjectCache::iterator oldPos = m_aDatabaseObjects.find( _rOldURL );
755 ENSURE_OR_THROW( oldPos != m_aDatabaseObjects.end(), "illegal old database document URL" );
756 ObjectCache::iterator newPos = m_aDatabaseObjects.find( _rNewURL );
757 ENSURE_OR_THROW( newPos == m_aDatabaseObjects.end(), "illegal new database document URL" );
758
759 m_aDatabaseObjects[ _rNewURL ] = oldPos->second;
760 m_aDatabaseObjects.erase( oldPos );
761 }
762 // -----------------------------------------------------------------------------
getSomething(const Sequence<sal_Int8> & rId)763 sal_Int64 SAL_CALL ODatabaseContext::getSomething( const Sequence< sal_Int8 >& rId )
764 {
765 if (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
766 return reinterpret_cast<sal_Int64>(this);
767
768 return 0;
769 }
770 // -----------------------------------------------------------------------------
getUnoTunnelImplementationId()771 Sequence< sal_Int8 > ODatabaseContext::getUnoTunnelImplementationId()
772 {
773 static ::cppu::OImplementationId * pId = 0;
774 if (! pId)
775 {
776 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
777 if (! pId)
778 {
779 static ::cppu::OImplementationId aId;
780 pId = &aId;
781 }
782 }
783 return pId->getImplementationId();
784 }
785
786 // -----------------------------------------------------------------------------
onBasicManagerCreated(const Reference<XModel> & _rxForDocument,BasicManager & _rBasicManager)787 void ODatabaseContext::onBasicManagerCreated( const Reference< XModel >& _rxForDocument, BasicManager& _rBasicManager )
788 {
789 // if it's a database document ...
790 Reference< XOfficeDatabaseDocument > xDatabaseDocument( _rxForDocument, UNO_QUERY );
791 // ... or a sub document of a database document ...
792 if ( !xDatabaseDocument.is() )
793 {
794 Reference< XChild > xDocAsChild( _rxForDocument, UNO_QUERY );
795 if ( xDocAsChild.is() )
796 xDatabaseDocument.set( xDocAsChild->getParent(), UNO_QUERY );
797 }
798
799 // ... whose BasicManager has just been created, then add the global DatabaseDocument variable to its scope.
800 if ( xDatabaseDocument.is() )
801 _rBasicManager.SetGlobalUNOConstant( "ThisDatabaseDocument", makeAny( xDatabaseDocument ) );
802 }
803
804 //........................................................................
805 } // namespace dbaccess
806 //........................................................................
807