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 #ifndef DBACCESS_CONNECTION_DEPENDENT_HXX
25 #define DBACCESS_CONNECTION_DEPENDENT_HXX
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/sdbc/XConnection.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 /** === end UNO includes === **/
31 
32 #include <comphelper/componentcontext.hxx>
33 #include <cppuhelper/weakref.hxx>
34 #include <osl/mutex.hxx>
35 
36 //........................................................................
37 namespace sdbtools
38 {
39 //........................................................................
40 
41 	//====================================================================
42 	//= ConnectionDependentComponent
43 	//====================================================================
44     class ConnectionDependentComponent
45     {
46     private:
47         mutable ::osl::Mutex    m_aMutex;
48         ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XConnection >
49                                 m_aConnection;
50         ::comphelper::ComponentContext
51                                 m_aContext;
52 
53         /** a hard reference to the connection we're working for
54 
55             This member is only valid as long as a EntryGuard is on the stack.
56             The guard will, in its constructor, set the member, and reset it in its destructor.
57             This ensures that the connection is only held hard when it's needed, and weak otherwise.
58         */
59         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
60                                 m_xConnection;
61 
62     protected:
getMutex() const63         ::osl::Mutex&   getMutex() const { return m_aMutex; }
64 
65         const ::comphelper::ComponentContext&
getContext() const66                         getContext() const { return m_aContext; }
67 
68     protected:
69         class EntryGuard;
70 
71     protected:
ConnectionDependentComponent(const::comphelper::ComponentContext & _rContext)72         ConnectionDependentComponent( const ::comphelper::ComponentContext& _rContext )
73             :m_aContext( _rContext )
74         {
75         }
76 
77         /** sets the connection we depend on.
78 
79             To be called exactly once.
80 
81             @param  _rxConnection
82                 the connection to set
83         */
setWeakConnection(const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XConnection> & _rxConnection)84         void    setWeakConnection( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection )
85         {
86             m_aConnection = _rxConnection;
87         }
88 
89         const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&
getConnection() const90                 getConnection() const { return m_xConnection; }
91 
92     public:
93 	struct GuardAccess;
94 	friend struct GuardAccess;
95         /** helper for granting exclusive access to various other methods
96         */
GuardAccesssdbtools::ConnectionDependentComponent::GuardAccess97         struct GuardAccess { friend class EntryGuard; private: GuardAccess() { } };
98 
getMutex(GuardAccess) const99         ::osl::Mutex&   getMutex( GuardAccess ) const { return m_aMutex; }
100 
acquireConnection(GuardAccess)101         inline bool acquireConnection( GuardAccess )
102         {
103             m_xConnection = (::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >)m_aConnection;
104             return m_xConnection.is();
105         }
releaseConnection(GuardAccess)106         inline void releaseConnection( GuardAccess )
107         {
108             m_xConnection.clear();
109         }
110     };
111 
112 	//====================================================================
113 	//= ConnectionDependentComponent::EntryGuard
114 	//====================================================================
115     /** a class for guarding methods of a connection-dependent component
116 
117         This class serves multiple purposes:
118         <ul><li>It ensures multi-threading safety by guarding the component's mutex
119                 as long as it lives.</li>
120             <li>It ensures that the component's connection is alive. The constructor
121                 throws a DisposedException if no hard reference to the connection can
122                 be obtained.</li>
123         </ul>
124     */
125     class ConnectionDependentComponent::EntryGuard
126     {
127     private:
128         ::osl::MutexGuard               m_aMutexGuard;
129         ConnectionDependentComponent&   m_rComponent;
130 
131     public:
EntryGuard(ConnectionDependentComponent & _rComponent)132         EntryGuard( ConnectionDependentComponent& _rComponent )
133             :m_aMutexGuard( _rComponent.getMutex( ConnectionDependentComponent::GuardAccess() ) )
134             ,m_rComponent( _rComponent )
135         {
136             if ( !m_rComponent.acquireConnection( ConnectionDependentComponent::GuardAccess() ) )
137                 throw ::com::sun::star::lang::DisposedException();
138         }
139 
~EntryGuard()140         ~EntryGuard()
141         {
142             m_rComponent.releaseConnection( ConnectionDependentComponent::GuardAccess() );
143         }
144     };
145 
146 
147 //........................................................................
148 } // namespace sdbtools
149 //........................................................................
150 
151 #endif // DBACCESS_CONNECTION_DEPENDENT_HXX
152 
153