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