Module: nagvis
Branch: master
Commit: 9ec278827590a36ba17d2aeff5695525a3d2acc2
URL:    
http://nagvis.git.sourceforge.net/git/gitweb.cgi?p=nagvis/nagvis;a=commit;h=9ec278827590a36ba17d2aeff5695525a3d2acc2

Author: Lars Michelsen <[email protected]>
Date:   Wed Sep 29 21:11:00 2010 +0200

Implemented optional second main configuration file to pre-define values which 
might be out of the users scope

---

 install.sh                                    |    3 +-
 share/frontend/wui/classes/WuiCore.php        |    8 ++-
 share/server/core/classes/GlobalCore.php      |    8 ++-
 share/server/core/classes/GlobalFileCache.php |    2 +-
 share/server/core/classes/GlobalMainCfg.php   |  111 +++++++++++--------------
 share/server/core/defines/global.php          |   14 +++
 6 files changed, 76 insertions(+), 70 deletions(-)

diff --git a/install.sh b/install.sh
index ceafaee..6b627e4 100755
--- a/install.sh
+++ b/install.sh
@@ -1436,8 +1436,9 @@ if [ "$INSTALLER_ACTION" = "update" -a "$NAGVIS_VER_OLD" 
!= "UNKNOWN" ]; then
        USERFILES_DIR="share/userfiles"
 
        if [ $NAGVIS_TAG_OLD -ge 01050000 ]; then
-               LINE="Restoring main configuration file..."
+               LINE="Restoring main configuration file(s)..."
                copy "" "$NAGVIS_CONF" "main configuration file"
+               copy "" "etc/nagvis-site.ini.php" "site main configuration file"
        
                LINE="Restoring custom map configuration files..."
                copy "\/(demo\.cfg|demo2\.cfg|demo-server\.cfg|demo-map\.cfg)$" 
