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