1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew 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 // --------------------------------------------------------------------------------
49cdf0e10cSrcweir OConnectionWrapper::OConnectionWrapper()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir }
53cdf0e10cSrcweir // -----------------------------------------------------------------------------
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 // -----------------------------------------------------------------------------
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 // -----------------------------------------------------------------------------
103cdf0e10cSrcweir void OConnectionWrapper::disposing()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir m_xConnection.clear();
106cdf0e10cSrcweir }
107cdf0e10cSrcweir //-----------------------------------------------------------------------------
108cdf0e10cSrcweir OConnectionWrapper::~OConnectionWrapper()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	if (m_xProxyConnection.is())
111cdf0e10cSrcweir 		m_xProxyConnection->setDelegator(NULL);
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir // XServiceInfo
115cdf0e10cSrcweir // --------------------------------------------------------------------------------
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 // --------------------------------------------------------------------------------
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 // --------------------------------------------------------------------------------
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 // --------------------------------------------------------------------------------
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 // --------------------------------------------------------------------------------
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
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 // -----------------------------------------------------------------------------
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:
195cdf0e10cSrcweir 		TPropertyValueLessFunctor()
196cdf0e10cSrcweir 		{}
197cdf0e10cSrcweir 		bool operator() (const ::com::sun::star::beans::PropertyValue& lhs, const ::com::sun::star::beans::PropertyValue& rhs) const
198cdf0e10cSrcweir 		{
199cdf0e10cSrcweir 			return !!(lhs.Name.equalsIgnoreAsciiCase( rhs.Name ));
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 	};
202cdf0e10cSrcweir 
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir // -----------------------------------------------------------------------------
206cdf0e10cSrcweir // creates a unique id out of the url and sequence of properties
207cdf0e10cSrcweir void OConnectionWrapper::createUniqueId( const ::rtl::OUString& _rURL
208cdf0e10cSrcweir 					,Sequence< PropertyValue >& _rInfo
209cdf0e10cSrcweir 					,sal_uInt8* _pBuffer
210cdf0e10cSrcweir 					,const ::rtl::OUString& _rUserName
211cdf0e10cSrcweir 					,const ::rtl::OUString& _rPassword)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	// first we create the digest we want to have
214cdf0e10cSrcweir 	rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
215cdf0e10cSrcweir 	rtlDigestError aError = rtl_digest_update(aDigest,_rURL.getStr(),_rURL.getLength()*sizeof(sal_Unicode));
216cdf0e10cSrcweir 	if ( _rUserName.getLength() )
217cdf0e10cSrcweir 		aError = rtl_digest_update(aDigest,_rUserName.getStr(),_rUserName.getLength()*sizeof(sal_Unicode));
218cdf0e10cSrcweir 	if ( _rPassword.getLength() )
219cdf0e10cSrcweir 		aError = rtl_digest_update(aDigest,_rPassword.getStr(),_rPassword.getLength()*sizeof(sal_Unicode));
220cdf0e10cSrcweir 	// now we need to sort the properties
221cdf0e10cSrcweir 	PropertyValue* pBegin = _rInfo.getArray();
222cdf0e10cSrcweir 	PropertyValue* pEnd   = pBegin + _rInfo.getLength();
223cdf0e10cSrcweir 	::std::sort(pBegin,pEnd,TPropertyValueLessFunctor());
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	pBegin = _rInfo.getArray();
226cdf0e10cSrcweir 	pEnd   = pBegin + _rInfo.getLength();
227cdf0e10cSrcweir 	for (; pBegin != pEnd; ++pBegin)
228cdf0e10cSrcweir 	{
229cdf0e10cSrcweir 		// we only include strings an integer values
230cdf0e10cSrcweir 		::rtl::OUString sValue;
231cdf0e10cSrcweir 		if ( pBegin->Value >>= sValue )
232cdf0e10cSrcweir 			;
233cdf0e10cSrcweir 		else
234cdf0e10cSrcweir 		{
235cdf0e10cSrcweir 			sal_Int32 nValue = 0;
236cdf0e10cSrcweir 			if ( pBegin->Value >>= nValue )
237cdf0e10cSrcweir 				sValue = ::rtl::OUString::valueOf(nValue);
238cdf0e10cSrcweir 			else
239cdf0e10cSrcweir 			{
240cdf0e10cSrcweir 				Sequence< ::rtl::OUString> aSeq;
241cdf0e10cSrcweir 				if ( pBegin->Value >>= aSeq )
242cdf0e10cSrcweir 				{
243cdf0e10cSrcweir 					const ::rtl::OUString* pSBegin = aSeq.getConstArray();
244cdf0e10cSrcweir 					const ::rtl::OUString* pSEnd   = pSBegin + aSeq.getLength();
245cdf0e10cSrcweir 					for(;pSBegin != pSEnd;++pSBegin)
246cdf0e10cSrcweir 						aError = rtl_digest_update(aDigest,pSBegin->getStr(),pSBegin->getLength()*sizeof(sal_Unicode));
247cdf0e10cSrcweir 				}
248cdf0e10cSrcweir 			}
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 		if ( sValue.getLength() > 0 )
251cdf0e10cSrcweir 		{
252cdf0e10cSrcweir 			// we don't have to convert this into UTF8 because we don't store on a file system
253cdf0e10cSrcweir 			aError = rtl_digest_update(aDigest,sValue.getStr(),sValue.getLength()*sizeof(sal_Unicode));
254cdf0e10cSrcweir 		}
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	aError = rtl_digest_get(aDigest,_pBuffer,RTL_DIGEST_LENGTH_SHA1);
258cdf0e10cSrcweir 	// we have to destroy the digest
259cdf0e10cSrcweir 	rtl_digest_destroy(aDigest);
260cdf0e10cSrcweir }
261cdf0e10cSrcweir // -----------------------------------------------------------------------------
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 
264