Confdump Agent
1.4.0
|
00001 /* 00002 * Confdump-Agent - Dump static and runtime system configuration 00003 * Copyright (C) 2009-2012 Straton IT, SAS 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 3 as 00007 * published by the Free Software Foundation. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "boost/foreach.hpp" 00019 00020 #include "Confdump/Dumper.hpp" 00021 #include "Confdump/DumperSpecification.hpp" 00022 #include "Confdump/DumperFactory.hpp" 00023 #include "Confdump/RuntimeError.hpp" 00024 00025 #if CD_UNIX 00026 #include "Confdump/System/LinuxDumper.hpp" 00027 #elif CD_WIN32 00028 #include "Confdump/System/WmiDumper.hpp" 00029 #include "Confdump/System/Win32Dumper.hpp" 00030 #endif 00031 00032 using boost::shared_ptr; 00033 00034 namespace Confdump 00035 { 00036 00037 DumperFactory::DumperFactory() 00038 { 00039 specifications_["main"] = mainDumperSpecification(); 00040 } 00041 00042 DumperFactory::~DumperFactory() 00043 { 00044 } 00045 00046 DumperSpecification DumperFactory::mainDumperSpecification() 00047 { 00048 DumperSpecification spec; 00049 #if CD_UNIX 00050 spec.name = "linux"; 00051 #elif CD_WIN32 00052 spec.name = "wmi"; 00053 #endif 00054 return spec; 00055 } 00056 00057 shared_ptr<Dumper> DumperFactory::createDumper( DumperSpecification spec ) 00058 { 00059 QString name = spec.name; 00060 00061 #if CD_UNIX 00062 if ( name == "linux" ) 00063 return shared_ptr<Dumper>( new System::LinuxDumper ); 00064 #elif CD_WIN32 00065 if ( name == "wmi" ) 00066 { 00067 QString ns = spec.parameters["namespace"]; 00068 if ( !ns.isEmpty() ) 00069 return shared_ptr<Dumper>( new System::WmiDumper( ns ) ); 00070 else 00071 return shared_ptr<Dumper>( new System::WmiDumper ); 00072 } 00073 else if ( name == "win32" ) 00074 { 00075 return shared_ptr<Dumper>( new System::Win32Dumper ); 00076 } 00077 #endif 00078 00079 throw RuntimeError( "No such Dumper " + spec.name ); 00080 } 00081 00082 void DumperFactory::registerSpecifications( const SpecificationsMap &specs ) 00083 { 00084 typedef std::map<QString, DumperSpecification> collection; 00085 BOOST_FOREACH( collection::value_type pair, specs ) 00086 { 00087 if ( pair.first == "main" ) 00088 throw RuntimeError( "The 'main' dumper alias may not be redefined" ); 00089 specifications_[pair.first] = pair.second; 00090 } 00091 } 00092 00093 shared_ptr<Dumper> DumperFactory::dumper( QString alias ) 00094 { 00095 if ( cache_.find( alias ) != cache_.end() ) 00096 { 00097 return cache_[alias]; 00098 } 00099 else 00100 { 00101 if ( specifications_.find( alias ) == specifications_.end() ) 00102 throw RuntimeError( "No such Dumper alias " + alias ); 00103 cache_[alias] = createDumper( specifications_[alias] ); 00104 return cache_[alias]; 00105 } 00106 } 00107 00108 QStringList DumperFactory::aliasList() const 00109 { 00110 QStringList list; 00111 00112 BOOST_FOREACH( SpecificationsMap::value_type spec, specifications_ ) 00113 list << spec.first; 00114 00115 return list; 00116 } 00117 00118 }