commit 74f1db3a130a3149a98d54f2eeb625276f47a4ad
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Tue Oct 13 00:42:46 2009 +0200

    [evol] improve config #7 (move shadow loading in the Controller)

diff --git a/TODO b/TODO
index 7e31cc1..91589dd 100644
--- a/TODO
+++ b/TODO
@@ -3,6 +3,8 @@
     each type in a separate subdirectory (only global config on top)
   * handle relations globally to allow sharing them among objects
   * validate each file with a schema (using kwalify)
+  * create global config and support loading a selected Shadow (a default
+     Shadow can be given in the global config)
 - select parent at creation with :parent ? this could be integrated in
    common code to be able to relocate an item
 - find a way to restrict parent locations for new objects (regex for
diff --git a/bin/shadowwalker b/bin/shadowwalker
index 26b0673..847b2ae 100755
--- a/bin/shadowwalker
+++ b/bin/shadowwalker
@@ -33,21 +33,6 @@ include LdapShadows
 
 set_locale(locale)
 
-config_str = IO.read(File.join(LdapShadows::Config::CFG_DIR, "shadow.conf"))
-config = YAML.load(config_str)
-config_str_prv_filelist = [
-  File.join(ENV['HOME'], ".shadowwalker"),
-  File.join(LdapShadows::Config::CFG_DIR, "shadow_private.conf")
-  ]
-config_str_prv_filelist.each do |file|
-  if File.exists?(file)
-    config_str_prv = IO.read(file)
-    config.merge!(YAML.load(config_str_prv))
-    break
-  end
-end
-config.recursive_symbolize_keys!
-
 
 $program_options = {}
 
@@ -68,37 +53,15 @@ cmdparser.add_command(CmdParse::HelpCommand.new)
 cmdparser.add_command(CmdParse::VersionCommand.new)
 
 
-def load_config_components(type)
-  c_config_dir = File.join(LdapShadows::Config::CFG_DIR, type)
-  c_config_pattern = File.join(c_config_dir, "**", "*.conf")
-
-  Dir.glob(c_config_pattern).each do |f|
-    next if f[0..0] == "."
-
-    c_name = File.basename(f).sub(".conf", "").to_sym
-    c_config = YAML.load(IO.read(f))
-    c_config.recursive_symbolize_keys!
-
-    yield(c_name, c_config)
-  end
-end
-
 $ldapctl = Controller.new
-$ldapctl.set_global_config(config[:presentation])
-load_config_components("aspects") do |c_name, c_config|
-  $ldapctl.set_aspect(c_name, c_config)
-end
 begin
-  ActiveLdap::Base.setup_connection(config[:ldap])
-
-  load_config_components("objects") do |c_name, c_config|
-    $ldapctl.load_object(c_name, c_config)
-  end
-
-  $ldapctl.load_relations
+  $ldapctl.load_shadow()
 rescue ActiveLdap::Error => e
   STDERR.puts _("LDAP connection error: %s") % e.to_s
   exit 2
+rescue
+  STDERR.puts _("Cannot load shadow configuration")
+  exit 2
 end
 
 
diff --git a/lib/ldap_shadows/controller.rb b/lib/ldap_shadows/controller.rb
index 9cbc8b3..701f614 100755
--- a/lib/ldap_shadows/controller.rb
+++ b/lib/ldap_shadows/controller.rb
@@ -28,12 +28,17 @@ module LdapShadows
   class Controller
     def initialize(mod_container = LdapShadows::Objects)
       @mod_container = mod_container
+
+      clear_shadow
+      LdapShadows::LdapObject.mapper = self
+    end
+
+    def clear_shadow
       @object_definitions = {}
       @aspects = {}
-      # should replace @aspects properly one day
+      # TODO: should replace @aspects properly one day
       @aspects2 = {}
-
-      LdapShadows::LdapObject.mapper = self
+      @shadow_dir = nil
     end
 
     def set_global_config(global_config)
@@ -154,6 +159,59 @@ module LdapShadows
     def objects
       @object_definitions.keys.collect{|key| key.to_s }.sort
     end
+
+    def load_shadow(shadow_name = nil)
+      clear_shadow
+
+      # TODO: local global config and use shadow name to load corresponding Shadow (or defautl Shadow if nil)
+      @shadow_dir = LdapShadows::Config::CFG_DIR
+
+      config_str = IO.read(File.join(@shadow_dir, "shadow.conf"))
+      config = YAML.load(config_str)
+      config_str_prv_filelist = [
+        File.join(ENV['HOME'], ".shadowwalker"),
+        File.join(@shadow_dir, "shadow_private.conf")
+        ]
+      config_str_prv_filelist.each do |file|
+        if File.exists?(file)
+          config_str_prv = IO.read(file)
+          config.merge!(YAML.load(config_str_prv))
+          break
+        end
+      end
+      config.recursive_symbolize_keys!
+
+      $ldapctl.set_global_config(config[:presentation])
+
+      load_config_components("aspects") do |c_name, c_config|
+        $ldapctl.set_aspect(c_name, c_config)
+      end
+
+      ActiveLdap::Base.setup_connection(config[:ldap])
+
+      load_config_components("objects") do |c_name, c_config|
+        $ldapctl.load_object(c_name, c_config)
+      end
+
+      $ldapctl.load_relations
+    end
+
+    protected
+
+    def load_config_components(type)
+      c_config_dir = File.join(@shadow_dir, type)
+      c_config_pattern = File.join(c_config_dir, "**", "*.conf")
+
+      Dir.glob(c_config_pattern).each do |f|
+        next if f[0..0] == "."
+
+        c_name = File.basename(f).sub(".conf", "").to_sym
+        c_config = YAML.load(IO.read(f))
+        c_config.recursive_symbolize_keys!
+
+        yield(c_name, c_config)
+      end
+    end
   end
 
   # default location for mapped objects
