LibreOffice Module connectivity (master) 1
conncleanup.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/beans/XPropertySet.hpp>
22#include <com/sun/star/lang/XComponent.hpp>
23#include <com/sun/star/sdbc/XRowSet.hpp>
24#include <com/sun/star/sdbc/XConnection.hpp>
25#include <osl/diagnose.h>
27
28
29namespace dbtools
30{
31
32
33 using namespace css::uno;
34 using namespace css::beans;
35 using namespace css::sdbc;
36 using namespace css::lang;
37
38 constexpr OUStringLiteral ACTIVE_CONNECTION_PROPERTY_NAME = u"ActiveConnection";
39
40 OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection)
41 :m_xRowSet( _rxRowSet )
42 ,m_bRSListening( false )
43 ,m_bPropertyListening( false )
44 {
45 Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY);
46 OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
47
48 if (!xProps.is())
49 return;
50
51 try
52 {
53 xProps->setPropertyValue( ACTIVE_CONNECTION_PROPERTY_NAME, Any( _rxConnection ) );
54 m_xOriginalConnection = _rxConnection;
55 startPropertyListening( xProps );
56 }
57 catch( const Exception& )
58 {
59 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::OAutoConnectionDisposer" );
60 }
61 }
62
63
64 void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet )
65 {
66 try
67 {
68 _rxRowSet->addPropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
70 }
71 catch( const Exception& )
72 {
73 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::startPropertyListening" );
74 }
75 }
76
77
78 void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource )
79 {
80 // prevent deletion of ourself while we're herein
81 Reference< XInterface > xKeepAlive(getXWeak());
82
83 try
84 { // remove ourself as property change listener
85 OSL_ENSURE( _rxEventSource.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
86 if ( _rxEventSource.is() )
87 {
88 _rxEventSource->removePropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
90 }
91 }
92 catch( const Exception& )
93 {
94 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::stopPropertyListening" );
95 }
96 }
97
98
100 {
101 OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
102 try
103 {
104 if ( !m_bRSListening )
105 m_xRowSet->addRowSetListener( this );
106 }
107 catch( const Exception& )
108 {
109 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::startRowSetListening" );
110 }
111 m_bRSListening = true;
112 }
113
114
116 {
117 OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
118 try
119 {
120 m_xRowSet->removeRowSetListener( this );
121 }
122 catch( const Exception& )
123 {
124 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::stopRowSetListening" );
125 }
126 m_bRSListening = false;
127 }
128
129
130 void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent )
131 {
132 if ( _rEvent.PropertyName != ACTIVE_CONNECTION_PROPERTY_NAME )
133 return;
134
135// somebody set a new ActiveConnection
136
137 Reference< XConnection > xNewConnection;
138 _rEvent.NewValue >>= xNewConnection;
139
140 if ( isRowSetListening() )
141 {
142 // we're listening at the row set, this means that the row set does not have our
143 // m_xOriginalConnection as active connection anymore
144 // So there are two possibilities
145 // a. somebody sets a new connection which is not our original one
146 // b. somebody sets a new connection, which is exactly the original one
147 // a. we're not interested in a, but in b: In this case, we simply need to move to the state
148 // we had originally: listen for property changes, do not listen for row set changes, and
149 // do not dispose the connection until the row set does not need it anymore
150 if ( xNewConnection.get() == m_xOriginalConnection.get() )
151 {
153 }
154 }
155 else
156 {
157 // start listening at the row set. We're allowed to dispose the old connection as soon
158 // as the RowSet changed
159
160 // Unfortunately, the our database form implementations sometimes fire the change of their
161 // ActiveConnection twice. This is an error in forms/source/component/DatabaseForm.cxx, but
162 // changing this would require incompatible changes we can't do for a while.
163 // So for the moment, we have to live with it here.
164 //
165 // The only scenario where this doubled notification causes problems is when the connection
166 // of the form is reset to the one we're responsible for (m_xOriginalConnection), so we
167 // check this here.
168 //
169 // Yes, this is a HACK :(
170 if ( xNewConnection.get() != m_xOriginalConnection.get() )
171 {
172#if OSL_DEBUG_LEVEL > 0
173 Reference< XConnection > xOldConnection;
174 _rEvent.OldValue >>= xOldConnection;
175 OSL_ENSURE( xOldConnection.get() == m_xOriginalConnection.get(), "OAutoConnectionDisposer::propertyChange: unexpected (original) property value!" );
176#endif
178 }
179 }
180 }
181
182
183 void SAL_CALL OAutoConnectionDisposer::disposing( const EventObject& _rSource )
184 {
185 // the rowset is being disposed, and nobody has set a new ActiveConnection in the meantime
186 if ( isRowSetListening() )
188
190
192 stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) );
193 }
194
196 {
197 try
198 {
199 // dispose the old connection
200 Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY);
201 if (xComp.is())
202 xComp->dispose();
203 m_xOriginalConnection.clear();
204 }
205 catch(Exception&)
206 {
207 TOOLS_WARN_EXCEPTION("connectivity.commontools", "OAutoConnectionDisposer::clearConnection");
208 }
209 }
210
211 void SAL_CALL OAutoConnectionDisposer::cursorMoved( const css::lang::EventObject& /*event*/ )
212 {
213 }
214
215 void SAL_CALL OAutoConnectionDisposer::rowChanged( const css::lang::EventObject& /*event*/ )
216 {
217 }
218
219 void SAL_CALL OAutoConnectionDisposer::rowSetChanged( const css::lang::EventObject& /*event*/ )
220 {
223
224 }
225
226
227} // namespace dbtools
228
229
230/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &_rEvent) override
virtual void SAL_CALL rowSetChanged(const css::lang::EventObject &event) override
virtual void SAL_CALL disposing(const css::lang::EventObject &_rSource) override
void startPropertyListening(const css::uno::Reference< css::beans::XPropertySet > &_rxProps)
Definition: conncleanup.cxx:64
virtual void SAL_CALL cursorMoved(const css::lang::EventObject &event) override
css::uno::Reference< css::sdbc::XConnection > m_xOriginalConnection
Definition: conncleanup.hxx:46
virtual void SAL_CALL rowChanged(const css::lang::EventObject &event) override
OAutoConnectionDisposer(const css::uno::Reference< css::sdbc::XRowSet > &_rxRowSet, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection)
constructs an object
Definition: conncleanup.cxx:40
css::uno::Reference< css::sdbc::XRowSet > m_xRowSet
Definition: conncleanup.hxx:47
void stopPropertyListening(const css::uno::Reference< css::beans::XPropertySet > &_rxEventSource)
Definition: conncleanup.cxx:78
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
@ Exception
constexpr OUStringLiteral ACTIVE_CONNECTION_PROPERTY_NAME
Definition: conncleanup.cxx:38