bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / cpool / ZConnectionPool.hxx
blobd8b3c41c0d07e96c8b7f8d7760cd4c9ee2f4f1f7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_CONNECTIVITY_SOURCE_CPOOL_ZCONNECTIONPOOL_HXX
20 #define INCLUDED_CONNECTIVITY_SOURCE_CPOOL_ZCONNECTIONPOOL_HXX
22 #include <sal/config.h>
24 #include <map>
25 #include <vector>
27 #include <com/sun/star/lang/XEventListener.hpp>
28 #include <com/sun/star/sdbc/XPooledConnection.hpp>
29 #include <com/sun/star/sdbc/XDriver.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
32 #include <com/sun/star/reflection/XProxyFactory.hpp>
33 #include <cppuhelper/weakref.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <osl/mutex.hxx>
36 #include <salhelper/timer.hxx>
37 #include <rtl/ref.hxx>
38 #include <rtl/digest.h>
40 namespace connectivity
42 class OConnectionPool;
44 /// OPoolTimer - Invalidates the connection pool
46 class OPoolTimer : public ::salhelper::Timer
48 OConnectionPool* m_pPool;
49 public:
50 OPoolTimer(OConnectionPool* _pPool,const ::salhelper::TTimeValue& _Time)
51 : ::salhelper::Timer(_Time)
52 ,m_pPool(_pPool)
54 protected:
55 virtual void SAL_CALL onShot() SAL_OVERRIDE;
59 // OConnectionPool - the one-instance service for PooledConnections
60 // manages the active connections and the connections in the pool
62 typedef ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener> OConnectionPool_Base;
64 // typedef for the interanl structure
65 typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> > TPooledConnections;
67 // contains the currently pooled connections
68 typedef struct
70 TPooledConnections aConnections;
71 sal_Int32 nALiveCount; // will be decremented every time a time says to, when will reach zero the pool will be deleted
72 } TConnectionPool;
74 struct TDigestHolder
76 sal_uInt8 m_pBuffer[RTL_DIGEST_LENGTH_SHA1];
77 TDigestHolder()
79 m_pBuffer[0] = 0;
84 // typedef TDigestHolder
86 struct TDigestLess : public ::std::binary_function< TDigestHolder, TDigestHolder, bool>
88 bool operator() (const TDigestHolder& x, const TDigestHolder& y) const
90 sal_uInt32 i;
91 for(i=0;i < RTL_DIGEST_LENGTH_SHA1 && (x.m_pBuffer[i] >= y.m_pBuffer[i]); ++i)
93 return i < RTL_DIGEST_LENGTH_SHA1;
97 typedef ::std::map< TDigestHolder,TConnectionPool,TDigestLess> TConnectionMap;
99 // contains additional information about a activeconnection which is needed to put it back to the pool
100 typedef struct
102 TConnectionMap::iterator aPos;
103 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> xPooledConnection;
104 } TActiveConnectionInfo;
106 typedef ::std::map< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>,
107 TActiveConnectionInfo> TActiveConnectionMap;
109 class OConnectionPool : public OConnectionPool_Base
111 TConnectionMap m_aPool; // the pooled connections
112 TActiveConnectionMap m_aActiveConnections; // the currently active connections
114 ::osl::Mutex m_aMutex;
115 ::rtl::Reference<OPoolTimer> m_xInvalidator; // invalidates the conntection pool when shot
117 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver > m_xDriver; // the one and only driver for this connectionpool
118 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xDriverNode; // config node entry
119 ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory > m_xProxyFactory;
120 sal_Int32 m_nTimeOut;
121 sal_Int32 m_nALiveCount;
123 private:
124 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> createNewConnection(const OUString& _rURL,
125 const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rInfo);
126 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> getPooledConnection(TConnectionMap::iterator& _rIter);
127 // calculate the timeout and the corresponding ALiveCount
128 void calculateTimeOuts();
130 protected:
131 // the dtor will be called from the last instance (last release call)
132 virtual ~OConnectionPool();
133 public:
134 OConnectionPool(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >& _xDriver,
135 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xDriverNode,
136 const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory);
138 // delete all refs
139 void clear(bool _bDispose);
140 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL getConnectionWithInfo( const 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);
141 // XEventListener
142 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
143 // XPropertyChangeListener
144 virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
146 void invalidatePooledConnections();
149 #endif // INCLUDED_CONNECTIVITY_SOURCE_CPOOL_ZCONNECTIONPOOL_HXX
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */