1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cui.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "connpoolconfig.hxx"
32*cdf0e10cSrcweir #include "connpoolsettings.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "connpooloptions.hxx"
35*cdf0e10cSrcweir #include <svl/itemset.hxx>
36*cdf0e10cSrcweir #include <unotools/confignode.hxx>
37*cdf0e10cSrcweir #include <comphelper/extract.hxx>
38*cdf0e10cSrcweir #include <svl/eitem.hxx>
39*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40*cdf0e10cSrcweir #include "sdbcdriverenum.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //........................................................................
43*cdf0e10cSrcweir namespace offapp
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir //........................................................................
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 	using namespace ::utl;
48*cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 	//--------------------------------------------------------------------
51*cdf0e10cSrcweir 	static const ::rtl::OUString& getConnectionPoolNodeName()
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("org.openoffice.Office.DataAccess/ConnectionPool");
54*cdf0e10cSrcweir 		return s_sNodeName;
55*cdf0e10cSrcweir 	}
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	//--------------------------------------------------------------------
58*cdf0e10cSrcweir 	static const ::rtl::OUString& getEnablePoolingNodeName()
59*cdf0e10cSrcweir 	{
60*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("EnablePooling");
61*cdf0e10cSrcweir 		return s_sNodeName;
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	//--------------------------------------------------------------------
65*cdf0e10cSrcweir 	static const ::rtl::OUString& getDriverSettingsNodeName()
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverSettings");
68*cdf0e10cSrcweir 		return s_sNodeName;
69*cdf0e10cSrcweir 	}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	//--------------------------------------------------------------------
72*cdf0e10cSrcweir 	static const ::rtl::OUString& getDriverNameNodeName()
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("DriverName");
75*cdf0e10cSrcweir 		return s_sNodeName;
76*cdf0e10cSrcweir 	}
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	//--------------------------------------------------------------------
79*cdf0e10cSrcweir 	static const ::rtl::OUString& getEnableNodeName()
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Enable");
82*cdf0e10cSrcweir 		return s_sNodeName;
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	//--------------------------------------------------------------------
86*cdf0e10cSrcweir 	static const ::rtl::OUString& getTimeoutNodeName()
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Timeout");
89*cdf0e10cSrcweir 		return s_sNodeName;
90*cdf0e10cSrcweir 	}
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	//====================================================================
93*cdf0e10cSrcweir 	//= ConnectionPoolConfig
94*cdf0e10cSrcweir 	//====================================================================
95*cdf0e10cSrcweir 	//--------------------------------------------------------------------
96*cdf0e10cSrcweir 	void ConnectionPoolConfig::GetOptions(SfxItemSet& _rFillItems)
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		// the config node where all pooling relevant info are stored under
99*cdf0e10cSrcweir 		OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
100*cdf0e10cSrcweir 			::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 		// the global "enabled" flag
103*cdf0e10cSrcweir 		Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
104*cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True;
105*cdf0e10cSrcweir 		aEnabled >>= bEnabled;
106*cdf0e10cSrcweir 		_rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 		// the settings for the single drivers
109*cdf0e10cSrcweir 		DriverPoolingSettings aSettings;
110*cdf0e10cSrcweir 		// first get all the drivers register at the driver manager
111*cdf0e10cSrcweir 		ODriverEnumeration aEnumDrivers;
112*cdf0e10cSrcweir 		for	(	ODriverEnumeration::const_iterator aLoopDrivers = aEnumDrivers.begin();
113*cdf0e10cSrcweir 				aLoopDrivers != aEnumDrivers.end();
114*cdf0e10cSrcweir 				++aLoopDrivers
115*cdf0e10cSrcweir 			)
116*cdf0e10cSrcweir 		{
117*cdf0e10cSrcweir 			aSettings.push_back(DriverPooling(*aLoopDrivers, sal_False, 120));
118*cdf0e10cSrcweir 		}
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 		// then look for which of them settings are stored in the configuration
121*cdf0e10cSrcweir 		OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aDriverKeys = aDriverSettings.getNodeNames();
124*cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
125*cdf0e10cSrcweir 		const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
126*cdf0e10cSrcweir 		for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
127*cdf0e10cSrcweir 		{
128*cdf0e10cSrcweir 			// the name of the driver in this round
129*cdf0e10cSrcweir 			OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
130*cdf0e10cSrcweir 			::rtl::OUString sThisDriverName;
131*cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 			// look if we (resp. the driver manager) know this driver
134*cdf0e10cSrcweir 			// doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
135*cdf0e10cSrcweir  			DriverPoolingSettings::iterator aLookup;
136*cdf0e10cSrcweir  			for	(	aLookup = aSettings.begin();
137*cdf0e10cSrcweir 					aLookup != aSettings.end();
138*cdf0e10cSrcweir 					++aLookup
139*cdf0e10cSrcweir 				)
140*cdf0e10cSrcweir 				if (sThisDriverName.equals(aLookup->sName))
141*cdf0e10cSrcweir 					break;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 			if (aLookup == aSettings.end())
144*cdf0e10cSrcweir 			{	// do not know the driver - add it
145*cdf0e10cSrcweir 				aSettings.push_back(DriverPooling(sThisDriverName, sal_False, 120));
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 				// and the position of the new entry
148*cdf0e10cSrcweir 				aLookup = aSettings.end();
149*cdf0e10cSrcweir 				--aLookup;
150*cdf0e10cSrcweir 			}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 			// now fill this entry with the settings from the configuration
153*cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
154*cdf0e10cSrcweir 			aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
155*cdf0e10cSrcweir 		}
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 		_rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	//--------------------------------------------------------------------
161*cdf0e10cSrcweir 	void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		// the config node where all pooling relevant info are stored under
164*cdf0e10cSrcweir 		OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
165*cdf0e10cSrcweir 			::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_UPDATABLE);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 		if (!aConnectionPoolRoot.isValid())
168*cdf0e10cSrcweir 			// already asserted by the OConfigurationTreeRoot
169*cdf0e10cSrcweir 			return;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 		sal_Bool bNeedCommit = sal_False;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		// the global "enabled" flag
174*cdf0e10cSrcweir 		SFX_ITEMSET_GET( _rSourceItems, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, sal_True );
175*cdf0e10cSrcweir 		if (pEnabled)
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 			sal_Bool bEnabled = pEnabled->GetValue();
178*cdf0e10cSrcweir 			aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(&bEnabled, ::getBooleanCppuType()));
179*cdf0e10cSrcweir 			bNeedCommit = sal_True;
180*cdf0e10cSrcweir 		}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		// the settings for the single drivers
183*cdf0e10cSrcweir 		SFX_ITEMSET_GET( _rSourceItems, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, sal_True );
184*cdf0e10cSrcweir 		if (pDriverSettings)
185*cdf0e10cSrcweir 		{
186*cdf0e10cSrcweir 			OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
187*cdf0e10cSrcweir 			if (!aDriverSettings.isValid())
188*cdf0e10cSrcweir 				return;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 			::rtl::OUString sThisDriverName;
191*cdf0e10cSrcweir 			OConfigurationNode aThisDriverSettings;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
194*cdf0e10cSrcweir 			for (	DriverPoolingSettings::const_iterator aLoop = rNewSettings.begin();
195*cdf0e10cSrcweir 					aLoop != rNewSettings.end();
196*cdf0e10cSrcweir 					++aLoop
197*cdf0e10cSrcweir 				)
198*cdf0e10cSrcweir 			{
199*cdf0e10cSrcweir 				// need the name as ::rtl::OUString
200*cdf0e10cSrcweir 				sThisDriverName = aLoop->sName;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 				// the sub-node for this driver
203*cdf0e10cSrcweir 				if (aDriverSettings.hasByName(aLoop->sName))
204*cdf0e10cSrcweir 					aThisDriverSettings = aDriverSettings.openNode(aLoop->sName);
205*cdf0e10cSrcweir 				else
206*cdf0e10cSrcweir 					aThisDriverSettings = aDriverSettings.createNode(aLoop->sName);
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 				// set the values
209*cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getDriverNameNodeName(), makeAny(sThisDriverName));
210*cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(&aLoop->bEnabled, ::getBooleanCppuType()));
211*cdf0e10cSrcweir 				aThisDriverSettings.setNodeValue(getTimeoutNodeName(), makeAny(aLoop->nTimeoutSeconds));
212*cdf0e10cSrcweir 			}
213*cdf0e10cSrcweir 			bNeedCommit = sal_True;
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 		if (bNeedCommit)
216*cdf0e10cSrcweir 			aConnectionPoolRoot.commit();
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir //........................................................................
220*cdf0e10cSrcweir }	// namespace offapp
221*cdf0e10cSrcweir //........................................................................
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 
224