"etc/maps" "map configuration files"
diff --git a/share/frontend/wui/classes/WuiCore.php 
b/share/frontend/wui/classes/WuiCore.php
index 737a4dd..31d4bd0 100644
--- a/share/frontend/wui/classes/WuiCore.php
+++ b/share/frontend/wui/classes/WuiCore.php
@@ -42,9 +42,13 @@ class WuiCore extends GlobalCore {
         * @author Lars Michelsen <[email protected]>
         */
        public static function getMainCfg() {
+               // Initialize main configuration when not set yet
                if(parent::$MAINCFG === null) {
-                       // Initialize main configuration when not set yet
-                       parent::$MAINCFG = new WuiMainCfg(CONST_MAINCFG);
+                       if(defined('CONST_MAINCFG_SITE'))
+                               parent::$MAINCFG = new 
WuiMainCfg(Array(CONST_MAINCFG_SITE, CONST_MAINCFG));
+                       else
+                               parent::$MAINCFG = new 
WuiMainCfg(Array(CONST_MAINCFG));
+
                        parent::$MAINCFG->init();
                        
                        // Set WuiCore MAINCFG too
diff --git a/share/server/core/classes/GlobalCore.php 
b/share/server/core/classes/GlobalCore.php
index 413f098..db2f194 100644
--- a/share/server/core/classes/GlobalCore.php
+++ b/share/server/core/classes/GlobalCore.php
@@ -56,9 +56,13 @@ class GlobalCore {
         * @author Lars Michelsen <[email protected]>
         */
        public static function getMainCfg() {
+               // Initialize main configuration when not set yet
                if(self::$MAINCFG === null) {
-                       // Initialize main configuration when not set yet
-                       self::$MAINCFG = new GlobalMainCfg(CONST_MAINCFG);
+                       if(defined('CONST_MAINCFG_SITE'))
+                               self::$MAINCFG = new 
GlobalMainCfg(Array(CONST_MAINCFG_SITE, CONST_MAINCFG));
+                       else
+                               self::$MAINCFG = new 
GlobalMainCfg(Array(CONST_MAINCFG));
+
                        self::$MAINCFG->init();
                }
                
diff --git a/share/server/core/classes/GlobalFileCache.php 
b/share/server/core/classes/GlobalFileCache.php
index ec17eee..990bd8e 100644
--- a/share/server/core/classes/GlobalFileCache.php
+++ b/share/server/core/classes/GlobalFileCache.php
@@ -100,7 +100,7 @@ class GlobalFileCache {
         * @return  Integer  Unix timestamp of cache creation time or -1 when 
not cached
         * @author  Lars Michelsen <[email protected]>
         */
-       public function isCached($printErr=0) {
+       public function isCached($printErr = false) {
                // Checks
                // a) Cache file exists
                // b) Cache file older than regular file
diff --git a/share/server/core/classes/GlobalMainCfg.php 
b/share/server/core/classes/GlobalMainCfg.php
index 17df69d..9c79c74 100644
--- a/share/server/core/classes/GlobalMainCfg.php
+++ b/share/server/core/classes/GlobalMainCfg.php
@@ -26,26 +26,24 @@
  * @author     Lars Michelsen <[email protected]>
  */
 class GlobalMainCfg {
-       private $CACHE;
+       private $useCache = true;
+       private $CACHES;
        
-       protected $config;
-       protected $runtimeConfig;
+       protected $config = Array();
+       protected $runtimeConfig = Array();
        protected $stateWeight;
        
-       protected $configFile;
+       protected $configFiles;
        
        protected $validConfig;
        
        /**
         * Class Constructor
         *
-        * @param       String  $configFile                     String with 
path to config file
+        * @param       Array $configFile    List of paths to configuration 
files
         * @author Lars Michelsen <[email protected]>
         */
-       public function __construct($configFile) {
-               $this->config = Array();
-               $this->runtimeConfig = Array();
-               
+       public function __construct($configFiles) {
                $this->validConfig = Array(
                        'global' => Array(
                                'audit_log' => Array('must' => 1,
@@ -889,32 +887,39 @@ class GlobalMainCfg {
                $this->validConfig['paths']['base']['default'] = 
$this->getBasePath();
                
$this->setPathsByBase($this->getValue('paths','base'),$this->getValue('paths','htmlbase'));
                
-               // Define the main configuration file
-               $this->configFile = $configFile;
+               // Define the main configuration files
+               $this->configFiles = $configFiles;
        }
-       
-       public function init() {
-               // Do preflight checks
+
+       private function loadConfigFile($file) {
                // Only proceed when the configuration file exists and is 
readable
-               if(!$this->checkNagVisConfigExists(TRUE) || 
!$this->checkNagVisConfigReadable(TRUE)) {
-                       return FALSE;
-               }
+               if(!GlobalCore::getInstance()->checkExisting($file, true) || 
!GlobalCore::getInstance()->checkReadable($file, true))
+                       return false;
                
                // Create instance of GlobalFileCache object for caching the 
config
-               $this->CACHE = new GlobalFileCache(GlobalCore::getInstance(), 
$this->configFile, CONST_MAINCFG_CACHE.'-'.CONST_VERSION.'-cache');
-               
+               $id = count($this->CACHES);
+               $this->CACHES[$id] = new 
GlobalFileCache(GlobalCore::getInstance(), $file, 
CONST_MAINCFG_CACHE.'-'.CONST_VERSION.'-cache');
+
+               // When this or a file before is newer than the cache parse the 
file
+               if($this->useCache > 0)
+               $this->useCache = $this->CACHES[$id]->isCached(false);
+               if($this->useCache === -1)
+                       return $this->readConfig($file, true);
+       }
+       
+       public function init() {
                // Get the valid configuration definitions from the available 
backends
                $this->getBackendValidConf();
-               
-               if($this->CACHE->isCached(FALSE) !== -1) {
-                       $this->config = $this->CACHE->getCache();
-               } else {
-                       
-                       // Read Main Config file, when succeeded cache it
-                       if($this->readConfig(TRUE)) {
-                               $this->CACHE->writeCache($this->config, TRUE);
-                       }
-               }
+
+               // Load all given config files
+               foreach($this->configFiles AS $configFile)
+                       $this->loadConfigFile($configFile);
+
+               // Use or write the cache depending on the situation
+               if($this->useCache !== -1)
+                       $this->config = $this->CACHES[0]->getCache();
+               else
+                       $this->CACHES[0]->writeCache($this->config, true);
 
                // Parse the state weight array
                $this->parseStateWeight();
@@ -1028,18 +1033,18 @@ class GlobalMainCfg {
        }
        
        /**
-        * Reads the config file specified in $this->configFile
+        * Reads the specified config file
         *
         * @param       Boolean $printErr
         * @return      Boolean Is Successful?
         * @author      Lars Michelsen <[email protected]>
         */
-       private function readConfig($printErr=1) {
+       private function readConfig($configFile, $printErr=1) {
                $numComments = 0;
                $sec = '';
                
                // read thx config file line by line in array $file
-               $file = file($this->configFile);
+               $file = file($configFile);
                
                // Count the lines before the loop (only counts once)
                $countLines = count($file);
@@ -1142,11 +1147,10 @@ class GlobalMainCfg {
                                        }
                                        
                                        // write in config array
-                                       if(isset($sec)) {
+                                       if(isset($sec))
                                                $this->config[$sec][$key] = 
$val;
-                                       } else {
+                                       else
                                                $this->config[$key] = $val;
-                                       }
                                }
                        } else {
                                $sec = '';
@@ -1154,11 +1158,7 @@ class GlobalMainCfg {
                        }
                }
                
-               if($this->checkMainConfigIsValid(1)) {
-                       return TRUE;
-               } else {
-                       return FALSE;
-               }
+               return $this->checkMainConfigIsValid(1);
        }
        
        /**
@@ -1273,35 +1273,18 @@ class GlobalMainCfg {
        }
        
        /**
-        * Checks for existing config file
-        *
-        * @param       Boolean $printErr
-        * @return      Boolean Is Successful?
-        * @author      Lars Michelsen <[email protected]>
-        */
-       private function checkNagVisConfigExists($printErr) {
-               return 
GlobalCore::getInstance()->checkExisting($this->configFile, $printErr);
-       }
-       
-       /**
-        * Checks for readable config file
-        *
-        * @param       Boolean $printErr
-        * @return      Boolean Is Successful?
-        * @author      Lars Michelsen <[email protected]>
-        */
-       private function checkNagVisConfigReadable($printErr) {
-               return 
GlobalCore::getInstance()->checkReadable($this->configFile, $printErr);
-       }
-       
-       /**
         * Returns the last modification time of the configuration file
         *
         * @return      Integer Unix Timestamp
         * @author      Lars Michelsen <[email protected]>
         */
        public function getConfigFileAge() {
-               return filemtime($this->configFile);
+               $newest = 0;
+               foreach($this->configFiles AS $configFile) {
+                       $age = filemtime($configFile);
+                       $newest = ($age > $newest ? $age : $newest);
+               }
+               return $newest;
        }
        
        /**
@@ -1312,7 +1295,7 @@ class GlobalMainCfg {
         * @author  Lars Michelsen <[email protected]>
         */
        public function isCached() {
-               return $this->CACHE->isCached();
+               return $this->useCache;
        }
        
        /**
diff --git a/share/server/core/defines/global.php 
b/share/server/core/defines/global.php
index 656c82f..4efa67c 100644
--- a/share/server/core/defines/global.php
+++ b/share/server/core/defines/global.php
@@ -60,10 +60,24 @@ define('DEBUGLEVEL', 6);
 // Path to the debug file
 define('DEBUGFILE', '../../../var/nagvis-debug.log');
 
+// It is possible to define a second main configuration file
+// to pre-define some options in a file the user may not be
+// able to modify.
+//
+// The parameters are applied in this direction:
+// 1. hardcoded
+// 2. CONST_MAINCFG_SITE
+// 3. CONST_MAINCFG
+//
+// The last value wins.
+//
 // Path to the main configuration file
 define('CONST_MAINCFG', '../../../etc/nagvis.ini.php');
 define('CONST_MAINCFG_CACHE', '../../../var/nagvis.ini.php');
 
+// Path to "site" main configuration file
+define('CONST_MAINCFG_SITE', '../../../etc/nagvis-site.ini.php');
+
 // Needed minimal PHP version
 define('CONST_NEEDED_PHP_VERSION', '5.0');
 


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Nagvis-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nagvis-checkins

Reply via email to