Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / ConnectionWrapper.cxx
bloba3529b4a63a9ded8919ca039f96aeb9ca66b32d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
33 using namespace connectivity;
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;
41 OConnectionWrapper::OConnectionWrapper()
46 void OConnectionWrapper::setDelegation(Reference< XAggregation >& _rxProxyConnection,oslInterlockedCount& _rRefCount)
48 OSL_ENSURE(_rxProxyConnection.is(),"OConnectionWrapper: Connection must be valid!");
49 osl_atomic_increment( &_rRefCount );
50 if (_rxProxyConnection.is())
52 // transfer the (one and only) real ref to the aggregate to our member
53 m_xProxyConnection = _rxProxyConnection;
54 _rxProxyConnection = NULL;
55 ::comphelper::query_aggregation(m_xProxyConnection,m_xConnection);
56 m_xTypeProvider.set(m_xConnection,UNO_QUERY);
57 m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
58 m_xServiceInfo.set(m_xConnection,UNO_QUERY);
60 // set ourself as delegator
61 Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
62 m_xProxyConnection->setDelegator( xIf );
65 osl_atomic_decrement( &_rRefCount );
68 void OConnectionWrapper::setDelegation(const Reference< XConnection >& _xConnection
69 ,const Reference< XComponentContext>& _rxContext
70 ,oslInterlockedCount& _rRefCount)
72 OSL_ENSURE(_xConnection.is(),"OConnectionWrapper: Connection must be valid!");
73 osl_atomic_increment( &_rRefCount );
75 m_xConnection = _xConnection;
76 m_xTypeProvider.set(m_xConnection,UNO_QUERY);
77 m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
78 m_xServiceInfo.set(m_xConnection,UNO_QUERY);
80 Reference< XProxyFactory > xProxyFactory = ProxyFactory::create( _rxContext );
81 Reference< XAggregation > xConProxy = xProxyFactory->createProxy(_xConnection);
82 if (xConProxy.is())
84 // transfer the (one and only) real ref to the aggregate to our member
85 m_xProxyConnection = xConProxy;
87 // set ourself as delegator
88 Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
89 m_xProxyConnection->setDelegator( xIf );
92 osl_atomic_decrement( &_rRefCount );
95 void OConnectionWrapper::disposing()
97 m_xConnection.clear();
100 OConnectionWrapper::~OConnectionWrapper()
102 if (m_xProxyConnection.is())
103 m_xProxyConnection->setDelegator(NULL);
106 // XServiceInfo
108 OUString SAL_CALL OConnectionWrapper::getImplementationName( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
110 return OUString( "com.sun.star.sdbc.drivers.OConnectionWrapper" );
114 ::com::sun::star::uno::Sequence< OUString > SAL_CALL OConnectionWrapper::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
116 // first collect the services which are supported by our aggregate
117 Sequence< OUString > aSupported;
118 if ( m_xServiceInfo.is() )
119 aSupported = m_xServiceInfo->getSupportedServiceNames();
121 // append our own service, if necessary
122 OUString sConnectionService( "com.sun.star.sdbc.Connection" );
123 if ( 0 == ::comphelper::findValue( aSupported, sConnectionService, true ).getLength() )
125 sal_Int32 nLen = aSupported.getLength();
126 aSupported.realloc( nLen + 1 );
127 aSupported[ nLen ] = sConnectionService;
130 // outta here
131 return aSupported;
135 sal_Bool SAL_CALL OConnectionWrapper::supportsService( const OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
137 return cppu::supportsService(this, _rServiceName);
141 Any SAL_CALL OConnectionWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
143 Any aReturn = OConnection_BASE::queryInterface(_rType);
144 return aReturn.hasValue() ? aReturn : (m_xProxyConnection.is() ? m_xProxyConnection->queryAggregation(_rType) : aReturn);
147 Sequence< Type > SAL_CALL OConnectionWrapper::getTypes( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
149 return ::comphelper::concatSequences(
150 OConnection_BASE::getTypes(),
151 m_xTypeProvider->getTypes()
155 // com::sun::star::lang::XUnoTunnel
156 sal_Int64 SAL_CALL OConnectionWrapper::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException, std::exception)
158 if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
159 return reinterpret_cast< sal_Int64 >( this );
161 if(m_xUnoTunnel.is())
162 return m_xUnoTunnel->getSomething(rId);
163 return 0;
167 Sequence< sal_Int8 > OConnectionWrapper::getUnoTunnelImplementationId()
169 static ::cppu::OImplementationId * pId = 0;
170 if (! pId)
172 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
173 if (! pId)
175 static ::cppu::OImplementationId aId;
176 pId = &aId;
179 return pId->getImplementationId();
182 namespace
184 class TPropertyValueLessFunctor : public ::std::binary_function< ::com::sun::star::beans::PropertyValue,::com::sun::star::beans::PropertyValue,bool>
186 public:
187 TPropertyValueLessFunctor()
189 bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
191 return lhs.Name.compareToIgnoreAsciiCase(rhs.Name) < 0;
198 // creates a unique id out of the url and sequence of properties
199 void OConnectionWrapper::createUniqueId( const OUString& _rURL
200 ,Sequence< PropertyValue >& _rInfo
201 ,sal_uInt8* _pBuffer
202 ,const OUString& _rUserName
203 ,const OUString& _rPassword)
205 // first we create the digest we want to have
206 rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
207 rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
208 if ( !_rUserName.isEmpty() )
209 rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
210 if ( !_rPassword.isEmpty() )
211 rtl_digest_update(aDigest,_rPassword.getStr(),_rPassword.getLength()*sizeof(sal_Unicode));
212 // now we need to sort the properties
213 PropertyValue* pBegin = _rInfo.getArray();
214 PropertyValue* pEnd = pBegin + _rInfo.getLength();
215 ::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
217 pBegin = _rInfo.getArray();
218 pEnd = pBegin + _rInfo.getLength();
219 for (; pBegin != pEnd; ++pBegin)
221 // we only include strings an integer values
222 OUString sValue;
223 if ( pBegin->Value >>= sValue )
225 else
227 sal_Int32 nValue = 0;
228 if ( pBegin->Value >>= nValue )
229 sValue = OUString::number(nValue);
230 else
232 Sequence< OUString> aSeq;
233 if ( pBegin->Value >>= aSeq )
235 const OUString* pSBegin = aSeq.getConstArray();
236 const OUString* pSEnd = pSBegin + aSeq.getLength();
237 for(;pSBegin != pSEnd;++pSBegin)
238 rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
242 if ( !sValue.isEmpty() )
244 // we don't have to convert this into UTF8 because we don't store on a file system
245 rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
249 rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
250 // we have to destroy the digest
251 rtl_digest_destroy(aDigest);
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */