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