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

Generated by: LCOV version 1.10