LibreOffice Module cui (master)  1
connpoolconfig.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "connpoolconfig.hxx"
21 #include "connpoolsettings.hxx"
22 
23 #include "connpooloptions.hxx"
24 #include <svl/itemset.hxx>
26 #include <unotools/confignode.hxx>
27 #include <svl/eitem.hxx>
28 #include <svl/solar.hrc>
30 #include "sdbcdriverenum.hxx"
31 
32 
33 namespace offapp
34 {
35 
36 
37  using namespace ::utl;
38  using namespace ::com::sun::star::uno;
39 
40 
41  static OUString getConnectionPoolNodeName()
42  {
43  return "org.openoffice.Office.DataAccess/ConnectionPool";
44  }
45 
46 
47  static OUString getEnablePoolingNodeName()
48  {
49  return "EnablePooling";
50  }
51 
52 
53  static OUString getDriverSettingsNodeName()
54  {
55  return "DriverSettings";
56  }
57 
58 
59  static OUString getDriverNameNodeName()
60  {
61  return "DriverName";
62  }
63 
64 
65  static OUString getEnableNodeName()
66  {
67  return "Enable";
68  }
69 
70 
71  static OUString getTimeoutNodeName()
72  {
73  return "Timeout";
74  }
75 
77  {
78  // the config node where all pooling relevant info are stored under
79  OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithComponentContext(
80  ::comphelper::getProcessComponentContext(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
81 
82  // the global "enabled" flag
83  Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
84  bool bEnabled = true;
85  aEnabled >>= bEnabled;
86  _rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
87 
88  // the settings for the single drivers
89  DriverPoolingSettings aSettings;
90  // first get all the drivers register at the driver manager
91  ODriverEnumeration aEnumDrivers;
92  for (auto const& elem : aEnumDrivers)
93  {
94  aSettings.push_back(DriverPooling(elem));
95  }
96 
97  // then look for which of them settings are stored in the configuration
98  OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
99 
100  Sequence< OUString > aDriverKeys = aDriverSettings.getNodeNames();
101  const OUString* pDriverKeys = aDriverKeys.getConstArray();
102  const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
103  for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
104  {
105  // the name of the driver in this round
106  OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
107  OUString sThisDriverName;
108  aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
109 
110  // look if we (resp. the driver manager) know this driver
111  // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
113  for ( aLookup = aSettings.begin();
114  aLookup != aSettings.end();
115  ++aLookup
116  )
117  if (sThisDriverName == aLookup->sName)
118  break;
119 
120  if (aLookup == aSettings.end())
121  { // do not know the driver - add it
122  aSettings.push_back(DriverPooling(sThisDriverName));
123 
124  // and the position of the new entry
125  aLookup = aSettings.end();
126  --aLookup;
127  }
128 
129  // now fill this entry with the settings from the configuration
130  aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
131  aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
132  }
133 
134  _rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
135  }
136 
137 
138  void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
139  {
140  // the config node where all pooling relevant info are stored under
141  OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithComponentContext(
143 
144  if (!aConnectionPoolRoot.isValid())
145  // already asserted by the OConfigurationTreeRoot
146  return;
147 
148  bool bNeedCommit = false;
149 
150  // the global "enabled" flag
151  const SfxBoolItem* pEnabled = _rSourceItems.GetItem<SfxBoolItem>(SID_SB_POOLING_ENABLED);
152  if (pEnabled)
153  {
154  bool bEnabled = pEnabled->GetValue();
155  aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(bEnabled));
156  bNeedCommit = true;
157  }
158 
159  // the settings for the single drivers
161  if (pDriverSettings)
162  {
163  OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
164  if (!aDriverSettings.isValid())
165  return;
166 
167  OUString sThisDriverName;
168  OConfigurationNode aThisDriverSettings;
169 
170  const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
171  for (auto const& newSetting : rNewSettings)
172  {
173  // need the name as OUString
174  sThisDriverName = newSetting.sName;
175 
176  // the sub-node for this driver
177  if (aDriverSettings.hasByName(newSetting.sName))
178  aThisDriverSettings = aDriverSettings.openNode(newSetting.sName);
179  else
180  aThisDriverSettings = aDriverSettings.createNode(newSetting.sName);
181 
182  // set the values
183  aThisDriverSettings.setNodeValue(getDriverNameNodeName(), Any(sThisDriverName));
184  aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(newSetting.bEnabled));
185  aThisDriverSettings.setNodeValue(getTimeoutNodeName(), Any(newSetting.nTimeoutSeconds));
186  }
187  bNeedCommit = true;
188  }
189  if (bNeedCommit)
190  aConnectionPoolRoot.commit();
191  }
192 
193 
194 } // namespace offapp
195 
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool GetValue() const
static OUString getConnectionPoolNodeName()
static void GetOptions(SfxItemSet &_rFillItems)
#define SID_SB_POOLING_ENABLED
static OUString getEnableNodeName()
#define SID_SB_DRIVER_TIMEOUTS
static OUString getEnablePoolingNodeName()
DriverSettings::iterator iterator
const DriverPoolingSettings & getSettings() const
void push_back(const DriverPooling &_rElement)
static void SetOptions(const SfxItemSet &_rSourceItems)
static OUString getDriverNameNodeName()
static OUString getDriverSettingsNodeName()
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
simple class for accessing SDBC drivers registered within the office
const_iterator end() const
Reference< XComponentContext > getProcessComponentContext()
static OUString getTimeoutNodeName()
const_iterator begin() const
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const