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 <svl/itemset.hxx>
25 #include <unotools/confignode.hxx>
26 #include <svl/eitem.hxx>
28 #include "sdbcdriverenum.hxx"
29 
30 #include <com/sun/star/uno/Sequence.hxx>
31 
32 namespace offapp
33 {
34 
35 
36  using namespace ::utl;
37  using namespace ::com::sun::star::uno;
38 
39 
40  static OUString getConnectionPoolNodeName()
41  {
42  return "org.openoffice.Office.DataAccess/ConnectionPool";
43  }
44 
45 
46  static OUString getEnablePoolingNodeName()
47  {
48  return "EnablePooling";
49  }
50 
51 
52  static OUString getDriverSettingsNodeName()
53  {
54  return "DriverSettings";
55  }
56 
57 
58  static OUString getDriverNameNodeName()
59  {
60  return "DriverName";
61  }
62 
63 
64  static OUString getEnableNodeName()
65  {
66  return "Enable";
67  }
68 
69 
70  static OUString getTimeoutNodeName()
71  {
72  return "Timeout";
73  }
74 
76  {
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);
80 
81  // the global "enabled" flag
82  Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
83  bool bEnabled = true;
84  aEnabled >>= bEnabled;
85  _rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
86 
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 (auto const& elem : aEnumDrivers)
92  {
93  aSettings.push_back(DriverPooling(elem));
94  }
95 
96  // then look for which of them settings are stored in the configuration
97  OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
98 
99  Sequence< OUString > aDriverKeys = aDriverSettings.getNodeNames();
100  const OUString* pDriverKeys = aDriverKeys.getConstArray();
101  const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
102  for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
103  {
104  // the name of the driver in this round
105  OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
106  OUString sThisDriverName;
107  aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
108 
109  // look if we (resp. the driver manager) know this driver
110  // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
112  for ( aLookup = aSettings.begin();
113  aLookup != aSettings.end();
114  ++aLookup
115  )
116  if (sThisDriverName == aLookup->sName)
117  break;
118 
119  if (aLookup == aSettings.end())
120  { // do not know the driver - add it
121  aSettings.push_back(DriverPooling(sThisDriverName));
122 
123  // and the position of the new entry
124  aLookup = aSettings.end();
125  --aLookup;
126  }
127 
128  // now fill this entry with the settings from the configuration
129  aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
130  aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
131  }
132 
133  _rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
134  }
135 
136 
137  void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
138  {
139  // the config node where all pooling relevant info are stored under
140  OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithComponentContext(
142 
143  if (!aConnectionPoolRoot.isValid())
144  // already asserted by the OConfigurationTreeRoot
145  return;
146 
147  bool bNeedCommit = false;
148 
149  // the global "enabled" flag
150  const SfxBoolItem* pEnabled = _rSourceItems.GetItem<SfxBoolItem>(SID_SB_POOLING_ENABLED);
151  if (pEnabled)
152  {
153  bool bEnabled = pEnabled->GetValue();
154  aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(bEnabled));
155  bNeedCommit = true;
156  }
157 
158  // the settings for the single drivers
160  if (pDriverSettings)
161  {
162  OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
163  if (!aDriverSettings.isValid())
164  return;
165 
166  OUString sThisDriverName;
167  OConfigurationNode aThisDriverSettings;
168 
169  const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
170  for (auto const& newSetting : rNewSettings)
171  {
172  // need the name as OUString
173  sThisDriverName = newSetting.sName;
174 
175  // the sub-node for this driver
176  if (aDriverSettings.hasByName(newSetting.sName))
177  aThisDriverSettings = aDriverSettings.openNode(newSetting.sName);
178  else
179  aThisDriverSettings = aDriverSettings.createNode(newSetting.sName);
180 
181  // set the values
182  aThisDriverSettings.setNodeValue(getDriverNameNodeName(), Any(sThisDriverName));
183  aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(newSetting.bEnabled));
184  aThisDriverSettings.setNodeValue(getTimeoutNodeName(), Any(newSetting.nTimeoutSeconds));
185  }
186  bNeedCommit = true;
187  }
188  if (bNeedCommit)
189  aConnectionPoolRoot.commit();
190  }
191 
192 
193 } // namespace offapp
194 
195 
196 /* 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