Branch data 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/XProxyFactory.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 : 112 : OConnectionWrapper::OConnectionWrapper()
41 : : {
42 : :
43 : 112 : }
44 : : // -----------------------------------------------------------------------------
45 : 112 : void OConnectionWrapper::setDelegation(Reference< XAggregation >& _rxProxyConnection,oslInterlockedCount& _rRefCount)
46 : : {
47 : : OSL_ENSURE(_rxProxyConnection.is(),"OConnectionWrapper: Connection must be valid!");
48 : 112 : osl_incrementInterlockedCount( &_rRefCount );
49 [ + - ]: 112 : if (_rxProxyConnection.is())
50 : : {
51 : : // transfer the (one and only) real ref to the aggregate to our member
52 [ + - ]: 112 : m_xProxyConnection = _rxProxyConnection;
53 [ + - ]: 112 : _rxProxyConnection = NULL;
54 [ + - ]: 112 : ::comphelper::query_aggregation(m_xProxyConnection,m_xConnection);
55 [ + - ]: 112 : m_xTypeProvider.set(m_xConnection,UNO_QUERY);
56 [ + - ]: 112 : m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
57 [ + - ]: 112 : m_xServiceInfo.set(m_xConnection,UNO_QUERY);
58 : :
59 : : // set ourself as delegator
60 [ + - ]: 112 : Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
61 [ + - ][ + - ]: 112 : m_xProxyConnection->setDelegator( xIf );
62 : :
63 : : }
64 : 112 : osl_decrementInterlockedCount( &_rRefCount );
65 : 112 : }
66 : : // -----------------------------------------------------------------------------
67 : 0 : void OConnectionWrapper::setDelegation(const Reference< XConnection >& _xConnection
68 : : ,const Reference< XMultiServiceFactory>& _xORB
69 : : ,oslInterlockedCount& _rRefCount)
70 : : {
71 : : OSL_ENSURE(_xConnection.is(),"OConnectionWrapper: Connection must be valid!");
72 [ # # ]: 0 : osl_incrementInterlockedCount( &_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(_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.reflection.ProxyFactory"))),UNO_QUERY);
[ # # ][ # # ]
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_decrementInterlockedCount( &_rRefCount );
92 : 0 : }
93 : : // -----------------------------------------------------------------------------
94 : 112 : void OConnectionWrapper::disposing()
95 : : {
96 : 112 : m_xConnection.clear();
97 : 112 : }
98 : : //-----------------------------------------------------------------------------
99 : 112 : OConnectionWrapper::~OConnectionWrapper()
100 : : {
101 [ + - ]: 112 : if (m_xProxyConnection.is())
102 [ + - ][ + - ]: 112 : m_xProxyConnection->setDelegator(NULL);
[ + - ]
103 [ - + ]: 112 : }
104 : :
105 : : // XServiceInfo
106 : : // --------------------------------------------------------------------------------
107 : 0 : ::rtl::OUString SAL_CALL OConnectionWrapper::getImplementationName( ) throw (::com::sun::star::uno::RuntimeException)
108 : : {
109 : 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdbc.drivers.OConnectionWrapper" ) );
110 : : }
111 : :
112 : : // --------------------------------------------------------------------------------
113 : 0 : ::com::sun::star::uno::Sequence< ::rtl::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< ::rtl::OUString > aSupported;
117 [ # # ]: 0 : if ( m_xServiceInfo.is() )
118 [ # # ][ # # ]: 0 : aSupported = m_xServiceInfo->getSupportedServiceNames();
[ # # ][ # # ]
119 : :
120 : : // append our own service, if necessary
121 [ # # ]: 0 : ::rtl::OUString sConnectionService( RTL_CONSTASCII_USTRINGPARAM( "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 ::rtl::OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException)
135 : : {
136 [ # # ][ # # ]: 0 : return ::comphelper::findValue( getSupportedServiceNames(), _rServiceName, sal_True ).getLength() != 0;
137 : : }
138 : :
139 : : // --------------------------------------------------------------------------------
140 : 1116 : Any SAL_CALL OConnectionWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
141 : : {
142 [ + - ]: 1116 : Any aReturn = OConnection_BASE::queryInterface(_rType);
143 [ + + ][ + - ]: 1116 : return aReturn.hasValue() ? aReturn : (m_xProxyConnection.is() ? m_xProxyConnection->queryAggregation(_rType) : aReturn);
[ + - ][ + - ]
144 : : }
145 : : // --------------------------------------------------------------------------------
146 : 4 : Sequence< Type > SAL_CALL OConnectionWrapper::getTypes( ) throw (::com::sun::star::uno::RuntimeException)
147 : : {
148 : : return ::comphelper::concatSequences(
149 : : OConnection_BASE::getTypes(),
150 : 4 : m_xTypeProvider->getTypes()
151 [ + - ][ + - ]: 4 : );
[ + - ]
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 : 56 : TPropertyValueLessFunctor()
187 : 56 : {}
188 : 10640 : bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
189 : : {
190 : 10640 : 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 : 56 : void OConnectionWrapper::createUniqueId( const ::rtl::OUString& _rURL
199 : : ,Sequence< PropertyValue >& _rInfo
200 : : ,sal_uInt8* _pBuffer
201 : : ,const ::rtl::OUString& _rUserName
202 : : ,const ::rtl::OUString& _rPassword)
203 : : {
204 : : // first we create the digest we want to have
205 : 56 : rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
206 : 56 : rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
207 [ - + ]: 56 : if ( !_rUserName.isEmpty() )
208 : 0 : rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
209 [ - + ]: 56 : 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 : 56 : PropertyValue* pBegin = _rInfo.getArray();
213 : 56 : PropertyValue* pEnd = pBegin + _rInfo.getLength();
214 [ + - ]: 56 : ::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
215 : :
216 : 56 : pBegin = _rInfo.getArray();
217 : 56 : pEnd = pBegin + _rInfo.getLength();
218 [ + + ]: 3136 : for (; pBegin != pEnd; ++pBegin)
219 : : {
220 : : // we only include strings an integer values
221 : 3080 : ::rtl::OUString sValue;
222 [ + + ]: 3080 : if ( pBegin->Value >>= sValue )
223 : : ;
224 : : else
225 : : {
226 : 1736 : sal_Int32 nValue = 0;
227 [ + + ]: 1736 : if ( pBegin->Value >>= nValue )
228 : 224 : sValue = ::rtl::OUString::valueOf(nValue);
229 : : else
230 : : {
231 [ + - ]: 1512 : Sequence< ::rtl::OUString> aSeq;
232 [ + - ][ + + ]: 1512 : if ( pBegin->Value >>= aSeq )
233 : : {
234 : 168 : const ::rtl::OUString* pSBegin = aSeq.getConstArray();
235 : 168 : const ::rtl::OUString* pSEnd = pSBegin + aSeq.getLength();
236 [ + + ]: 224 : for(;pSBegin != pSEnd;++pSBegin)
237 : 56 : rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
238 [ + - ]: 1736 : }
239 : : }
240 : : }
241 [ + + ]: 3080 : if ( !sValue.isEmpty() )
242 : : {
243 : : // we don't have to convert this into UTF8 because we don't store on a file system
244 : 406 : rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
245 : : }
246 : 3080 : }
247 : :
248 : 56 : rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
249 : : // we have to destroy the digest
250 : 56 : rtl_digest_destroy(aDigest);
251 : 56 : }
252 : : // -----------------------------------------------------------------------------
253 : :
254 : :
255 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|