1*2ee96f1cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2ee96f1cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2ee96f1cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2ee96f1cSAndrew Rist  * distributed with this work for additional information
6*2ee96f1cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2ee96f1cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2ee96f1cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2ee96f1cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2ee96f1cSAndrew Rist  *
11*2ee96f1cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2ee96f1cSAndrew Rist  *
13*2ee96f1cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2ee96f1cSAndrew Rist  * software distributed under the License is distributed on an
15*2ee96f1cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2ee96f1cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2ee96f1cSAndrew Rist  * specific language governing permissions and limitations
18*2ee96f1cSAndrew Rist  * under the License.
19*2ee96f1cSAndrew Rist  *
20*2ee96f1cSAndrew Rist  *************************************************************/
21*2ee96f1cSAndrew Rist 
22*2ee96f1cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cui.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "connpoolconfig.hxx"
28cdf0e10cSrcweir #include "connpoolsettings.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "connpooloptions.hxx"
31cdf0e10cSrcweir #include <svl/itemset.hxx>
32cdf0e10cSrcweir #include <unotools/confignode.hxx>
33cdf0e10cSrcweir #include <comphelper/extract.hxx>
34cdf0e10cSrcweir #include <svl/eitem.hxx>
35cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
36cdf0e10cSrcweir #include "sdbcdriverenum.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir namespace offapp
40cdf0e10cSrcweir {
41cdf0e10cSrcweir //........................................................................
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	using namespace ::utl;
44cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	//--------------------------------------------------------------------
getConnectionPoolNodeName()47cdf0e10cSrcweir 	static const ::rtl::OUString& getConnectionPoolNodeName()
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("org.openoffice.Office.DataAccess/ConnectionPool");
50cdf0e10cSrcweir 		return s_sNodeName;
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	//--------------------------------------------------------------------
getEnablePoolingNodeName()54cdf0e10cSrcweir 	static const ::rtl::OUString& getEnablePoolingNodeName()
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("EnablePooling");
57cdf0e10cSrcweir 		return s_sNodeName;
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	//--------------------------------------------------------------------
getDriverSettingsNodeName()61cdf0e10cSrcweir 	static const ::rtl::OUString& getDriverSettingsNodeName()
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverSettings");
64cdf0e10cSrcweir 		return s_sNodeName;
65cdf0e10cSrcweir 	}
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	//--------------------------------------------------------------------
getDriverNameNodeName()68cdf0e10cSrcweir 	static const ::rtl::OUString& getDriverNameNodeName()
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverName");
71cdf0e10cSrcweir 		return s_sNodeName;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	//--------------------------------------------------------------------
getEnableNodeName()75cdf0e10cSrcweir 	static const ::rtl::OUString& getEnableNodeName()
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Enable");
78cdf0e10cSrcweir 		return s_sNodeName;
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	//--------------------------------------------------------------------
getTimeoutNodeName()82cdf0e10cSrcweir 	static const ::rtl::OUString& getTimeoutNodeName()
83cdf0e10cSrcweir 	{
84cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Timeout");
85cdf0e10cSrcweir 		return s_sNodeName;
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	//====================================================================
89cdf0e10cSrcweir 	//= ConnectionPoolConfig
90cdf0e10cSrcweir 	//====================================================================
91cdf0e10cSrcweir 	//--------------------------------------------------------------------
GetOptions(SfxItemSet & _rFillItems)92cdf0e10cSrcweir 	void ConnectionPoolConfig::GetOptions(SfxItemSet& _rFillItems)
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir 		// the config node where all pooling relevant info are stored under
95cdf0e10cSrcweir 		OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
96cdf0e10cSrcweir 			::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 		// the global "enabled" flag
99cdf0e10cSrcweir 		Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
100cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True;
101cdf0e10cSrcweir 		aEnabled >>= bEnabled;
102cdf0e10cSrcweir 		_rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		// the settings for the single drivers
105cdf0e10cSrcweir 		DriverPoolingSettings aSettings;
106cdf0e10cSrcweir 		// first get all the drivers register at the driver manager
107cdf0e10cSrcweir 		ODriverEnumeration aEnumDrivers;
108cdf0e10cSrcweir 		for	(	ODriverEnumeration::const_iterator aLoopDrivers = aEnumDrivers.begin();
109cdf0e10cSrcweir 				aLoopDrivers != aEnumDrivers.end();
110cdf0e10cSrcweir 				++aLoopDrivers
111cdf0e10cSrcweir 			)
112cdf0e10cSrcweir 		{
113cdf0e10cSrcweir 			aSettings.push_back(DriverPooling(*aLoopDrivers, sal_False, 120));
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		// then look for which of them settings are stored in the configuration
117cdf0e10cSrcweir 		OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aDriverKeys = aDriverSettings.getNodeNames();
120cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
121cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
122cdf0e10cSrcweir 		for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			// the name of the driver in this round
125cdf0e10cSrcweir 			OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
126cdf0e10cSrcweir 			::rtl::OUString sThisDriverName;
127cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 			// look if we (resp. the driver manager) know this driver
130cdf0e10cSrcweir 			// doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
131cdf0e10cSrcweir  			DriverPoolingSettings::iterator aLookup;
132cdf0e10cSrcweir  			for	(	aLookup = aSettings.begin();
133cdf0e10cSrcweir 					aLookup != aSettings.end();
134cdf0e10cSrcweir 					++aLookup
135cdf0e10cSrcweir 				)
136cdf0e10cSrcweir 				if (sThisDriverName.equals(aLookup->sName))
137cdf0e10cSrcweir 					break;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 			if (aLookup == aSettings.end())
140cdf0e10cSrcweir 			{	// do not know the driver - add it
141cdf0e10cSrcweir 				aSettings.push_back(DriverPooling(sThisDriverName, sal_False, 120));
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 				// and the position of the new entry
144cdf0e10cSrcweir 				aLookup = aSettings.end();
145cdf0e10cSrcweir 				--aLookup;
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			// now fill this entry with the settings from the configuration
149cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
150cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		_rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	//--------------------------------------------------------------------
SetOptions(const SfxItemSet & _rSourceItems)157cdf0e10cSrcweir 	void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
158cdf0e10cSrcweir 	{
159cdf0e10cSrcweir 		// the config node where all pooling relevant info are stored under
160cdf0e10cSrcweir 		OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
161cdf0e10cSrcweir 			::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_UPDATABLE);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 		if (!aConnectionPoolRoot.isValid())
164cdf0e10cSrcweir 			// already asserted by the OConfigurationTreeRoot
165cdf0e10cSrcweir 			return;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 		sal_Bool bNeedCommit = sal_False;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		// the global "enabled" flag
170cdf0e10cSrcweir 		SFX_ITEMSET_GET( _rSourceItems, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, sal_True );
171cdf0e10cSrcweir 		if (pEnabled)
172cdf0e10cSrcweir 		{
173cdf0e10cSrcweir 			sal_Bool bEnabled = pEnabled->GetValue();
174cdf0e10cSrcweir 			aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(&bEnabled, ::getBooleanCppuType()));
175cdf0e10cSrcweir 			bNeedCommit = sal_True;
176cdf0e10cSrcweir 		}
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 		// the settings for the single drivers
179cdf0e10cSrcweir 		SFX_ITEMSET_GET( _rSourceItems, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, sal_True );
180cdf0e10cSrcweir 		if (pDriverSettings)
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
183cdf0e10cSrcweir 			if (!aDriverSettings.isValid())
184cdf0e10cSrcweir 				return;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 			::rtl::OUString sThisDriverName;
187cdf0e10cSrcweir 			OConfigurationNode aThisDriverSettings;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
190cdf0e10cSrcweir 			for (	DriverPoolingSettings::const_iterator aLoop = rNewSettings.begin();
191cdf0e10cSrcweir 					aLoop != rNewSettings.end();
192cdf0e10cSrcweir 					++aLoop
193cdf0e10cSrcweir 				)
194cdf0e10cSrcweir 			{
195cdf0e10cSrcweir 				// need the name as ::rtl::OUString
196cdf0e10cSrcweir 				sThisDriverName = aLoop->sName;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 				// the sub-node for this driver
199cdf0e10cSrcweir 				if (aDriverSettings.hasByName(aLoop->sName))
200cdf0e10cSrcweir 					aThisDriverSettings = aDriverSettings.openNode(aLoop->sName);
201cdf0e10cSrcweir 				else
202cdf0e10cSrcweir 					aThisDriverSettings = aDriverSettings.createNode(aLoop->sName);
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 				// set the values
205cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getDriverNameNodeName(), makeAny(sThisDriverName));
206cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(&aLoop->bEnabled, ::getBooleanCppuType()));
207cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getTimeoutNodeName(), makeAny(aLoop->nTimeoutSeconds));
208cdf0e10cSrcweir 			}
209cdf0e10cSrcweir 			bNeedCommit = sal_True;
210cdf0e10cSrcweir 		}
211cdf0e10cSrcweir 		if (bNeedCommit)
212cdf0e10cSrcweir 			aConnectionPoolRoot.commit();
213cdf0e10cSrcweir 	}
214cdf0e10cSrcweir 
215cdf0e10cSrcweir //........................................................................
216cdf0e10cSrcweir }	// namespace offapp
217cdf0e10cSrcweir //........................................................................
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220