Update ooo320-m1
[ooovba.git] / connectivity / source / cpool / ZConnectionPool.hxx
blob7ccc9cd5bf34f452c6a9a7f1c143ae287a055dd9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ZConnectionPool.hxx,v $
10 * $Revision: 1.10 $
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 ************************************************************************/
30 #ifndef _CONNECTIVITY_ZCONNECTIONPOOL_HXX_
31 #define _CONNECTIVITY_ZCONNECTIONPOOL_HXX_
33 #include <com/sun/star/lang/XEventListener.hpp>
34 #include <com/sun/star/sdbc/XPooledConnection.hpp>
35 #include <com/sun/star/sdbc/XDriver.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
38 #include <com/sun/star/reflection/XProxyFactory.hpp>
39 #include <cppuhelper/weakref.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 #include <comphelper/stl_types.hxx>
42 #include <osl/mutex.hxx>
43 #include <vos/timer.hxx>
44 #include <vos/ref.hxx>
45 #include <rtl/digest.h>
47 namespace connectivity
49 class OConnectionPool;
50 //==========================================================================
51 /// OPoolTimer - Invalidates the connection pool
52 //==========================================================================
53 class OPoolTimer : public ::vos::OTimer
55 OConnectionPool* m_pPool;
56 public:
57 OPoolTimer(OConnectionPool* _pPool,const ::vos::TTimeValue& _Time)
58 : ::vos::OTimer(_Time)
59 ,m_pPool(_pPool)
61 protected:
62 virtual void SAL_CALL onShot();
65 //==========================================================================
66 //= OConnectionPool - the one-instance service for PooledConnections
67 //= manages the active connections and the connections in the pool
68 //==========================================================================
69 typedef ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener> OConnectionPool_Base;
71 // typedef for the interanl structure
72 typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> > TPooledConnections;
74 // contains the currently pooled connections
75 typedef struct
77 TPooledConnections aConnections;
78 sal_Int32 nALiveCount; // will be decremented everytime a time says to, when will reach zero the pool will be deleted
79 } TConnectionPool;
81 struct TDigestHolder
83 sal_uInt8 m_pBuffer[RTL_DIGEST_LENGTH_SHA1];
84 TDigestHolder()
86 m_pBuffer[0] = 0;
91 // typedef TDigestHolder
93 struct TDigestLess : public ::std::binary_function< TDigestHolder, TDigestHolder, bool>
95 bool operator() (const TDigestHolder& x, const TDigestHolder& y) const
97 sal_uInt32 i;
98 for(i=0;i < RTL_DIGEST_LENGTH_SHA1 && (x.m_pBuffer[i] >= y.m_pBuffer[i]); ++i)
100 return i < RTL_DIGEST_LENGTH_SHA1;
104 typedef ::std::map< TDigestHolder,TConnectionPool,TDigestLess> TConnectionMap;
106 // contains additional information about a activeconnection which is needed to put it back to the pool
107 typedef struct
109 TConnectionMap::iterator aPos;
110 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> xPooledConnection;
111 } TActiveConnectionInfo;
113 typedef ::std::map< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>,
114 TActiveConnectionInfo> TActiveConnectionMap;
116 class OConnectionPool : public OConnectionPool_Base
118 TConnectionMap m_aPool; // the pooled connections
119 TActiveConnectionMap m_aActiveConnections; // the currently active connections
121 ::osl::Mutex m_aMutex;
122 ::vos::ORef<OPoolTimer> m_xInvalidator; // invalidates the conntection pool when shot
124 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver > m_xDriver; // the one and only driver for this connectionpool
125 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xDriverNode; // config node entry
126 ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory > m_xProxyFactory;
127 sal_Int32 m_nTimeOut;
128 sal_Int32 m_nALiveCount;
130 private:
131 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> createNewConnection(const ::rtl::OUString& _rURL,
132 const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rInfo);
133 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> getPooledConnection(TConnectionMap::iterator& _rIter);
134 // calculate the timeout and the corresponding ALiveCount
135 void calculateTimeOuts();
137 protected:
138 // the dtor will be called from the last instance (last release call)
139 virtual ~OConnectionPool();
140 public:
141 OConnectionPool(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >& _xDriver,
142 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xDriverNode,
143 const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory);
145 // delete all refs
146 void clear(sal_Bool _bDispose);
147 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL getConnectionWithInfo( const ::rtl::OUString& url, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
148 // XEventListener
149 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
150 // XPropertyChangeListener
151 virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException);
153 void invalidatePooledConnections();
156 #endif // _CONNECTIVITY_ZCONNECTIONPOOL_HXX_