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