LCOV - code coverage report
Current view: top level - connectivity/source/commontools - ConnectionWrapper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 87 105 82.9 %
Date: 2014-11-03 Functions: 12 16 75.0 %
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         144 : OConnectionWrapper::OConnectionWrapper()
      42             : {
      43             : 
      44         144 : }
      45             : 
      46         140 : void OConnectionWrapper::setDelegation(Reference< XAggregation >& _rxProxyConnection,oslInterlockedCount& _rRefCount)
      47             : {
      48             :     OSL_ENSURE(_rxProxyConnection.is(),"OConnectionWrapper: Connection must be valid!");
      49         140 :     osl_atomic_increment( &_rRefCount );
      50         140 :     if (_rxProxyConnection.is())
      51             :     {
      52             :         // transfer the (one and only) real ref to the aggregate to our member
      53         140 :         m_xProxyConnection = _rxProxyConnection;
      54         140 :         _rxProxyConnection = NULL;
      55         140 :         ::comphelper::query_aggregation(m_xProxyConnection,m_xConnection);
      56         140 :         m_xTypeProvider.set(m_xConnection,UNO_QUERY);
      57         140 :         m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
      58         140 :         m_xServiceInfo.set(m_xConnection,UNO_QUERY);
      59             : 
      60             :         // set ourself as delegator
      61         140 :         Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
      62         140 :         m_xProxyConnection->setDelegator( xIf );
      63             : 
      64             :     }
      65         140 :     osl_atomic_decrement( &_rRefCount );
      66         140 : }
      67             : 
      68           4 : 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           4 :     osl_atomic_increment( &_rRefCount );
      74             : 
      75           4 :     m_xConnection = _xConnection;
      76           4 :     m_xTypeProvider.set(m_xConnection,UNO_QUERY);
      77           4 :     m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
      78           4 :     m_xServiceInfo.set(m_xConnection,UNO_QUERY);
      79             : 
      80           4 :     Reference< XProxyFactory >  xProxyFactory = ProxyFactory::create( _rxContext );
      81           8 :     Reference< XAggregation > xConProxy = xProxyFactory->createProxy(_xConnection);
      82           4 :     if (xConProxy.is())
      83             :     {
      84             :         // transfer the (one and only) real ref to the aggregate to our member
      85           4 :         m_xProxyConnection = xConProxy;
      86             : 
      87             :         // set ourself as delegator
      88           4 :         Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
      89           4 :         m_xProxyConnection->setDelegator( xIf );
      90             : 
      91             :     }
      92           8 :     osl_atomic_decrement( &_rRefCount );
      93           4 : }
      94             : 
      95         138 : void OConnectionWrapper::disposing()
      96             : {
      97         138 : m_xConnection.clear();
      98         138 : }
      99             : 
     100         276 : OConnectionWrapper::~OConnectionWrapper()
     101             : {
     102         138 :     if (m_xProxyConnection.is())
     103         138 :         m_xProxyConnection->setDelegator(NULL);
     104         138 : }
     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        1618 : Any SAL_CALL OConnectionWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
     142             : {
     143        1618 :     Any aReturn = OConnection_BASE::queryInterface(_rType);
     144        1618 :     return aReturn.hasValue() ? aReturn : (m_xProxyConnection.is() ? m_xProxyConnection->queryAggregation(_rType) : aReturn);
     145             : }
     146             : 
     147          20 : Sequence< Type > SAL_CALL OConnectionWrapper::getTypes(  ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     148             : {
     149             :     return ::comphelper::concatSequences(
     150             :         OConnection_BASE::getTypes(),
     151          20 :         m_xTypeProvider->getTypes()
     152          20 :     );
     153             : }
     154             : 
     155             : // com::sun::star::lang::XUnoTunnel
     156           6 : sal_Int64 SAL_CALL OConnectionWrapper::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException, std::exception)
     157             : {
     158           6 :     if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
     159           0 :         return reinterpret_cast< sal_Int64 >( this );
     160             : 
     161           6 :     if(m_xUnoTunnel.is())
     162           6 :         return m_xUnoTunnel->getSomething(rId);
     163           0 :     return 0;
     164             : }
     165             : 
     166             : 
     167           6 : Sequence< sal_Int8 > OConnectionWrapper::getUnoTunnelImplementationId()
     168             : {
     169             :     static ::cppu::OImplementationId * pId = 0;
     170           6 :     if (! pId)
     171             :     {
     172           2 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     173           2 :         if (! pId)
     174             :         {
     175           2 :             static ::cppu::OImplementationId aId;
     176           2 :             pId = &aId;
     177           2 :         }
     178             :     }
     179           6 :     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          70 :         TPropertyValueLessFunctor()
     188          70 :         {}
     189       19880 :         bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
     190             :         {
     191       19880 :             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          70 : 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          70 :     rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
     207          70 :     rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
     208          70 :     if ( !_rUserName.isEmpty() )
     209           0 :         rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
     210          70 :     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          70 :     PropertyValue* pBegin = _rInfo.getArray();
     214          70 :     PropertyValue* pEnd   = pBegin + _rInfo.getLength();
     215          70 :     ::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
     216             : 
     217          70 :     pBegin = _rInfo.getArray();
     218          70 :     pEnd   = pBegin + _rInfo.getLength();
     219        3920 :     for (; pBegin != pEnd; ++pBegin)
     220             :     {
     221             :         // we only include strings an integer values
     222        3850 :         OUString sValue;
     223        3850 :         if ( pBegin->Value >>= sValue )
     224             :             ;
     225             :         else
     226             :         {
     227        2170 :             sal_Int32 nValue = 0;
     228        2170 :             if ( pBegin->Value >>= nValue )
     229         280 :                 sValue = OUString::number(nValue);
     230             :             else
     231             :             {
     232        1890 :                 Sequence< OUString> aSeq;
     233        1890 :                 if ( pBegin->Value >>= aSeq )
     234             :                 {
     235         210 :                     const OUString* pSBegin = aSeq.getConstArray();
     236         210 :                     const OUString* pSEnd   = pSBegin + aSeq.getLength();
     237         280 :                     for(;pSBegin != pSEnd;++pSBegin)
     238          70 :                         rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
     239        1890 :                 }
     240             :             }
     241             :         }
     242        3850 :         if ( !sValue.isEmpty() )
     243             :         {
     244             :             // we don't have to convert this into UTF8 because we don't store on a file system
     245         506 :             rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
     246             :         }
     247        3850 :     }
     248             : 
     249          70 :     rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
     250             :     // we have to destroy the digest
     251          70 :     rtl_digest_destroy(aDigest);
     252          70 : }
     253             : 
     254             : 
     255             : 
     256             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10