Hauptseite | Liste aller Namensbereiche | Klassenhierarchie | Übersicht | Auflistung der Dateien | Elemente eines Namensbereiches | Datenstruktur-Elemente | Datei-Elemente

Admin.cpp

gehe zur Dokumentation dieser Datei
00001 // Copyright (C) 2004 Bernhard Assmann <bernie@tuxomania.net>
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //  
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //  
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //  
00017 
00018 #include <iostream>
00019 #include <iterator>
00020 #include <cstdlib>
00021 
00022 #include "psfa/Admin.hpp"
00023 #include "psfa/Helper.hpp"
00024 #include "psfa/DbWriteDriverMySql.hpp"
00025 #include "psfa/Config.hpp"
00026 #include "psfa/CheckData.hpp"
00027 
00028 
00029 
00030 
00031 psfa::Admin::Admin() 
00032   : db_( 0 ),
00033     success_( false )
00034 {
00035 
00036 
00037 }
00038 
00039 
00040 
00041 psfa::Admin::~Admin() 
00042 {
00043   delete db_;
00044   db_ = 0;
00045 }
00046 
00047 
00048 
00049 void
00050 psfa::Admin::init() 
00051 {
00052   setUpEnviroment();
00053   if( !success_ ) {
00054     return;
00055   }
00056   db_->initPsfa();
00057   return;
00058 }
00059 
00060 
00061 
00062 void
00063 psfa::Admin::del( const std::string poolName ) 
00064 {
00065   setUpEnviroment();
00066   if( !success_ ) {
00067     return;
00068   }
00069   if( db_->existPool( poolName ) ) {
00070     db_->delPool( poolName );
00071     success_ = true;
00072     return;
00073   }
00074   std::cout << "Fehler: Der Pool " 
00075        << poolName 
00076        << " existiert nicht." 
00077        << std::endl;
00078   success_ = false;
00079   return;
00080 }
00081 
00082 
00083 
00084 void
00085 psfa::Admin::index( const std::string poolName ) 
00086 {
00087   setUpEnviroment();
00088   if( !success_ ) {
00089     return;
00090   }
00091   if( !db_->existPool( poolName ) ) {
00092     std::cout << "Fehler: Der Pool " 
00093          << poolName 
00094          << " existiert nicht." 
00095          << std::endl;
00096     success_ = false;
00097     return;
00098   }
00099   
00100   CheckData data;
00101   db_->check( data );
00102   if( data.init ) {
00103     std::cout << "Die Konfiguration und die Datenbank stimmen nicht überein.\n"
00104          << "Es wird geraten 'psfa init' auszuführen."
00105          << "Die Aktion wurde abgebrochen."
00106          << std::endl;
00107     return;
00108   }
00109   int count = db_->index( poolName, data );
00110   std::cout << "Im Pool "
00111        << poolName
00112        << " sind "
00113        << count
00114        << " Tags indiziert."
00115        << std::endl;
00116   success_ = db_->success();
00117   return;
00118 }
00119 
00120 
00121 
00122 void
00123 psfa::Admin::check() 
00124 {
00125   using std::cout;
00126   using std::endl;
00127   using std::string;
00128   using std::ostream_iterator;
00129 
00130   setUpEnviroment();
00131   if( !success_ ) {
00132     return;
00133   }
00134 
00135   bool upToDate = true;
00136   CheckData data;
00137   db_->check( data );
00138   if( data.init ) {
00139     cout << "Die Konfiguration und die Datenbank stimmen nicht überein.\n"
00140     << "Es wird geraten 'psfa init' und danach "
00141     << "'psfa index Poolname' auszuführen.";
00142     if( data.poolDel ) {
00143       cout << "\n"
00144       << "Außerdem befinden sich in der Datenbank noch folgende Pools,\n"
00145       << "die nicht mehr in der Konfiguration enthalten sind:\n"
00146       << "\n";
00147       copy( data.poolDelList.begin(),
00148        data.poolDelList.end(),
00149        ostream_iterator<string>( cout, " " ) );
00150       cout << "\n\n";
00151       cout << "Sie sollten mit 'psfa delete Poolname' gelöscht werden";
00152     }
00153     cout << endl;
00154     upToDate = false;
00155   }
00156   bool index = false;
00157   if( data.poolIndex && upToDate ) {
00158     cout << "Folgende Dateien sind noch nicht indiziert:\n";
00159     typedef std::map< std::string, std::vector< std::string > > vMap;
00160     typedef std::vector< std::string > sVec;
00161     vMap::iterator i;
00162     vMap::iterator iEnd = data.poolIndexList.end();
00163     for( i = data.poolIndexList.begin(); i != iEnd; ++i ) {
00164       sVec tmp = i->second;
00165       cout << "Im Pool: " << i->first << "\n";
00166       copy( tmp.begin(),
00167        tmp.end(),
00168        ostream_iterator<string>( cout, " " ) );
00169       cout << endl;
00170     }
00171     index = true;
00172   }
00173   if( data.poolInsert && upToDate ) {
00174     cout << "Folgende Dateien sind neu (und nicht indiziert):\n";
00175     typedef std::map< std::string, std::vector< std::string > > vMap;
00176     typedef std::vector< std::string > sVec;
00177     vMap::iterator i;
00178     vMap::iterator iEnd = data.poolInsertList.end();
00179     for( i = data.poolInsertList.begin(); i != iEnd; ++i ) {
00180       sVec tmp = i->second;
00181       if( !tmp.empty() ) {
00182    cout << "Im Pool: " << i->first << "\n";
00183    copy( tmp.begin(),
00184          tmp.end(),
00185          ostream_iterator<string>( cout, " " ) );
00186    cout << endl;
00187       }
00188     }
00189     index = true;
00190   }
00191   if( data.poolUpdate && upToDate ) {
00192     cout << "Folgende Dateien wurden geändert:\n";
00193     typedef std::map< std::string, std::vector< std::string > > vMap;
00194     typedef std::vector< std::string > sVec;
00195     vMap::iterator i;
00196     vMap::iterator iEnd = data.poolUpdateList.end();
00197     for( i = data.poolUpdateList.begin(); i != iEnd; ++i ) {
00198       sVec tmp = i->second;
00199       if( !tmp.empty() ) {
00200    cout << "Im Pool: " << i->first << "\n";
00201    copy( tmp.begin(),
00202          tmp.end(),
00203          ostream_iterator<string>( cout, " " ) );
00204    cout << endl;
00205       }
00206     }
00207     index = true;
00208   }
00209   if( data.poolRemove && upToDate ) {
00210     cout << "Folgende Dateien wurden gelöscht:\n";
00211     typedef std::map< std::string, std::vector< std::string > > vMap;
00212     typedef std::vector< std::string > sVec;
00213     vMap::iterator i;
00214     vMap::iterator iEnd = data.poolRemoveList.end();
00215     for( i = data.poolRemoveList.begin(); i != iEnd; ++i ) {
00216       sVec tmp = i->second;
00217       if( !tmp.empty() ) {
00218    cout << "Im Pool: " << i->first << "\n";
00219    copy( tmp.begin(),
00220          tmp.end(),
00221          ostream_iterator<string>( cout, " " ) );
00222    cout << endl;
00223       }
00224       
00225     }
00226     index = true;
00227   }
00228   if( index ) {
00229     cout << "Es wird geraten 'psfa index Poolname' auszuführen."
00230     << endl;
00231     upToDate = false;
00232   }
00233   if ( upToDate ) {
00234     cout << "Es ist alles auf dem letzten Stand." << endl;
00235   }
00236   cout << "Die Anzahl der indizierten Tags lautet:\n";
00237   typedef std::map< std::string, int > iMap;
00238   iMap::iterator i;
00239   iMap::iterator iEnd = data.indexCountList.end();
00240   for( i = data.indexCountList.begin(); i != iEnd; ++i ) {
00241     cout << "Im Pool: " 
00242     << i->first 
00243     << ", "
00244     << i->second
00245     << endl;
00246   }
00247 
00248   success_ = true;
00249   return;
00250 }
00251 
00252 
00253 
00254 bool
00255 psfa::Admin::isSuccess() const
00256 {
00257   return success_;
00258 }
00259 
00260 
00261 
00262 void
00263 psfa::Admin::setUpEnviroment() 
00264 {
00265   using std::cout;
00266   using std::endl;
00267   using psfahelper::getFilePaths;
00268   using psfahelper::splitString;
00269   using psfahelper::pathIsReadable;
00270   using psfahelper::pathIsDir;
00271   typedef std::vector< std::string > sVec;
00272 
00273   bool success = true;
00274   
00275   sVec psfaConfigs;
00276   psfaConfigs.reserve( 3 );
00277   const char* env = getenv("PSFA_CONFIG");
00278   if ( env ) {
00279     std::string pC( env );
00280     if( pathIsReadable( pC ) ) {
00281       psfaConfigs.push_back( pC );
00282     }
00283     delete env;
00284   }
00285   env = getenv("HOME");
00286   if ( env ) {
00287     std::string h( env );
00288     h += "/.psfa/psfa_conf.xml";
00289     if( pathIsReadable( h ) ) {
00290       psfaConfigs.push_back( h );
00291     }
00292     delete env;
00293   }
00294   std::string etc("/etc/psfa_conf.xml");
00295   if( pathIsReadable( etc ) ) {
00296     psfaConfigs.push_back( etc );
00297   }
00298   
00299   if( psfaConfigs.empty() ) {
00300     cout << "Es wurde keine Konfiguration gefunden.\n"
00301     << "Der Vorgang wurde abgebrochen."
00302     << endl;
00303     success_ = false;
00304     return;
00305   }
00306   
00307   Config* conf = Config::getConfig();
00308   sVec::iterator i;
00309   sVec::iterator iEnd = psfaConfigs.end();
00310   for( i = psfaConfigs.begin(); i != iEnd; ++i ) {
00311       conf->parseConfig( *i );
00312   }
00313   std::string pools = conf->getConfigValue( "psfa", "pools" );
00314   sVec inConfig;
00315   splitString( pools, inConfig );
00316   if( inConfig.empty() ) {
00317     cout << "Es sind keine Pools in der Konfiguration angegeben."
00318     << endl;
00319     success = false;
00320   }
00321   sVec::iterator p;
00322   sVec::iterator pEnd = inConfig.end();
00323   for( p = inConfig.begin(); p != pEnd; ++p ) {
00324     sVec tmpV;
00325     std::string tmpS;
00326 
00327     tmpS = conf->getConfigValue( *p, "tags" );
00328     splitString( tmpS, tmpV );
00329     if( tmpV.empty() ) {
00330       cout << "Für den Pool "
00331       << *p
00332       << " sind keine Tags, die indiziert werden sollen, angegeben."
00333       << endl;
00334       success = false;
00335     }
00336 
00337     tmpV.clear();
00338     tmpS = conf->getConfigValue( *p, "kontexts" );
00339     splitString( tmpS, tmpV );
00340     if( tmpV.empty() ) {
00341       cout << "Für den Pool "
00342       << *p
00343       << " sind keine Kontexte angegeben."
00344       << endl;
00345       success = false;
00346     }
00347 
00348     tmpS = conf->getConfigValue( *p, "root" );
00349     bool b = pathIsDir( tmpS );
00350     if( !b ) {
00351       cout << "Das Root-Verzeichnis des Pools "
00352       << *p
00353       << ", "
00354       << tmpS
00355       << ", ist kein Verzeichnis."
00356       << endl;
00357       success = false;
00358     }
00359     b = pathIsReadable( tmpS );
00360     if( !b ) {
00361       cout << "Das Root-Verzeichnis des Pools "
00362       << *p
00363       << ", "
00364       << tmpS
00365       << ", ist für den aktuellen Benutzer nicht lesbar."
00366       << endl;
00367       success = false;
00368     }
00369 
00370     tmpV.clear();
00371     getFilePaths( tmpS, "*.xml", tmpV );
00372     if( tmpV.empty() ) {
00373       cout << "Das Root-Verzeichnis des Pools "
00374       << *p
00375       << ", "
00376       << tmpS
00377       << ", enthält keine Dateien."
00378       << endl;
00379       success = false;
00380     }
00381     
00382     tmpS = conf->getConfigValue( *p, "unitTag" );
00383     if( tmpS.empty() ) {
00384       cout << "Für den Pool "
00385       << *p
00386       << " ist nicht angegeben, wie eine Einheit ermittelt werden kann."
00387       << endl;
00388       success = false;
00389     }
00390   }
00391 
00392   std::string tmpS;
00393 
00394   tmpS = conf->getConfigValue( "mysql", "host" );
00395   if( tmpS.empty() ) {
00396     cout << "Es fehlt die Angabe des Datanbank Hosts."
00397     << endl;
00398     success = false;
00399   }
00400   tmpS = conf->getConfigValue( "mysql", "user" );
00401   if( tmpS.empty() ) {
00402     cout << "Es fehlt die Angabe des Datanbank Users."
00403     << endl;
00404     success = false;
00405   }
00406   tmpS = conf->getConfigValue( "mysql", "password" );
00407   if( tmpS.empty() ) {
00408     cout << "Es fehlt die Angabe des Datanbank Passwortes."
00409     << endl;
00410     success = false;
00411   }
00412   tmpS = conf->getConfigValue( "mysql", "db" );
00413   if( tmpS.empty() ) {
00414     cout << "Es fehlt die Angabe des Datanbanknamens."
00415     << endl;
00416     success = false;
00417   }
00418 
00419   if( !success ) {
00420     cout << "Die Aktion wurde aufgrund der Fehler abgebrochen."
00421     << endl;
00422     return;
00423   }
00424 
00425   db_ = new DbWriteDriverMySql;
00426   success_ = true;
00427   return;
00428 }

Erzeugt am Mon Jul 12 11:45:04 2004 für PSFA von doxygen 1.3.4