(file) Return to ConfigFileHandler.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Config

  1 martin 1.26 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.27 //
  3 martin 1.26 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.27 //
 10 martin 1.26 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.27 //
 17 martin 1.26 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.27 //
 20 martin 1.26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.27 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.27 //
 28 martin 1.26 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32             
 33             #include <fstream>
 34 kumpf  1.12 #include <errno.h>
 35 mike   1.2  #include <Pegasus/Common/FileSystem.h>
 36             #include <Pegasus/Common/HashTable.h>
 37 kumpf  1.5  #include <Pegasus/Common/Tracer.h>
 38 kumpf  1.22 #include <Pegasus/Common/Executor.h>
 39 mike   1.2  #include "ConfigFileHandler.h"
 40 kumpf  1.3  #include "ConfigManager.h"
 41 mike   1.2  
 42             PEGASUS_USING_STD;
 43             
 44             PEGASUS_NAMESPACE_BEGIN
 45             
 46             ////////////////////////////////////////////////////////////////////////////////
 47             //
 48             //  ConfigFileHandler Class
 49             //
 50             ////////////////////////////////////////////////////////////////////////////////
 51             
 52             
 53             ////////////////////////////////////////////////////////////////////////////////
 54 kumpf  1.21 // ConfigTable
 55 mike   1.2  ////////////////////////////////////////////////////////////////////////////////
 56             
 57             typedef HashTable<String, String, EqualFunc<String>, HashFunc<String> > Table;
 58             
 59             struct ConfigTable
 60             {
 61                 Table table;
 62             };
 63             
 64             
 65 kumpf  1.21 /**
 66 mike   1.2      Constructor.
 67             */
 68 kumpf  1.21 ConfigFileHandler::ConfigFileHandler(
 69                 const String& currentFile,
 70 mike   1.2      const String& plannedFile,
 71                 const Boolean offLine)
 72                 : _offLine(offLine)
 73             {
 74 kumpf  1.3      String cFile;
 75                 String pFile;
 76 mike   1.2  
 77 kumpf  1.3      //
 78                 // Set the current and planned config files
 79                 //
 80                 cFile = ConfigManager::getHomedPath(currentFile);
 81             
 82                 pFile = ConfigManager::getHomedPath(plannedFile);
 83 mike   1.2  
 84                 //
 85                 // Initialize instance variables.
 86                 //
 87 kumpf  1.21 
 88 mike   1.2      _currentFileExist = true;
 89                 _plannedFileExist = true;
 90             
 91             
 92 a.arora 1.16     _currentConfFile.reset(new ConfigFile(cFile));
 93                  _plannedConfFile.reset(new ConfigFile(pFile));
 94 mike    1.2  
 95                  _currentConfig = new ConfigTable;
 96                  _plannedConfig = new ConfigTable;
 97              
 98                  //
 99                  // check whether the planned file exists or not
100                  //
101                  if (!FileSystem::exists(pFile))
102                  {
103                      _plannedFileExist = false;
104                      return;
105                  }
106              
107                  //
108                  // check whether the file is readable or not
109                  //
110                  if (!FileSystem::canRead(pFile))
111                  {
112                      throw FileNotReadable(pFile);
113                  }
114              
115 mike    1.2      //
116                  // check whether the current file exists or not
117                  //
118                  if (!FileSystem::exists(cFile))
119                  {
120                      _currentFileExist = false;
121                      //
122                      // Current file need not exist.
123                      // try creating one so that planned file contents
124                      // can be copied over.
125                      //
126 kumpf   1.22 
127                      FILE* is = Executor::openFile(cFile.getCString(), 'w');
128              
129                      if (!is)
130 mike    1.2          {
131 kumpf   1.12             // unable to create file
132 thilo.boehm 1.25             PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
133                                  "Failed to create config file: %s",
134                                   (const char*)cFile.getCString()));
135 mike        1.2              throw NoSuchFile(cFile);
136                          }
137 kumpf       1.22 
138                          fclose(is);
139 mike        1.2      }
140                  
141                      //
142                      // check whether the file is readable or not
143                      //
144                      if (!FileSystem::canRead(cFile))
145                      {
146                          throw FileNotReadable(cFile);
147                      }
148                  
149                  }
150                  
151 kumpf       1.21 /**
152                      Destructor.
153 mike        1.2  */
154 kumpf       1.21 ConfigFileHandler::~ConfigFileHandler()
155 mike        1.2  {
156                      //
157                      // delete tables
158                      //
159                      delete _currentConfig;
160                      delete _plannedConfig;
161                  }
162                  
163 kumpf       1.21 /**
164                      Overwrites config properties in the current config file with the
165                      the config properties from the planned config file.
166 mike        1.2  
167                      The content of the current config file will be copied in to a
168 kumpf       1.21     backup (.bak) file before copying planned file contents over the
169 mike        1.2      current file.
170                  */
171                  void ConfigFileHandler::copyPlannedFileOverCurrentFile()
172                  {
173                      if (_plannedFileExist)
174                      {
175                          _currentConfFile->replace(_plannedConfFile->getFileName());
176 kumpf       1.7          _currentFileExist = true;
177 mike        1.2      }
178                      else if (_currentFileExist)
179                      {
180                          //
181                          // Remove the current file
182                          //
183 kumpf       1.22 
184                          Executor::removeFile(
185                              _currentConfFile->getFileName().getCString());
186 mike        1.2      }
187                  }
188                  
189 kumpf       1.21 /**
190 mike        1.2      Load the config properties from the config files.
191                  */
192 kumpf       1.21 void ConfigFileHandler::loadAllConfigProperties()
193 mike        1.2  {
194                      loadCurrentConfigProperties();
195                  
196                      loadPlannedConfigProperties();
197                  }
198                  
199                  
200 kumpf       1.21 /**
201 mike        1.2      Load the config properties from the current config file.
202                  */
203 kumpf       1.21 void ConfigFileHandler::loadCurrentConfigProperties()
204 mike        1.2  {
205                      if (_currentFileExist)
206                      {
207                          _currentConfFile->load(_currentConfig);
208                      }
209                  }
210                  
211                  
212 kumpf       1.21 /**
213 mike        1.2      Load the config properties from the planned config file.
214                  */
215 kumpf       1.21 void ConfigFileHandler::loadPlannedConfigProperties()
216 mike        1.2  {
217                      if (_plannedFileExist)
218                      {
219                          _plannedConfFile->load(_plannedConfig);
220                      }
221                  }
222                  
223                  
224 kumpf       1.21 /**
225                      Update the specified property name and value in the current
226                      config file.
227 mike        1.2  */
228 kumpf       1.21 Boolean ConfigFileHandler::updateCurrentValue(
229                      const CIMName& name,
230 kumpf       1.4      const String& value,
231 venkat.puvvada 1.29     const String& userName,
232 venkat.puvvada 1.30     Uint32 timeoutSeconds,
233 kumpf          1.4      Boolean unset)
234 mike           1.2  {
235                         // Remove the old property name and value from the table
236 kumpf          1.9      if (_currentConfig->table.contains(name.getString()))
237 mike           1.2      {
238 kumpf          1.9          if (!_currentConfig->table.remove(name.getString()))
239 mike           1.2          {
240                                 return false;
241                             }
242                         }
243                     
244 kumpf          1.4      if (!unset)
245 mike           1.2      {
246                             // Store the new property name and value in to the table
247 kumpf          1.9          if (!_currentConfig->table.insert(name.getString(), value))
248 mike           1.2          {
249                                 return false;
250                             }
251                         }
252                     
253                         try
254                         {
255                             // Store the new property in current config file.
256                             _currentConfFile->save(_currentConfig);
257                         }
258 thilo.boehm    1.28     catch (Exception& e)
259 mike           1.2      {
260 thilo.boehm    1.25         PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
261 thilo.boehm    1.28             "Can not save current configuration: %s",
262 thilo.boehm    1.25             (const char*)e.getMessage().getCString()));
263 mike           1.2  
264 thilo.boehm    1.28         throw;
265 kumpf          1.14     }
266                     
267 mike           1.2      //
268                         // The current config file would now been created,
269                         // so set the flag to true.
270                         //
271                         _currentFileExist = true;
272                     
273                         return true;
274                     }
275                     
276                     
277 kumpf          1.21 /**
278                         Update the specified property name and value in the planned
279                         config file.
280 mike           1.2  */
281 kumpf          1.21 Boolean ConfigFileHandler::updatePlannedValue(
282                         const CIMName& name,
283 kumpf          1.4      const String& value,
284                         Boolean unset)
285 mike           1.2  {
286                         //
287                         // Remove the old property name and value from the table
288                         //
289 kumpf          1.9      if (_plannedConfig->table.contains(name.getString()))
290 mike           1.2      {
291 kumpf          1.9          if (!_plannedConfig->table.remove(name.getString()))
292 mike           1.2          {
293                                 return false;
294                             }
295                         }
296                     
297 kumpf          1.4      if (!unset)
298 mike           1.2      {
299                             //
300                             // Store the new property name and value in to the table
301                             //
302 kumpf          1.9          if (!_plannedConfig->table.insert(name.getString(), value))
303 mike           1.2          {
304                                 return false;
305                             }
306                         }
307                     
308                         try
309                         {
310                             //
311                             // Planned file need not exist for off line
312                             // configuration setting update.
313                             //
314                             if (_offLine)
315                             {
316                                 String pFile = _plannedConfFile->getFileName();
317                     
318 kumpf          1.22             FILE* fs = Executor::openFile(pFile.getCString(), 'w');
319                     
320                                 if (!fs)
321 mike           1.2              {
322 thilo.boehm    1.25                 PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
323                                         "Failed to create config file: %s",
324                                         (const char*)pFile.getCString()));
325 mike           1.2                  throw NoSuchFile(pFile);
326                                 }
327 kumpf          1.22 
328                                 fclose(fs);
329 mike           1.2          }
330                     
331                             //
332                             // Store the new property in planned config file.
333                             //
334                             _plannedConfFile->save(_plannedConfig);
335                     
336                         }
337 thilo.boehm    1.28     catch (Exception& e)
338 mike           1.2      {
339 thilo.boehm    1.25         PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
340 thilo.boehm    1.28             "Can not save planned configuration: %s",
341 thilo.boehm    1.25              (const char*)e.getMessage().getCString()));
342 mike           1.2  
343 thilo.boehm    1.28         throw;
344 kumpf          1.14     }
345                     
346 mike           1.2      //
347                         // The planned config file would now been created,
348                         // so set the flag to true.
349                         //
350                         _plannedFileExist = true;
351                     
352                         return true;
353                     }
354                     
355                     
356 kumpf          1.21 /**
357                         Get the current property value for the specified property name.
358 mike           1.2  */
359 kumpf          1.21 Boolean ConfigFileHandler::getCurrentValue(
360                         const CIMName& name,
361                         String& value) const
362 mike           1.2  {
363                         if (_currentFileExist)
364                         {
365 kumpf          1.9          return _currentConfig->table.lookup(name.getString(), value);
366 mike           1.2      }
367                     
368 kumpf          1.4      return false;
369 mike           1.2  }
370                     
371                     
372 kumpf          1.21 /**
373                         Get the planned property value for the specified property name.
374 mike           1.2  */
375 kumpf          1.21 Boolean ConfigFileHandler::getPlannedValue(
376                         const CIMName& name,
377                         String& value) const
378 mike           1.2  {
379                         if (_plannedFileExist)
380                         {
381 kumpf          1.9          return _plannedConfig->table.lookup(name.getString(), value);
382 mike           1.2      }
383                     
384 kumpf          1.4      return false;
385 mike           1.2  }
386                     
387                     
388 kumpf          1.21 /**
389 mike           1.2      Get all current property names.
390                     */
391 kumpf          1.21 void ConfigFileHandler::getAllCurrentPropertyNames(
392                         Array<CIMName>& propertyNames)
393 mike           1.2  {
394                         propertyNames.clear();
395                     
396                         if (_currentFileExist)
397                         {
398                             for (Table::Iterator i = _currentConfig->table.start(); i; i++)
399                             {
400                                 propertyNames.append(i.key());
401                             }
402                         }
403                     }
404                     
405                     
406 kumpf          1.21 /**
407 mike           1.2      Get all current property names and values.
408                     */
409 kumpf          1.21 void ConfigFileHandler::getAllCurrentProperties(
410                         Array<CIMName>& propertyNames,
411 mike           1.2      Array<String>& propertyValues)
412                     {
413                         propertyNames.clear();
414                         propertyValues.clear();
415                     
416                         if (_currentFileExist)
417                         {
418                             for (Table::Iterator i = _currentConfig->table.start(); i; i++)
419                             {
420                                 propertyNames.append(i.key());
421                                 propertyValues.append(i.value());
422                             }
423                         }
424                     }
425                     
426                     
427 kumpf          1.21 /**
428 mike           1.2      Get all planned property names and values.
429                     */
430 kumpf          1.21 void ConfigFileHandler::getAllPlannedPropertyNames(
431 kumpf          1.9      Array<CIMName>& propertyNames)
432 mike           1.2  {
433                         propertyNames.clear();
434                     
435                         if (_plannedFileExist)
436                         {
437                             for (Table::Iterator i = _plannedConfig->table.start(); i; i++)
438                             {
439                                 propertyNames.append(i.key());
440                             }
441                         }
442                     }
443                     
444                     
445 kumpf          1.21 /**
446 mike           1.2      Get all planned config property names and values.
447                     */
448 kumpf          1.21 void ConfigFileHandler::getAllPlannedProperties(
449                         Array<CIMName>& propertyNames,
450 mike           1.2      Array<String>& propertyValues)
451                     {
452                         propertyNames.clear();
453                         propertyValues.clear();
454                     
455                         if (_plannedFileExist)
456                         {
457                             for (Table::Iterator i = _plannedConfig->table.start(); i; i++)
458                             {
459                                 propertyNames.append(i.key());
460                                 propertyValues.append(i.value());
461                             }
462                         }
463                     }
464                     
465                     PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2