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 #ifndef _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 24 #define _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 25 26 #include <com/sun/star/lang/XEventListener.hpp> 27 #include <com/sun/star/sdbc/XPooledConnection.hpp> 28 #include <com/sun/star/sdbc/XDriver.hpp> 29 #include <com/sun/star/beans/PropertyValue.hpp> 30 #include <com/sun/star/beans/XPropertyChangeListener.hpp> 31 #include <com/sun/star/reflection/XProxyFactory.hpp> 32 #include <cppuhelper/weakref.hxx> 33 #include <cppuhelper/implbase1.hxx> 34 #include <comphelper/stl_types.hxx> 35 #include <osl/mutex.hxx> 36 #include <vos/timer.hxx> 37 #include <vos/ref.hxx> 38 #include <rtl/digest.h> 39 40 namespace connectivity 41 { 42 class OConnectionPool; 43 //========================================================================== 44 /// OPoolTimer - Invalidates the connection pool 45 //========================================================================== 46 class OPoolTimer : public ::vos::OTimer 47 { 48 OConnectionPool* m_pPool; 49 public: 50 OPoolTimer(OConnectionPool* _pPool,const ::vos::TTimeValue& _Time) 51 : ::vos::OTimer(_Time) 52 ,m_pPool(_pPool) 53 {} 54 protected: 55 virtual void SAL_CALL onShot(); 56 }; 57 58 //========================================================================== 59 //= OConnectionPool - the one-instance service for PooledConnections 60 //= manages the active connections and the connections in the pool 61 //========================================================================== 62 typedef ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener> OConnectionPool_Base; 63 64 // typedef for the interanl structure 65 typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> > TPooledConnections; 66 67 // contains the currently pooled connections 68 typedef struct 69 { 70 TPooledConnections aConnections; 71 sal_Int32 nALiveCount; // will be decremented everytime a time says to, when will reach zero the pool will be deleted 72 } TConnectionPool; 73 74 struct TDigestHolder 75 { 76 sal_uInt8 m_pBuffer[RTL_DIGEST_LENGTH_SHA1]; 77 TDigestHolder() 78 { 79 m_pBuffer[0] = 0; 80 } 81 82 }; 83 84 // typedef TDigestHolder 85 86 struct TDigestLess : public ::std::binary_function< TDigestHolder, TDigestHolder, bool> 87 { 88 bool operator() (const TDigestHolder& x, const TDigestHolder& y) const 89 { 90 sal_uInt32 i; 91 for(i=0;i < RTL_DIGEST_LENGTH_SHA1 && (x.m_pBuffer[i] >= y.m_pBuffer[i]); ++i) 92 ; 93 return i < RTL_DIGEST_LENGTH_SHA1; 94 } 95 }; 96 97 typedef ::std::map< TDigestHolder,TConnectionPool,TDigestLess> TConnectionMap; 98 99 // contains additional information about a activeconnection which is needed to put it back to the pool 100 typedef struct 101 { 102 TConnectionMap::iterator aPos; 103 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPooledConnection> xPooledConnection; 104 } TActiveConnectionInfo; 105 106 typedef ::std::map< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>, 107 TActiveConnectionInfo> TActiveConnectionMap; 108 109 class OConnectionPool : public OConnectionPool_Base 110 { 111 TConnectionMap m_aPool; // the pooled connections 112 TActiveConnectionMap m_aActiveConnections; // the currently active connections 113 114 ::osl::Mutex m_aMutex; 115 ::vos::ORef<OPoolTimer> m_xInvalidator; // invalidates the conntection pool when shot 116 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; 122 123 private: 124 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> createNewConnection(const ::rtl::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(); 129 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); 137 138 // delete all refs 139 void clear(sal_Bool _bDispose); 140 ::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); 141 // XEventListener 142 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); 143 // XPropertyChangeListener 144 virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException); 145 146 void invalidatePooledConnections(); 147 }; 148 } 149 #endif // _CONNECTIVITY_ZCONNECTIONPOOL_HXX_ 150 151 152