1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 28 #define _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 29 30 #include <com/sun/star/lang/XEventListener.hpp> 31 #include <com/sun/star/sdbc/XPooledConnection.hpp> 32 #include <com/sun/star/sdbc/XDriver.hpp> 33 #include <com/sun/star/beans/PropertyValue.hpp> 34 #include <com/sun/star/beans/XPropertyChangeListener.hpp> 35 #include <com/sun/star/reflection/XProxyFactory.hpp> 36 #include <cppuhelper/weakref.hxx> 37 #include <cppuhelper/implbase1.hxx> 38 #include <comphelper/stl_types.hxx> 39 #include <osl/mutex.hxx> 40 #include <vos/timer.hxx> 41 #include <vos/ref.hxx> 42 #include <rtl/digest.h> 43 44 namespace connectivity 45 { 46 class OConnectionPool; 47 //========================================================================== 48 /// OPoolTimer - Invalidates the connection pool 49 //========================================================================== 50 class OPoolTimer : public ::vos::OTimer 51 { 52 OConnectionPool* m_pPool; 53 public: 54 OPoolTimer(OConnectionPool* _pPool,const ::vos::TTimeValue& _Time) 55 : ::vos::OTimer(_Time) 56 ,m_pPool(_pPool) 57 {} 58 protected: 59 virtual void SAL_CALL onShot(); 60 }; 61 62 //========================================================================== 63 //= OConnectionPool - the one-instance service for PooledConnections 64 //= manages the active connections and the connections in the pool 65 //========================================================================== 66 typedef ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener> OConnectionPool_Base; 67 68 // typedef for the interanl structure 69 typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> > TPooledConnections; 70 71 // contains the currently pooled connections 72 typedef struct 73 { 74 TPooledConnections aConnections; 75 sal_Int32 nALiveCount; // will be decremented everytime a time says to, when will reach zero the pool will be deleted 76 } TConnectionPool; 77 78 struct TDigestHolder 79 { 80 sal_uInt8 m_pBuffer[RTL_DIGEST_LENGTH_SHA1]; 81 TDigestHolder() 82 { 83 m_pBuffer[0] = 0; 84 } 85 86 }; 87 88 // typedef TDigestHolder 89 90 struct TDigestLess : public ::std::binary_function< TDigestHolder, TDigestHolder, bool> 91 { 92 bool operator() (const TDigestHolder& x, const TDigestHolder& y) const 93 { 94 sal_uInt32 i; 95 for(i=0;i < RTL_DIGEST_LENGTH_SHA1 && (x.m_pBuffer[i] >= y.m_pBuffer[i]); ++i) 96 ; 97 return i < RTL_DIGEST_LENGTH_SHA1; 98 } 99 }; 100 101 typedef ::std::map< TDigestHolder,TConnectionPool,TDigestLess> TConnectionMap; 102 103 // contains additional information about a activeconnection which is needed to put it back to the pool 104 typedef struct 105 { 106 TConnectionMap::iterator aPos; 107 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> xPooledConnection; 108 } TActiveConnectionInfo; 109 110 typedef ::std::map< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>, 111 TActiveConnectionInfo> TActiveConnectionMap; 112 113 class OConnectionPool : public OConnectionPool_Base 114 { 115 TConnectionMap m_aPool; // the pooled connections 116 TActiveConnectionMap m_aActiveConnections; // the currently active connections 117 118 ::osl::Mutex m_aMutex; 119 ::vos::ORef<OPoolTimer> m_xInvalidator; // invalidates the conntection pool when shot 120 121 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver > m_xDriver; // the one and only driver for this connectionpool 122 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xDriverNode; // config node entry 123 ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory > m_xProxyFactory; 124 sal_Int32 m_nTimeOut; 125 sal_Int32 m_nALiveCount; 126 127 private: 128 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> createNewConnection(const ::rtl::OUString& _rURL, 129 const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rInfo); 130 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> getPooledConnection(TConnectionMap::iterator& _rIter); 131 // calculate the timeout and the corresponding ALiveCount 132 void calculateTimeOuts(); 133 134 protected: 135 // the dtor will be called from the last instance (last release call) 136 virtual ~OConnectionPool(); 137 public: 138 OConnectionPool(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >& _xDriver, 139 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xDriverNode, 140 const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory); 141 142 // delete all refs 143 void clear(sal_Bool _bDispose); 144 ::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); 145 // XEventListener 146 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); 147 // XPropertyChangeListener 148 virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException); 149 150 void invalidatePooledConnections(); 151 }; 152 } 153 #endif // _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 154 155 156