1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: connectiondependent.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef DBACCESS_CONNECTION_DEPENDENT_HXX
32 #define DBACCESS_CONNECTION_DEPENDENT_HXX
34 /** === begin UNO includes === **/
35 #include <com/sun/star/sdbc/XConnection.hpp>
36 #include <com/sun/star/lang/DisposedException.hpp>
37 /** === end UNO includes === **/
39 #include <comphelper/componentcontext.hxx>
40 #include <cppuhelper/weakref.hxx>
41 #include <osl/mutex.hxx>
43 //........................................................................
46 //........................................................................
48 //====================================================================
49 //= ConnectionDependentComponent
50 //====================================================================
51 class ConnectionDependentComponent
54 mutable ::osl::Mutex m_aMutex
;
55 ::com::sun::star::uno::WeakReference
< ::com::sun::star::sdbc::XConnection
>
57 ::comphelper::ComponentContext
60 /** a hard reference to the connection we're working for
62 This member is only valid as long as a EntryGuard is on the stack.
63 The guard will, in its constructor, set the member, and reset it in its destructor.
64 This ensures that the connection is only held hard when it's needed, and weak otherwise.
66 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
70 ::osl::Mutex
& getMutex() const { return m_aMutex
; }
72 const ::comphelper::ComponentContext
&
73 getContext() const { return m_aContext
; }
79 ConnectionDependentComponent( const ::comphelper::ComponentContext
& _rContext
)
80 :m_aContext( _rContext
)
84 /** sets the connection we depend on.
86 To be called exactly once.
91 void setWeakConnection( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
)
93 m_aConnection
= _rxConnection
;
96 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>&
97 getConnection() const { return m_xConnection
; }
101 friend struct GuardAccess
;
102 /** helper for granting exclusive access to various other methods
104 struct GuardAccess
{ friend class EntryGuard
; private: GuardAccess() { } };
106 ::osl::Mutex
& getMutex( GuardAccess
) const { return m_aMutex
; }
108 inline bool acquireConnection( GuardAccess
)
110 m_xConnection
= (::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>)m_aConnection
;
111 return m_xConnection
.is();
113 inline void releaseConnection( GuardAccess
)
115 m_xConnection
.clear();
119 //====================================================================
120 //= ConnectionDependentComponent::EntryGuard
121 //====================================================================
122 /** a class for guarding methods of a connection-dependent component
124 This class serves multiple purposes:
125 <ul><li>It ensures multi-threading safety by guarding the component's mutex
126 as long as it lives.</li>
127 <li>It ensures that the component's connection is alive. The constructor
128 throws a DisposedException if no hard reference to the connection can
132 class ConnectionDependentComponent::EntryGuard
135 ::osl::MutexGuard m_aMutexGuard
;
136 ConnectionDependentComponent
& m_rComponent
;
139 EntryGuard( ConnectionDependentComponent
& _rComponent
)
140 :m_aMutexGuard( _rComponent
.getMutex( ConnectionDependentComponent::GuardAccess() ) )
141 ,m_rComponent( _rComponent
)
143 if ( !m_rComponent
.acquireConnection( ConnectionDependentComponent::GuardAccess() ) )
144 throw ::com::sun::star::lang::DisposedException();
149 m_rComponent
.releaseConnection( ConnectionDependentComponent::GuardAccess() );
154 //........................................................................
155 } // namespace sdbtools
156 //........................................................................
158 #endif // DBACCESS_CONNECTION_DEPENDENT_HXX