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