Files
libreoffice/dbaccess/source/core/dataaccess/databaseregistrations.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

359 lines
14 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-14 17:39:53 +01:00
/*
* This file is part of the LibreOffice project.
*
2012-06-14 17:39:53 +01:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
2012-06-14 17:39:53 +01:00
* This file incorporates work covered by the following license notice:
*
2012-06-14 17:39:53 +01:00
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <com/sun/star/lang/IllegalAccessException.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/container/ElementExistException.hpp>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
#include <cppuhelper/basemutex.hxx>
#include <comphelper/interfacecontainer3.hxx>
#include <cppuhelper/implbase.hxx>
#include <osl/diagnose.h>
2010-01-19 12:39:03 +01:00
#include <unotools/pathoptions.hxx>
#include <tools/urlobj.hxx>
#include <unotools/confignode.hxx>
#include "databaseregistrations.hxx"
namespace dbaccess
{
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::RuntimeException;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::XComponentContext;
using ::com::sun::star::container::NoSuchElementException;
using ::com::sun::star::lang::IllegalArgumentException;
using ::com::sun::star::lang::IllegalAccessException;
using ::com::sun::star::container::ElementExistException;
using ::com::sun::star::sdb::XDatabaseRegistrations;
using ::com::sun::star::sdb::XDatabaseRegistrationsListener;
using ::com::sun::star::sdb::DatabaseRegistrationEvent;
using ::com::sun::star::uno::XAggregation;
constexpr OUString CONF_ROOT_PATH = u"org.openoffice.Office.DataAccess/RegisteredNames"_ustr;
constexpr OUString LOCATION = u"Location"_ustr;
constexpr OUString NAME = u"Name"_ustr;
// DatabaseRegistrations - declaration
typedef ::cppu::WeakImplHelper< XDatabaseRegistrations
> DatabaseRegistrations_Base;
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
namespace {
class DatabaseRegistrations :public ::cppu::BaseMutex
,public DatabaseRegistrations_Base
{
public:
explicit DatabaseRegistrations( const Reference<XComponentContext>& _rxContext );
protected:
virtual ~DatabaseRegistrations() override;
public:
virtual sal_Bool SAL_CALL hasRegisteredDatabase( const OUString& Name ) override;
virtual Sequence< OUString > SAL_CALL getRegistrationNames() override;
virtual OUString SAL_CALL getDatabaseLocation( const OUString& Name ) override;
virtual void SAL_CALL registerDatabaseLocation( const OUString& Name, const OUString& Location ) override;
virtual void SAL_CALL revokeDatabaseLocation( const OUString& Name ) override;
virtual void SAL_CALL changeDatabaseLocation( const OUString& Name, const OUString& NewLocation ) override;
virtual sal_Bool SAL_CALL isDatabaseRegistrationReadOnly( const OUString& Name ) override;
virtual void SAL_CALL addDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& Listener ) override;
virtual void SAL_CALL removeDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& Listener ) override;
private:
void
impl_checkValidName_common(std::u16string_view _rName);
::utl::OConfigurationNode
impl_checkValidName_throw_must_exist(const OUString& _rName);
::utl::OConfigurationNode
impl_checkValidName_throw_must_not_exist(const OUString& _rName);
void impl_checkValidLocation_throw( std::u16string_view _rLocation );
/** retrieves the configuration node whose "Name" sub node has the given value
Since we separated the name of the registration node from the "Name" value of the registration, we cannot
simply do a "getByName" (equivalent) when we want to retrieve the node for a given registration name.
Instead, we must search all nodes.
If a node with the given display name does not exist, then a NoSuchElementException is thrown.
If no exception is thrown, then a valid node is returned: If the node existed it is returned.
*/
::utl::OConfigurationNode
impl_getNodeForName_throw_must_exist(const OUString& _rName);
/** retrieves the configuration node whose "Name" sub node has the given value
Since we separated the name of the registration node from the "Name" value of the registration, we cannot
simply do a "getByName" (equivalent) when we want to retrieve the node for a given registration name.
Instead, we must search all nodes.
If a node with the given name already exists, then an ElementExistException is thrown.
If no exception is thrown, then a valid node is returned: If the node did not yet exist a new node is created,
in this case the root node is not yet committed.
*/
::utl::OConfigurationNode
impl_getNodeForName_throw_must_not_exist(const OUString& _rName);
::utl::OConfigurationNode
impl_getNodeForName_nothrow(std::u16string_view _rName);
private:
Reference<XComponentContext> m_aContext;
::utl::OConfigurationTreeRoot m_aConfigurationRoot;
::comphelper::OInterfaceContainerHelper3<XDatabaseRegistrationsListener> m_aRegistrationListeners;
};
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
}
// DatabaseRegistrations - implementation
DatabaseRegistrations::DatabaseRegistrations( const Reference<XComponentContext> & _rxContext )
:m_aContext( _rxContext )
,m_aRegistrationListeners( m_aMutex )
{
m_aConfigurationRoot = ::utl::OConfigurationTreeRoot::createWithComponentContext(
m_aContext, CONF_ROOT_PATH );
}
DatabaseRegistrations::~DatabaseRegistrations()
{
}
::utl::OConfigurationNode DatabaseRegistrations::impl_getNodeForName_nothrow( std::u16string_view _rName )
{
const Sequence< OUString > aNames( m_aConfigurationRoot.getNodeNames() );
for ( auto const & nodeName : aNames )
{
::utl::OConfigurationNode aNodeForName = m_aConfigurationRoot.openNode( nodeName );
OUString sTestName;
OSL_VERIFY( aNodeForName.getNodeValue( NAME ) >>= sTestName );
if ( sTestName == _rName )
return aNodeForName;
}
return ::utl::OConfigurationNode();
}
::utl::OConfigurationNode DatabaseRegistrations::impl_getNodeForName_throw_must_exist(const OUString& _rName)
{
::utl::OConfigurationNode aNodeForName( impl_getNodeForName_nothrow( _rName ) );
if (!aNodeForName.isValid())
{
throw NoSuchElementException( _rName, *this );
}
return aNodeForName;
}
::utl::OConfigurationNode DatabaseRegistrations::impl_getNodeForName_throw_must_not_exist(const OUString& _rName)
{
::utl::OConfigurationNode aNodeForName( impl_getNodeForName_nothrow( _rName ) );
if (aNodeForName.isValid())
throw ElementExistException( _rName, *this );
// make unique
OUString sNewNodeName = "org.openoffice." + _rName;
while ( m_aConfigurationRoot.hasByName( sNewNodeName ) )
{
sNewNodeName = "org.openoffice." + _rName + " 2";
}
::utl::OConfigurationNode aNewNode( m_aConfigurationRoot.createNode( sNewNodeName ) );
aNewNode.setNodeValue( NAME, Any( _rName ) );
return aNewNode;
}
void DatabaseRegistrations::impl_checkValidName_common(std::u16string_view _rName)
{
if ( !m_aConfigurationRoot.isValid() )
throw RuntimeException( OUString(), *this );
if ( _rName.empty() )
throw IllegalArgumentException( OUString(), *this, 1 );
}
::utl::OConfigurationNode DatabaseRegistrations::impl_checkValidName_throw_must_exist(const OUString& _rName)
{
impl_checkValidName_common(_rName);
return impl_getNodeForName_throw_must_exist(_rName);
}
::utl::OConfigurationNode DatabaseRegistrations::impl_checkValidName_throw_must_not_exist(const OUString& _rName)
{
impl_checkValidName_common(_rName);
return impl_getNodeForName_throw_must_not_exist(_rName);
}
void DatabaseRegistrations::impl_checkValidLocation_throw( std::u16string_view _rLocation )
{
if ( _rLocation.empty() )
throw IllegalArgumentException( OUString(), *this, 2 );
INetURLObject aURL( _rLocation );
if ( aURL.GetProtocol() == INetProtocol::NotValid )
throw IllegalArgumentException( OUString(), *this, 2 );
}
sal_Bool SAL_CALL DatabaseRegistrations::hasRegisteredDatabase( const OUString& Name )
{
::osl::MutexGuard aGuard( m_aMutex );
::utl::OConfigurationNode aNodeForName = impl_getNodeForName_nothrow( Name );
return aNodeForName.isValid();
}
Sequence< OUString > SAL_CALL DatabaseRegistrations::getRegistrationNames()
{
::osl::MutexGuard aGuard( m_aMutex );
if ( !m_aConfigurationRoot.isValid() )
throw RuntimeException( OUString(), *this );
const Sequence< OUString > aProgrammaticNames( m_aConfigurationRoot.getNodeNames() );
Sequence< OUString > aDisplayNames( aProgrammaticNames.getLength() );
OUString* pDisplayName = aDisplayNames.getArray();
for ( auto const & name : aProgrammaticNames )
{
::utl::OConfigurationNode aRegistrationNode = m_aConfigurationRoot.openNode( name );
OSL_VERIFY( aRegistrationNode.getNodeValue( NAME ) >>= *pDisplayName );
++pDisplayName;
}
return aDisplayNames;
}
OUString SAL_CALL DatabaseRegistrations::getDatabaseLocation( const OUString& Name )
{
::osl::MutexGuard aGuard( m_aMutex );
::utl::OConfigurationNode aNodeForName = impl_checkValidName_throw_must_exist(Name);
OUString sLocation;
OSL_VERIFY( aNodeForName.getNodeValue( LOCATION ) >>= sLocation );
sLocation = SvtPathOptions().SubstituteVariable( sLocation );
return sLocation;
}
void SAL_CALL DatabaseRegistrations::registerDatabaseLocation( const OUString& Name, const OUString& Location )
{
::osl::ClearableMutexGuard aGuard( m_aMutex );
// check
impl_checkValidLocation_throw( Location );
::utl::OConfigurationNode aDataSourceRegistration = impl_checkValidName_throw_must_not_exist(Name);
// register
aDataSourceRegistration.setNodeValue( LOCATION, Any( Location ) );
m_aConfigurationRoot.commit();
// notify
DatabaseRegistrationEvent aEvent( *this, Name, OUString(), Location );
aGuard.clear();
m_aRegistrationListeners.notifyEach( &XDatabaseRegistrationsListener::registeredDatabaseLocation, aEvent );
}
void SAL_CALL DatabaseRegistrations::revokeDatabaseLocation( const OUString& Name )
{
::osl::ClearableMutexGuard aGuard( m_aMutex );
// check
::utl::OConfigurationNode aNodeForName = impl_checkValidName_throw_must_exist(Name);
// obtain properties for notification
OUString sLocation;
OSL_VERIFY( aNodeForName.getNodeValue( LOCATION ) >>= sLocation );
// revoke
if ( aNodeForName.isReadonly()
|| !m_aConfigurationRoot.removeNode( aNodeForName.getLocalName() )
)
throw IllegalAccessException( OUString(), *this );
m_aConfigurationRoot.commit();
// notify
DatabaseRegistrationEvent aEvent( *this, Name, sLocation, OUString() );
aGuard.clear();
m_aRegistrationListeners.notifyEach( &XDatabaseRegistrationsListener::revokedDatabaseLocation, aEvent );
}
void SAL_CALL DatabaseRegistrations::changeDatabaseLocation( const OUString& Name, const OUString& NewLocation )
{
::osl::ClearableMutexGuard aGuard( m_aMutex );
// check
impl_checkValidLocation_throw( NewLocation );
::utl::OConfigurationNode aDataSourceRegistration = impl_checkValidName_throw_must_exist(Name);
if ( aDataSourceRegistration.isReadonly() )
throw IllegalAccessException( OUString(), *this );
// obtain properties for notification
OUString sOldLocation;
OSL_VERIFY( aDataSourceRegistration.getNodeValue( LOCATION ) >>= sOldLocation );
// change
aDataSourceRegistration.setNodeValue( LOCATION, Any( NewLocation ) );
m_aConfigurationRoot.commit();
// notify
DatabaseRegistrationEvent aEvent( *this, Name, sOldLocation, NewLocation );
aGuard.clear();
m_aRegistrationListeners.notifyEach( &XDatabaseRegistrationsListener::changedDatabaseLocation, aEvent );
}
sal_Bool SAL_CALL DatabaseRegistrations::isDatabaseRegistrationReadOnly( const OUString& Name )
{
::osl::MutexGuard aGuard( m_aMutex );
::utl::OConfigurationNode aDataSourceRegistration = impl_checkValidName_throw_must_exist(Name);
return aDataSourceRegistration.isReadonly();
}
void SAL_CALL DatabaseRegistrations::addDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& Listener )
{
if ( Listener.is() )
m_aRegistrationListeners.addInterface( Listener );
}
void SAL_CALL DatabaseRegistrations::removeDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& Listener )
{
if ( Listener.is() )
m_aRegistrationListeners.removeInterface( Listener );
}
// DatabaseRegistrations - factory
Reference< XDatabaseRegistrations > createDataSourceRegistrations( const Reference<XComponentContext> & _rxContext )
{
return new DatabaseRegistrations( _rxContext );
}
} // namespace dbaccess
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */