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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2