1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir #include <connectivity/conncleanup.hxx> 31*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 33*cdf0e10cSrcweir #include <osl/diagnose.h> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir //......................................................................... 36*cdf0e10cSrcweir namespace dbtools 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir //......................................................................... 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 41*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 42*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 43*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir //===================================================================== 46*cdf0e10cSrcweir static const ::rtl::OUString& getActiveConnectionPropertyName() 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir static const ::rtl::OUString s_sActiveConnectionPropertyName = ::rtl::OUString::createFromAscii("ActiveConnection"); 49*cdf0e10cSrcweir return s_sActiveConnectionPropertyName; 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir //===================================================================== 53*cdf0e10cSrcweir //= OAutoConnectionDisposer 54*cdf0e10cSrcweir //===================================================================== 55*cdf0e10cSrcweir //--------------------------------------------------------------------- 56*cdf0e10cSrcweir OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection) 57*cdf0e10cSrcweir :m_xRowSet( _rxRowSet ) 58*cdf0e10cSrcweir ,m_bRSListening( sal_False ) 59*cdf0e10cSrcweir ,m_bPropertyListening( sal_False ) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY); 62*cdf0e10cSrcweir OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!"); 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir if (!xProps.is()) 65*cdf0e10cSrcweir return; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir try 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir xProps->setPropertyValue( getActiveConnectionPropertyName(), makeAny( _rxConnection ) ); 70*cdf0e10cSrcweir m_xOriginalConnection = _rxConnection; 71*cdf0e10cSrcweir startPropertyListening( xProps ); 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir catch( const Exception& ) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" ); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir //--------------------------------------------------------------------- 80*cdf0e10cSrcweir void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir try 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir _rxRowSet->addPropertyChangeListener( getActiveConnectionPropertyName(), this ); 85*cdf0e10cSrcweir m_bPropertyListening = sal_True; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir catch( const Exception& ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAutoConnectionDisposer::startPropertyListening: caught an exception!" ); 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir //--------------------------------------------------------------------- 94*cdf0e10cSrcweir void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource ) 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir // prevent deletion of ourself while we're herein 97*cdf0e10cSrcweir Reference< XInterface > xKeepAlive(static_cast< XWeak* >(this)); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir try 100*cdf0e10cSrcweir { // remove ourself as property change listener 101*cdf0e10cSrcweir OSL_ENSURE( _rxEventSource.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" ); 102*cdf0e10cSrcweir if ( _rxEventSource.is() ) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir _rxEventSource->removePropertyChangeListener( getActiveConnectionPropertyName(), this ); 105*cdf0e10cSrcweir m_bPropertyListening = sal_False; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir catch( const Exception& ) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" ); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir //--------------------------------------------------------------------- 115*cdf0e10cSrcweir void OAutoConnectionDisposer::startRowSetListening() 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" ); 118*cdf0e10cSrcweir try 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir if ( !m_bRSListening ) 121*cdf0e10cSrcweir m_xRowSet->addRowSetListener( this ); 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir catch( const Exception& ) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAutoConnectionDisposer::startRowSetListening: caught an exception!" ); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir m_bRSListening = sal_True; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir //--------------------------------------------------------------------- 131*cdf0e10cSrcweir void OAutoConnectionDisposer::stopRowSetListening() 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" ); 134*cdf0e10cSrcweir try 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir m_xRowSet->removeRowSetListener( this ); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir catch( const Exception& ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" ); 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir m_bRSListening = sal_False; 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir //--------------------------------------------------------------------- 146*cdf0e10cSrcweir void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir if ( _rEvent.PropertyName.equals( getActiveConnectionPropertyName() ) ) 149*cdf0e10cSrcweir { // somebody set a new ActiveConnection 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir Reference< XConnection > xNewConnection; 152*cdf0e10cSrcweir _rEvent.NewValue >>= xNewConnection; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir if ( isRowSetListening() ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir // we're listening at the row set, this means that the row set does not have our 157*cdf0e10cSrcweir // m_xOriginalConnection as active connection anymore 158*cdf0e10cSrcweir // So there are two possibilities 159*cdf0e10cSrcweir // a. somebody sets a new connection which is not our original one 160*cdf0e10cSrcweir // b. somebody sets a new connection, which is exactly the original one 161*cdf0e10cSrcweir // a. we're not interested in a, but in b: In this case, we simply need to move to the state 162*cdf0e10cSrcweir // we had originally: listen for property changes, do not listen for row set changes, and 163*cdf0e10cSrcweir // do not dispose the connection until the row set does not need it anymore 164*cdf0e10cSrcweir if ( xNewConnection.get() == m_xOriginalConnection.get() ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir stopRowSetListening(); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir else 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir // start listening at the row set. We're allowed to dispose the old connection as soon 172*cdf0e10cSrcweir // as the RowSet changed 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir // Unfortunately, the our database form implementations sometimes fire the change of their 175*cdf0e10cSrcweir // ActiveConnection twice. This is a error in forms/source/component/DatabaseForm.cxx, but 176*cdf0e10cSrcweir // changing this would require incompatible changes we can't do for a while. 177*cdf0e10cSrcweir // So for the moment, we have to live with it here. 178*cdf0e10cSrcweir // 179*cdf0e10cSrcweir // The only scenario where this doubled notification causes problems is when the connection 180*cdf0e10cSrcweir // of the form is reset to the one we're responsible for (m_xOriginalConnection), so we 181*cdf0e10cSrcweir // check this here. 182*cdf0e10cSrcweir // 183*cdf0e10cSrcweir // Yes, this is a HACK :( 184*cdf0e10cSrcweir // 185*cdf0e10cSrcweir // 94407 - 08.11.2001 - fs@openoffice.org 186*cdf0e10cSrcweir if ( xNewConnection.get() != m_xOriginalConnection.get() ) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0 189*cdf0e10cSrcweir Reference< XConnection > xOldConnection; 190*cdf0e10cSrcweir _rEvent.OldValue >>= xOldConnection; 191*cdf0e10cSrcweir OSL_ENSURE( xOldConnection.get() == m_xOriginalConnection.get(), "OAutoConnectionDisposer::propertyChange: unexpected (original) property value!" ); 192*cdf0e10cSrcweir #endif 193*cdf0e10cSrcweir startRowSetListening(); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir //--------------------------------------------------------------------- 200*cdf0e10cSrcweir void SAL_CALL OAutoConnectionDisposer::disposing( const EventObject& _rSource ) throw (RuntimeException) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir // the rowset is beeing disposed, and nobody has set a new ActiveConnection in the meantime 203*cdf0e10cSrcweir if ( isRowSetListening() ) 204*cdf0e10cSrcweir stopRowSetListening(); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir clearConnection(); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir if ( isPropertyListening() ) 209*cdf0e10cSrcweir stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) ); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir //--------------------------------------------------------------------- 212*cdf0e10cSrcweir void OAutoConnectionDisposer::clearConnection() 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir try 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir // dispose the old connection 217*cdf0e10cSrcweir Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY); 218*cdf0e10cSrcweir if (xComp.is()) 219*cdf0e10cSrcweir xComp->dispose(); 220*cdf0e10cSrcweir m_xOriginalConnection.clear(); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir catch(Exception&) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir OSL_ENSURE(sal_False, "OAutoConnectionDisposer::clearConnection: caught an exception!"); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir //--------------------------------------------------------------------- 228*cdf0e10cSrcweir void SAL_CALL OAutoConnectionDisposer::cursorMoved( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir //--------------------------------------------------------------------- 232*cdf0e10cSrcweir void SAL_CALL OAutoConnectionDisposer::rowChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir //--------------------------------------------------------------------- 236*cdf0e10cSrcweir void SAL_CALL OAutoConnectionDisposer::rowSetChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir stopRowSetListening(); 239*cdf0e10cSrcweir clearConnection(); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir //--------------------------------------------------------------------- 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir //......................................................................... 245*cdf0e10cSrcweir } // namespace dbtools 246*cdf0e10cSrcweir //......................................................................... 247*cdf0e10cSrcweir 248