LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/connectivity/source/commontools - ConnectionWrapper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 61 105 58.1 %
Date: 2013-07-09 Functions: 9 16 56.2 %
Legend: Lines: hit not hit

          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             : #include "connectivity/ConnectionWrapper.hxx"
      21             : #include <com/sun/star/sdbc/ColumnValue.hpp>
      22             : #include <com/sun/star/sdbc/XRow.hpp>
      23             : #include <com/sun/star/lang/DisposedException.hpp>
      24             : #include <comphelper/uno3.hxx>
      25             : #include <comphelper/sequence.hxx>
      26             : #include <cppuhelper/typeprovider.hxx>
      27             : #include <com/sun/star/reflection/ProxyFactory.hpp>
      28             : #include <rtl/digest.h>
      29             : #include <algorithm>
      30             : #include <string.h>
      31             : 
      32             : using namespace connectivity;
      33             : //------------------------------------------------------------------------------
      34             : using namespace com::sun::star::uno;
      35             : using namespace com::sun::star::lang;
      36             : using namespace com::sun::star::beans;
      37             : using namespace com::sun::star::sdbc;
      38             : using namespace ::com::sun::star::reflection;
      39             : // --------------------------------------------------------------------------------
      40          58 : OConnectionWrapper::OConnectionWrapper()
      41             : {
      42             : 
      43          58 : }
      44             : // -----------------------------------------------------------------------------
      45          58 : void OConnectionWrapper::setDelegation(Reference< XAggregation >& _rxProxyConnection,oslInterlockedCount& _rRefCount)
      46             : {
      47             :     OSL_ENSURE(_rxProxyConnection.is(),"OConnectionWrapper: Connection must be valid!");
      48          58 :     osl_atomic_increment( &_rRefCount );
      49          58 :     if (_rxProxyConnection.is())
      50             :     {
      51             :         // transfer the (one and only) real ref to the aggregate to our member
      52          58 :         m_xProxyConnection = _rxProxyConnection;
      53          58 :         _rxProxyConnection = NULL;
      54          58 :         ::comphelper::query_aggregation(m_xProxyConnection,m_xConnection);
      55          58 :         m_xTypeProvider.set(m_xConnection,UNO_QUERY);
      56          58 :         m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
      57          58 :         m_xServiceInfo.set(m_xConnection,UNO_QUERY);
      58             : 
      59             :         // set ourself as delegator
      60          58 :         Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
      61          58 :         m_xProxyConnection->setDelegator( xIf );
      62             : 
      63             :     }
      64          58 :     osl_atomic_decrement( &_rRefCount );
      65          58 : }
      66             : // -----------------------------------------------------------------------------
      67           0 : void OConnectionWrapper::setDelegation(const Reference< XConnection >& _xConnection
      68             :                                        ,const Reference< XComponentContext>& _rxContext
      69             :                                        ,oslInterlockedCount& _rRefCount)
      70             : {
      71             :     OSL_ENSURE(_xConnection.is(),"OConnectionWrapper: Connection must be valid!");
      72           0 :     osl_atomic_increment( &_rRefCount );
      73             : 
      74           0 :     m_xConnection = _xConnection;
      75           0 :     m_xTypeProvider.set(m_xConnection,UNO_QUERY);
      76           0 :     m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
      77           0 :     m_xServiceInfo.set(m_xConnection,UNO_QUERY);
      78             : 
      79           0 :     Reference< XProxyFactory >  xProxyFactory = ProxyFactory::create( _rxContext );
      80           0 :     Reference< XAggregation > xConProxy = xProxyFactory->createProxy(_xConnection);
      81           0 :     if (xConProxy.is())
      82             :     {
      83             :         // transfer the (one and only) real ref to the aggregate to our member
      84           0 :         m_xProxyConnection = xConProxy;
      85             : 
      86             :         // set ourself as delegator
      87           0 :         Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
      88           0 :         m_xProxyConnection->setDelegator( xIf );
      89             : 
      90             :     }
      91           0 :     osl_atomic_decrement( &_rRefCount );
      92           0 : }
      93             : // -----------------------------------------------------------------------------
      94          58 : void OConnectionWrapper::disposing()
      95             : {
      96          58 : m_xConnection.clear();
      97          58 : }
      98             : //-----------------------------------------------------------------------------
      99         116 : OConnectionWrapper::~OConnectionWrapper()
     100             : {
     101          58 :     if (m_xProxyConnection.is())
     102          58 :         m_xProxyConnection->setDelegator(NULL);
     103          58 : }
     104             : 
     105             : // XServiceInfo
     106             : // --------------------------------------------------------------------------------
     107           0 : OUString SAL_CALL OConnectionWrapper::getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException)
     108             : {
     109           0 :     return OUString( "com.sun.star.sdbc.drivers.OConnectionWrapper" );
     110             : }
     111             : 
     112             : // --------------------------------------------------------------------------------
     113           0 : ::com::sun::star::uno::Sequence< OUString > SAL_CALL OConnectionWrapper::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
     114             : {
     115             :     // first collect the services which are supported by our aggregate
     116           0 :     Sequence< OUString > aSupported;
     117           0 :     if ( m_xServiceInfo.is() )
     118           0 :         aSupported = m_xServiceInfo->getSupportedServiceNames();
     119             : 
     120             :     // append our own service, if necessary
     121           0 :     OUString sConnectionService( "com.sun.star.sdbc.Connection" );
     122           0 :     if ( 0 == ::comphelper::findValue( aSupported, sConnectionService, sal_True ).getLength() )
     123             :     {
     124           0 :         sal_Int32 nLen = aSupported.getLength();
     125           0 :         aSupported.realloc( nLen + 1 );
     126           0 :         aSupported[ nLen ] = sConnectionService;
     127             :     }
     128             : 
     129             :     // outta here
     130           0 :     return aSupported;
     131             : }
     132             : 
     133             : // --------------------------------------------------------------------------------
     134           0 : sal_Bool SAL_CALL OConnectionWrapper::supportsService( const OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException)
     135             : {
     136           0 :     return ::comphelper::findValue( getSupportedServiceNames(), _rServiceName, sal_True ).getLength() != 0;
     137             : }
     138             : 
     139             : // --------------------------------------------------------------------------------
     140         564 : Any SAL_CALL OConnectionWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
     141             : {
     142         564 :     Any aReturn = OConnection_BASE::queryInterface(_rType);
     143         564 :     return aReturn.hasValue() ? aReturn : (m_xProxyConnection.is() ? m_xProxyConnection->queryAggregation(_rType) : aReturn);
     144             : }
     145             : // --------------------------------------------------------------------------------
     146           2 : Sequence< Type > SAL_CALL OConnectionWrapper::getTypes(  ) throw (::com::sun::star::uno::RuntimeException)
     147             : {
     148             :     return ::comphelper::concatSequences(
     149             :         OConnection_BASE::getTypes(),
     150           2 :         m_xTypeProvider->getTypes()
     151           2 :     );
     152             : }
     153             : // -----------------------------------------------------------------------------
     154             : // com::sun::star::lang::XUnoTunnel
     155           0 : sal_Int64 SAL_CALL OConnectionWrapper::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
     156             : {
     157           0 :     if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
     158           0 :         return reinterpret_cast< sal_Int64 >( this );
     159             : 
     160           0 :     if(m_xUnoTunnel.is())
     161           0 :         return m_xUnoTunnel->getSomething(rId);
     162           0 :     return 0;
     163             : }
     164             : 
     165             : // -----------------------------------------------------------------------------
     166           0 : Sequence< sal_Int8 > OConnectionWrapper::getUnoTunnelImplementationId()
     167             : {
     168             :     static ::cppu::OImplementationId * pId = 0;
     169           0 :     if (! pId)
     170             :     {
     171           0 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     172           0 :         if (! pId)
     173             :         {
     174           0 :             static ::cppu::OImplementationId aId;
     175           0 :             pId = &aId;
     176           0 :         }
     177             :     }
     178           0 :     return pId->getImplementationId();
     179             : }
     180             : // -----------------------------------------------------------------------------
     181             : namespace
     182             : {
     183             :     class TPropertyValueLessFunctor : public ::std::binary_function< ::com::sun::star::beans::PropertyValue,::com::sun::star::beans::PropertyValue,bool>
     184             :     {
     185             :     public:
     186          29 :         TPropertyValueLessFunctor()
     187          29 :         {}
     188        5510 :         bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
     189             :         {
     190        5510 :             return !!(lhs.Name.equalsIgnoreAsciiCase( rhs.Name ));
     191             :         }
     192             :     };
     193             : 
     194             : }
     195             : 
     196             : // -----------------------------------------------------------------------------
     197             : // creates a unique id out of the url and sequence of properties
     198          29 : void OConnectionWrapper::createUniqueId( const OUString& _rURL
     199             :                     ,Sequence< PropertyValue >& _rInfo
     200             :                     ,sal_uInt8* _pBuffer
     201             :                     ,const OUString& _rUserName
     202             :                     ,const OUString& _rPassword)
     203             : {
     204             :     // first we create the digest we want to have
     205          29 :     rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
     206          29 :     rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
     207          29 :     if ( !_rUserName.isEmpty() )
     208           0 :         rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
     209          29 :     if ( !_rPassword.isEmpty() )
     210           0 :         rtl_digest_update(aDigest,_rPassword.getStr(),_rPassword.getLength()*sizeof(sal_Unicode));
     211             :     // now we need to sort the properties
     212          29 :     PropertyValue* pBegin = _rInfo.getArray();
     213          29 :     PropertyValue* pEnd   = pBegin + _rInfo.getLength();
     214          29 :     ::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
     215             : 
     216          29 :     pBegin = _rInfo.getArray();
     217          29 :     pEnd   = pBegin + _rInfo.getLength();
     218        1624 :     for (; pBegin != pEnd; ++pBegin)
     219             :     {
     220             :         // we only include strings an integer values
     221        1595 :         OUString sValue;
     222        1595 :         if ( pBegin->Value >>= sValue )
     223             :             ;
     224             :         else
     225             :         {
     226         899 :             sal_Int32 nValue = 0;
     227         899 :             if ( pBegin->Value >>= nValue )
     228         116 :                 sValue = OUString::valueOf(nValue);
     229             :             else
     230             :             {
     231         783 :                 Sequence< OUString> aSeq;
     232         783 :                 if ( pBegin->Value >>= aSeq )
     233             :                 {
     234          87 :                     const OUString* pSBegin = aSeq.getConstArray();
     235          87 :                     const OUString* pSEnd   = pSBegin + aSeq.getLength();
     236         116 :                     for(;pSBegin != pSEnd;++pSBegin)
     237          29 :                         rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
     238         783 :                 }
     239             :             }
     240             :         }
     241        1595 :         if ( !sValue.isEmpty() )
     242             :         {
     243             :             // we don't have to convert this into UTF8 because we don't store on a file system
     244         211 :             rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
     245             :         }
     246        1595 :     }
     247             : 
     248          29 :     rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
     249             :     // we have to destroy the digest
     250          29 :     rtl_digest_destroy(aDigest);
     251          29 : }
     252             : // -----------------------------------------------------------------------------
     253             : 
     254             : 
     255             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10