1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "connpoolconfig.hxx"
21 #include "connpoolsettings.hxx"
23 #include "connpooloptions.hxx"
24 #include <svl/itemset.hxx>
25 #include <unotools/confignode.hxx>
26 #include <comphelper/extract.hxx>
27 #include <svl/eitem.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include "sdbcdriverenum.hxx"
36 using namespace ::utl
;
37 using namespace ::com::sun::star::uno
;
40 static OUString
getConnectionPoolNodeName()
42 return OUString("org.openoffice.Office.DataAccess/ConnectionPool" );
46 static OUString
getEnablePoolingNodeName()
48 return OUString("EnablePooling");
52 static OUString
getDriverSettingsNodeName()
54 return OUString("DriverSettings");
58 static OUString
getDriverNameNodeName()
60 return OUString("DriverName");
64 static OUString
getEnableNodeName()
66 return OUString("Enable");
70 static OUString
getTimeoutNodeName()
72 return OUString("Timeout");
75 void ConnectionPoolConfig::GetOptions(SfxItemSet
& _rFillItems
)
77 // the config node where all pooling relevant info are stored under
78 OConfigurationTreeRoot aConnectionPoolRoot
= OConfigurationTreeRoot::createWithComponentContext(
79 ::comphelper::getProcessComponentContext(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY
);
81 // the global "enabled" flag
82 Any aEnabled
= aConnectionPoolRoot
.getNodeValue(getEnablePoolingNodeName());
84 aEnabled
>>= bEnabled
;
85 _rFillItems
.Put(SfxBoolItem(SID_SB_POOLING_ENABLED
, bEnabled
));
87 // the settings for the single drivers
88 DriverPoolingSettings aSettings
;
89 // first get all the drivers register at the driver manager
90 ODriverEnumeration aEnumDrivers
;
91 for ( ODriverEnumeration::const_iterator aLoopDrivers
= aEnumDrivers
.begin();
92 aLoopDrivers
!= aEnumDrivers
.end();
96 aSettings
.push_back(DriverPooling(*aLoopDrivers
, false, 120));
99 // then look for which of them settings are stored in the configuration
100 OConfigurationNode aDriverSettings
= aConnectionPoolRoot
.openNode(getDriverSettingsNodeName());
102 Sequence
< OUString
> aDriverKeys
= aDriverSettings
.getNodeNames();
103 const OUString
* pDriverKeys
= aDriverKeys
.getConstArray();
104 const OUString
* pDriverKeysEnd
= pDriverKeys
+ aDriverKeys
.getLength();
105 for (;pDriverKeys
!= pDriverKeysEnd
; ++pDriverKeys
)
107 // the name of the driver in this round
108 OConfigurationNode aThisDriverSettings
= aDriverSettings
.openNode(*pDriverKeys
);
109 OUString sThisDriverName
;
110 aThisDriverSettings
.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName
;
112 // look if we (resp. the driver manager) know this driver
113 // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
114 DriverPoolingSettings::iterator aLookup
;
115 for ( aLookup
= aSettings
.begin();
116 aLookup
!= aSettings
.end();
119 if (sThisDriverName
.equals(aLookup
->sName
))
122 if (aLookup
== aSettings
.end())
123 { // do not know the driver - add it
124 aSettings
.push_back(DriverPooling(sThisDriverName
, false, 120));
126 // and the position of the new entry
127 aLookup
= aSettings
.end();
131 // now fill this entry with the settings from the configuration
132 aThisDriverSettings
.getNodeValue(getEnableNodeName()) >>= aLookup
->bEnabled
;
133 aThisDriverSettings
.getNodeValue(getTimeoutNodeName()) >>= aLookup
->nTimeoutSeconds
;
136 _rFillItems
.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS
, aSettings
));
140 void ConnectionPoolConfig::SetOptions(const SfxItemSet
& _rSourceItems
)
142 // the config node where all pooling relevant info are stored under
143 OConfigurationTreeRoot aConnectionPoolRoot
= OConfigurationTreeRoot::createWithComponentContext(
144 ::comphelper::getProcessComponentContext(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_UPDATABLE
);
146 if (!aConnectionPoolRoot
.isValid())
147 // already asserted by the OConfigurationTreeRoot
150 bool bNeedCommit
= false;
152 // the global "enabled" flag
153 SFX_ITEMSET_GET( _rSourceItems
, pEnabled
, SfxBoolItem
, SID_SB_POOLING_ENABLED
, true );
156 sal_Bool bEnabled
= pEnabled
->GetValue();
157 aConnectionPoolRoot
.setNodeValue(getEnablePoolingNodeName(), Any(&bEnabled
, cppu::UnoType
<bool>::get()));
161 // the settings for the single drivers
162 SFX_ITEMSET_GET( _rSourceItems
, pDriverSettings
, DriverPoolingSettingsItem
, SID_SB_DRIVER_TIMEOUTS
, true );
165 OConfigurationNode aDriverSettings
= aConnectionPoolRoot
.openNode(getDriverSettingsNodeName());
166 if (!aDriverSettings
.isValid())
169 OUString sThisDriverName
;
170 OConfigurationNode aThisDriverSettings
;
172 const DriverPoolingSettings
& rNewSettings
= pDriverSettings
->getSettings();
173 for ( DriverPoolingSettings::const_iterator aLoop
= rNewSettings
.begin();
174 aLoop
!= rNewSettings
.end();
178 // need the name as OUString
179 sThisDriverName
= aLoop
->sName
;
181 // the sub-node for this driver
182 if (aDriverSettings
.hasByName(aLoop
->sName
))
183 aThisDriverSettings
= aDriverSettings
.openNode(aLoop
->sName
);
185 aThisDriverSettings
= aDriverSettings
.createNode(aLoop
->sName
);
188 aThisDriverSettings
.setNodeValue(getDriverNameNodeName(), makeAny(sThisDriverName
));
189 aThisDriverSettings
.setNodeValue(getEnableNodeName(), Any(&aLoop
->bEnabled
, cppu::UnoType
<bool>::get()));
190 aThisDriverSettings
.setNodeValue(getTimeoutNodeName(), makeAny(aLoop
->nTimeoutSeconds
));
195 aConnectionPoolRoot
.commit();
199 } // namespace offapp
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */