1*caf5cd79SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*caf5cd79SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*caf5cd79SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*caf5cd79SAndrew Rist * distributed with this work for additional information 6*caf5cd79SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*caf5cd79SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*caf5cd79SAndrew Rist * "License"); you may not use this file except in compliance 9*caf5cd79SAndrew Rist * with the License. You may obtain a copy of the License at 10*caf5cd79SAndrew Rist * 11*caf5cd79SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*caf5cd79SAndrew Rist * 13*caf5cd79SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*caf5cd79SAndrew Rist * software distributed under the License is distributed on an 15*caf5cd79SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*caf5cd79SAndrew Rist * KIND, either express or implied. See the License for the 17*caf5cd79SAndrew Rist * specific language governing permissions and limitations 18*caf5cd79SAndrew Rist * under the License. 19*caf5cd79SAndrew Rist * 20*caf5cd79SAndrew Rist *************************************************************/ 21*caf5cd79SAndrew Rist 22*caf5cd79SAndrew Rist 23cdf0e10cSrcweir #ifndef _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 24cdf0e10cSrcweir #define _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp> 27cdf0e10cSrcweir #include <com/sun/star/sdbc/XPooledConnection.hpp> 28cdf0e10cSrcweir #include <com/sun/star/sdbc/XDriver.hpp> 29cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyChangeListener.hpp> 31cdf0e10cSrcweir #include <com/sun/star/reflection/XProxyFactory.hpp> 32cdf0e10cSrcweir #include <cppuhelper/weakref.hxx> 33cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 34cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 35cdf0e10cSrcweir #include <osl/mutex.hxx> 36cdf0e10cSrcweir #include <vos/timer.hxx> 37cdf0e10cSrcweir #include <vos/ref.hxx> 38cdf0e10cSrcweir #include <rtl/digest.h> 39cdf0e10cSrcweir 40cdf0e10cSrcweir namespace connectivity 41cdf0e10cSrcweir { 42cdf0e10cSrcweir class OConnectionPool; 43cdf0e10cSrcweir //========================================================================== 44cdf0e10cSrcweir /// OPoolTimer - Invalidates the connection pool 45cdf0e10cSrcweir //========================================================================== 46cdf0e10cSrcweir class OPoolTimer : public ::vos::OTimer 47cdf0e10cSrcweir { 48cdf0e10cSrcweir OConnectionPool* m_pPool; 49cdf0e10cSrcweir public: OPoolTimer(OConnectionPool * _pPool,const::vos::TTimeValue & _Time)50cdf0e10cSrcweir OPoolTimer(OConnectionPool* _pPool,const ::vos::TTimeValue& _Time) 51cdf0e10cSrcweir : ::vos::OTimer(_Time) 52cdf0e10cSrcweir ,m_pPool(_pPool) 53cdf0e10cSrcweir {} 54cdf0e10cSrcweir protected: 55cdf0e10cSrcweir virtual void SAL_CALL onShot(); 56cdf0e10cSrcweir }; 57cdf0e10cSrcweir 58cdf0e10cSrcweir //========================================================================== 59cdf0e10cSrcweir //= OConnectionPool - the one-instance service for PooledConnections 60cdf0e10cSrcweir //= manages the active connections and the connections in the pool 61cdf0e10cSrcweir //========================================================================== 62cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener> OConnectionPool_Base; 63cdf0e10cSrcweir 64cdf0e10cSrcweir // typedef for the interanl structure 65cdf0e10cSrcweir typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> > TPooledConnections; 66cdf0e10cSrcweir 67cdf0e10cSrcweir // contains the currently pooled connections 68cdf0e10cSrcweir typedef struct 69cdf0e10cSrcweir { 70cdf0e10cSrcweir TPooledConnections aConnections; 71cdf0e10cSrcweir sal_Int32 nALiveCount; // will be decremented everytime a time says to, when will reach zero the pool will be deleted 72cdf0e10cSrcweir } TConnectionPool; 73cdf0e10cSrcweir 74cdf0e10cSrcweir struct TDigestHolder 75cdf0e10cSrcweir { 76cdf0e10cSrcweir sal_uInt8 m_pBuffer[RTL_DIGEST_LENGTH_SHA1]; TDigestHolderconnectivity::TDigestHolder77cdf0e10cSrcweir TDigestHolder() 78cdf0e10cSrcweir { 79cdf0e10cSrcweir m_pBuffer[0] = 0; 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir }; 83cdf0e10cSrcweir 84cdf0e10cSrcweir // typedef TDigestHolder 85cdf0e10cSrcweir 86cdf0e10cSrcweir struct TDigestLess : public ::std::binary_function< TDigestHolder, TDigestHolder, bool> 87cdf0e10cSrcweir { operator ()connectivity::TDigestLess88cdf0e10cSrcweir bool operator() (const TDigestHolder& x, const TDigestHolder& y) const 89cdf0e10cSrcweir { 90cdf0e10cSrcweir sal_uInt32 i; 91cdf0e10cSrcweir for(i=0;i < RTL_DIGEST_LENGTH_SHA1 && (x.m_pBuffer[i] >= y.m_pBuffer[i]); ++i) 92cdf0e10cSrcweir ; 93cdf0e10cSrcweir return i < RTL_DIGEST_LENGTH_SHA1; 94cdf0e10cSrcweir } 95cdf0e10cSrcweir }; 96cdf0e10cSrcweir 97cdf0e10cSrcweir typedef ::std::map< TDigestHolder,TConnectionPool,TDigestLess> TConnectionMap; 98cdf0e10cSrcweir 99cdf0e10cSrcweir // contains additional information about a activeconnection which is needed to put it back to the pool 100cdf0e10cSrcweir typedef struct 101cdf0e10cSrcweir { 102cdf0e10cSrcweir TConnectionMap::iterator aPos; 103cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> xPooledConnection; 104cdf0e10cSrcweir } TActiveConnectionInfo; 105cdf0e10cSrcweir 106cdf0e10cSrcweir typedef ::std::map< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>, 107cdf0e10cSrcweir TActiveConnectionInfo> TActiveConnectionMap; 108cdf0e10cSrcweir 109cdf0e10cSrcweir class OConnectionPool : public OConnectionPool_Base 110cdf0e10cSrcweir { 111cdf0e10cSrcweir TConnectionMap m_aPool; // the pooled connections 112cdf0e10cSrcweir TActiveConnectionMap m_aActiveConnections; // the currently active connections 113cdf0e10cSrcweir 114cdf0e10cSrcweir ::osl::Mutex m_aMutex; 115cdf0e10cSrcweir ::vos::ORef<OPoolTimer> m_xInvalidator; // invalidates the conntection pool when shot 116cdf0e10cSrcweir 117cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver > m_xDriver; // the one and only driver for this connectionpool 118cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xDriverNode; // config node entry 119cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory > m_xProxyFactory; 120cdf0e10cSrcweir sal_Int32 m_nTimeOut; 121cdf0e10cSrcweir sal_Int32 m_nALiveCount; 122cdf0e10cSrcweir 123cdf0e10cSrcweir private: 124cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> createNewConnection(const ::rtl::OUString& _rURL, 125cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rInfo); 126cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> getPooledConnection(TConnectionMap::iterator& _rIter); 127cdf0e10cSrcweir // calculate the timeout and the corresponding ALiveCount 128cdf0e10cSrcweir void calculateTimeOuts(); 129cdf0e10cSrcweir 130cdf0e10cSrcweir protected: 131cdf0e10cSrcweir // the dtor will be called from the last instance (last release call) 132cdf0e10cSrcweir virtual ~OConnectionPool(); 133cdf0e10cSrcweir public: 134cdf0e10cSrcweir OConnectionPool(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >& _xDriver, 135cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xDriverNode, 136cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory); 137cdf0e10cSrcweir 138cdf0e10cSrcweir // delete all refs 139cdf0e10cSrcweir void clear(sal_Bool _bDispose); 140cdf0e10cSrcweir ::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); 141cdf0e10cSrcweir // XEventListener 142cdf0e10cSrcweir virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); 143cdf0e10cSrcweir // XPropertyChangeListener 144cdf0e10cSrcweir virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException); 145cdf0e10cSrcweir 146cdf0e10cSrcweir void invalidatePooledConnections(); 147cdf0e10cSrcweir }; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir #endif // _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 150cdf0e10cSrcweir 151cdf0e10cSrcweir 152