19b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59b5730f6SAndrew Rist  * distributed with this work for additional information
69b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
99b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
109b5730f6SAndrew Rist  *
119b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129b5730f6SAndrew Rist  *
139b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149b5730f6SAndrew Rist  * software distributed under the License is distributed on an
159b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
179b5730f6SAndrew Rist  * specific language governing permissions and limitations
189b5730f6SAndrew Rist  * under the License.
199b5730f6SAndrew Rist  *
209b5730f6SAndrew Rist  *************************************************************/
219b5730f6SAndrew Rist 
229b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #ifndef _CONNECTIVITY_CONNECTIONWRAPPER_HXX_
27cdf0e10cSrcweir #include "connectivity/ConnectionWrapper.hxx"
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
32cdf0e10cSrcweir #include <comphelper/uno3.hxx>
33cdf0e10cSrcweir #include <comphelper/sequence.hxx>
34cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
35cdf0e10cSrcweir #include <com/sun/star/reflection/XProxyFactory.hpp>
36cdf0e10cSrcweir #include <rtl/digest.h>
37cdf0e10cSrcweir #include <algorithm>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <algorithm>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace connectivity;
42cdf0e10cSrcweir //------------------------------------------------------------------------------
43cdf0e10cSrcweir using namespace com::sun::star::uno;
44cdf0e10cSrcweir using namespace com::sun::star::lang;
45cdf0e10cSrcweir using namespace com::sun::star::beans;
46cdf0e10cSrcweir using namespace com::sun::star::sdbc;
47cdf0e10cSrcweir using namespace ::com::sun::star::reflection;
48cdf0e10cSrcweir // --------------------------------------------------------------------------------
OConnectionWrapper()49cdf0e10cSrcweir OConnectionWrapper::OConnectionWrapper()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir }
53cdf0e10cSrcweir // -----------------------------------------------------------------------------
setDelegation(Reference<XAggregation> & _rxProxyConnection,oslInterlockedCount & _rRefCount)54cdf0e10cSrcweir void OConnectionWrapper::setDelegation(Reference< XAggregation >& _rxProxyConnection,oslInterlockedCount& _rRefCount)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	OSL_ENSURE(_rxProxyConnection.is(),"OConnectionWrapper: Connection must be valid!");
57cdf0e10cSrcweir 	osl_incrementInterlockedCount( &_rRefCount );
58cdf0e10cSrcweir 	if (_rxProxyConnection.is())
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		// transfer the (one and only) real ref to the aggregate to our member
61cdf0e10cSrcweir 		m_xProxyConnection = _rxProxyConnection;
62cdf0e10cSrcweir 		_rxProxyConnection = NULL;
63cdf0e10cSrcweir 		::comphelper::query_aggregation(m_xProxyConnection,m_xConnection);
64cdf0e10cSrcweir 		m_xTypeProvider.set(m_xConnection,UNO_QUERY);
65cdf0e10cSrcweir 		m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
66cdf0e10cSrcweir 		m_xServiceInfo.set(m_xConnection,UNO_QUERY);
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 		// set ourself as delegator
69cdf0e10cSrcweir 		Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
70cdf0e10cSrcweir 		m_xProxyConnection->setDelegator( xIf );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 	osl_decrementInterlockedCount( &_rRefCount );
74cdf0e10cSrcweir }
75cdf0e10cSrcweir // -----------------------------------------------------------------------------
setDelegation(const Reference<XConnection> & _xConnection,const Reference<XMultiServiceFactory> & _xORB,oslInterlockedCount & _rRefCount)76cdf0e10cSrcweir void OConnectionWrapper::setDelegation(const Reference< XConnection >& _xConnection
77cdf0e10cSrcweir 									   ,const Reference< XMultiServiceFactory>& _xORB
78cdf0e10cSrcweir 									   ,oslInterlockedCount& _rRefCount)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	OSL_ENSURE(_xConnection.is(),"OConnectionWrapper: Connection must be valid!");
81cdf0e10cSrcweir 	osl_incrementInterlockedCount( &_rRefCount );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	m_xConnection = _xConnection;
84cdf0e10cSrcweir 	m_xTypeProvider.set(m_xConnection,UNO_QUERY);
85cdf0e10cSrcweir 	m_xUnoTunnel.set(m_xConnection,UNO_QUERY);
86cdf0e10cSrcweir 	m_xServiceInfo.set(m_xConnection,UNO_QUERY);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	Reference< XProxyFactory >	xProxyFactory(_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.reflection.ProxyFactory"))),UNO_QUERY);
89cdf0e10cSrcweir 	Reference< XAggregation > xConProxy = xProxyFactory->createProxy(_xConnection);
90cdf0e10cSrcweir 	if (xConProxy.is())
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		// transfer the (one and only) real ref to the aggregate to our member
93cdf0e10cSrcweir 		m_xProxyConnection = xConProxy;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		// set ourself as delegator
96cdf0e10cSrcweir 		Reference<XInterface> xIf = static_cast< XUnoTunnel* >( this );
97cdf0e10cSrcweir 		m_xProxyConnection->setDelegator( xIf );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	}
100cdf0e10cSrcweir 	osl_decrementInterlockedCount( &_rRefCount );
101cdf0e10cSrcweir }
102cdf0e10cSrcweir // -----------------------------------------------------------------------------
disposing()103cdf0e10cSrcweir void OConnectionWrapper::disposing()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir m_xConnection.clear();
106cdf0e10cSrcweir }
107cdf0e10cSrcweir //-----------------------------------------------------------------------------
~OConnectionWrapper()108cdf0e10cSrcweir OConnectionWrapper::~OConnectionWrapper()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	if (m_xProxyConnection.is())
111cdf0e10cSrcweir 		m_xProxyConnection->setDelegator(NULL);
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir // XServiceInfo
115cdf0e10cSrcweir // --------------------------------------------------------------------------------
getImplementationName()116cdf0e10cSrcweir ::rtl::OUString SAL_CALL OConnectionWrapper::getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdbc.drivers.OConnectionWrapper" ) );
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir // --------------------------------------------------------------------------------
getSupportedServiceNames()122cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL OConnectionWrapper::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	// first collect the services which are supported by our aggregate
125cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSupported;
126cdf0e10cSrcweir 	if ( m_xServiceInfo.is() )
127cdf0e10cSrcweir 		aSupported = m_xServiceInfo->getSupportedServiceNames();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	// append our own service, if necessary
130cdf0e10cSrcweir 	::rtl::OUString sConnectionService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdbc.Connection" ) );
131cdf0e10cSrcweir 	if ( 0 == ::comphelper::findValue( aSupported, sConnectionService, sal_True ).getLength() )
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		sal_Int32 nLen = aSupported.getLength();
134cdf0e10cSrcweir 		aSupported.realloc( nLen + 1 );
135cdf0e10cSrcweir 		aSupported[ nLen ] = sConnectionService;
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	// outta here
139cdf0e10cSrcweir 	return aSupported;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir // --------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)143cdf0e10cSrcweir sal_Bool SAL_CALL OConnectionWrapper::supportsService( const ::rtl::OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	return ::comphelper::findValue( getSupportedServiceNames(), _rServiceName, sal_True ).getLength() != 0;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir // --------------------------------------------------------------------------------
queryInterface(const Type & _rType)149cdf0e10cSrcweir Any SAL_CALL OConnectionWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	Any aReturn = OConnection_BASE::queryInterface(_rType);
152cdf0e10cSrcweir 	return aReturn.hasValue() ? aReturn : (m_xProxyConnection.is() ? m_xProxyConnection->queryAggregation(_rType) : aReturn);
153cdf0e10cSrcweir }
154cdf0e10cSrcweir // --------------------------------------------------------------------------------
getTypes()155cdf0e10cSrcweir Sequence< Type > SAL_CALL OConnectionWrapper::getTypes(  ) throw (::com::sun::star::uno::RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	return ::comphelper::concatSequences(
158cdf0e10cSrcweir 		OConnection_BASE::getTypes(),
159cdf0e10cSrcweir 		m_xTypeProvider->getTypes()
160cdf0e10cSrcweir 	);
161cdf0e10cSrcweir }
162cdf0e10cSrcweir // -----------------------------------------------------------------------------
163cdf0e10cSrcweir // com::sun::star::lang::XUnoTunnel
getSomething(const Sequence<sal_Int8> & rId)164cdf0e10cSrcweir sal_Int64 SAL_CALL OConnectionWrapper::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	if (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
167cdf0e10cSrcweir 		return reinterpret_cast< sal_Int64 >( this );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	if(m_xUnoTunnel.is())
170cdf0e10cSrcweir 		return m_xUnoTunnel->getSomething(rId);
171cdf0e10cSrcweir 	return 0;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir // -----------------------------------------------------------------------------
getUnoTunnelImplementationId()175cdf0e10cSrcweir Sequence< sal_Int8 > OConnectionWrapper::getUnoTunnelImplementationId()
176cdf0e10cSrcweir {
177cdf0e10cSrcweir 	static ::cppu::OImplementationId * pId = 0;
178cdf0e10cSrcweir 	if (! pId)
179cdf0e10cSrcweir 	{
180cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
181cdf0e10cSrcweir 		if (! pId)
182cdf0e10cSrcweir 		{
183cdf0e10cSrcweir 			static ::cppu::OImplementationId aId;
184cdf0e10cSrcweir 			pId = &aId;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 	}
187cdf0e10cSrcweir 	return pId->getImplementationId();
188cdf0e10cSrcweir }
189cdf0e10cSrcweir // -----------------------------------------------------------------------------
190cdf0e10cSrcweir namespace
191cdf0e10cSrcweir {
192cdf0e10cSrcweir 	class TPropertyValueLessFunctor : public ::std::binary_function< ::com::sun::star::beans::PropertyValue,::com::sun::star::beans::PropertyValue,bool>
193cdf0e10cSrcweir 	{
194cdf0e10cSrcweir 	public:
TPropertyValueLessFunctor()195cdf0e10cSrcweir 		TPropertyValueLessFunctor()
196cdf0e10cSrcweir 		{}
operator ()(const::com::sun::star::beans::PropertyValue & lhs,const::com::sun::star::beans::PropertyValue & rhs) const197cdf0e10cSrcweir 		bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
198cdf0e10cSrcweir 		{
199*bc4f48a7SHerbert Dürr 			const rtl_uString* l = lhs.Name.pData;
200*bc4f48a7SHerbert Dürr 			const rtl_uString* r = rhs.Name.pData;
201*bc4f48a7SHerbert Dürr 			const int c = rtl_ustr_compareIgnoreAsciiCase_WithLength( l->buffer, l->length, r->buffer, r->length );
202*bc4f48a7SHerbert Dürr 			return (c < 0);
203cdf0e10cSrcweir 		}
204cdf0e10cSrcweir 	};
205cdf0e10cSrcweir 
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir // -----------------------------------------------------------------------------
209cdf0e10cSrcweir // creates a unique id out of the url and sequence of properties
createUniqueId(const::rtl::OUString & _rURL,Sequence<PropertyValue> & _rInfo,sal_uInt8 * _pBuffer,const::rtl::OUString & _rUserName,const::rtl::OUString & _rPassword)210cdf0e10cSrcweir void OConnectionWrapper::createUniqueId( const ::rtl::OUString& _rURL
211cdf0e10cSrcweir 					,Sequence< PropertyValue >& _rInfo
212cdf0e10cSrcweir 					,sal_uInt8* _pBuffer
213cdf0e10cSrcweir 					,const ::rtl::OUString& _rUserName
214cdf0e10cSrcweir 					,const ::rtl::OUString& _rPassword)
215cdf0e10cSrcweir {
216cdf0e10cSrcweir 	// first we create the digest we want to have
217cdf0e10cSrcweir 	rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
218cdf0e10cSrcweir 	rtlDigestError aError = rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
219cdf0e10cSrcweir 	if ( _rUserName.getLength() )
220cdf0e10cSrcweir 		aError = rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
221cdf0e10cSrcweir 	if ( _rPassword.getLength() )
222cdf0e10cSrcweir 		aError = rtl_digest_update(aDigest,_rPassword.getStr(),_rPassword.getLength()*sizeof(sal_Unicode));
223cdf0e10cSrcweir 	// now we need to sort the properties
224cdf0e10cSrcweir 	PropertyValue* pBegin = _rInfo.getArray();
225cdf0e10cSrcweir 	PropertyValue* pEnd   = pBegin + _rInfo.getLength();
226cdf0e10cSrcweir 	::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	pBegin = _rInfo.getArray();
229cdf0e10cSrcweir 	pEnd   = pBegin + _rInfo.getLength();
230cdf0e10cSrcweir 	for (; pBegin != pEnd; ++pBegin)
231cdf0e10cSrcweir 	{
232cdf0e10cSrcweir 		// we only include strings an integer values
233cdf0e10cSrcweir 		::rtl::OUString sValue;
234cdf0e10cSrcweir 		if ( pBegin->Value >>= sValue )
235cdf0e10cSrcweir 			;
236cdf0e10cSrcweir 		else
237cdf0e10cSrcweir 		{
238cdf0e10cSrcweir 			sal_Int32 nValue = 0;
239cdf0e10cSrcweir 			if ( pBegin->Value >>= nValue )
240cdf0e10cSrcweir 				sValue = ::rtl::OUString::valueOf(nValue);
241cdf0e10cSrcweir 			else
242cdf0e10cSrcweir 			{
243cdf0e10cSrcweir 				Sequence< ::rtl::OUString> aSeq;
244cdf0e10cSrcweir 				if ( pBegin->Value >>= aSeq )
245cdf0e10cSrcweir 				{
246cdf0e10cSrcweir 					const ::rtl::OUString* pSBegin = aSeq.getConstArray();
247cdf0e10cSrcweir 					const ::rtl::OUString* pSEnd   = pSBegin + aSeq.getLength();
248cdf0e10cSrcweir 					for(;pSBegin != pSEnd;++pSBegin)
249cdf0e10cSrcweir 						aError = rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
250cdf0e10cSrcweir 				}
251cdf0e10cSrcweir 			}
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 		if ( sValue.getLength() > 0 )
254cdf0e10cSrcweir 		{
255cdf0e10cSrcweir 			// we don't have to convert this into UTF8 because we don't store on a file system
256cdf0e10cSrcweir 			aError = rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
257cdf0e10cSrcweir 		}
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	aError = rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
261cdf0e10cSrcweir 	// we have to destroy the digest
262cdf0e10cSrcweir 	rtl_digest_destroy(aDigest);
263cdf0e10cSrcweir }
264cdf0e10cSrcweir // -----------------------------------------------------------------------------
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 
267