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