commit 4b58d4fd19ee646b78356e72908d4b5a6b4fda28
Author: Marc Dequenes <duck@Orfeo.duckcorp.org>
Date:   Wed Feb 18 23:29:14 2009 +0100

    [evol] move new classes into CyborgHood module and add new exception handling

diff --git a/lib/cyborghood/base.rb b/lib/cyborghood/base.rb
index e2a8e1a..84c4dfc 100644
--- a/lib/cyborghood/base.rb
+++ b/lib/cyborghood/base.rb
@@ -10,7 +10,28 @@ module CyborgHood
   PRODUCT = "CyborgHood"
   VERSION = "0.0.1"
 
-  class Logger <  Log4r::Logger
+  # severities: # :dangerous :unrecoverable :ignorable
+  # categories:
+  #  - db
+  #    + ldap
+  #    + sql
+  #  - protocol
+  #    + imap
+  #  - service
+  #    + dns
+  class Error < StandardError
+    attr_accessor :severity, :category
+
+    #include GetTextSupport
+
+    def initialize(severity, category, message)
+      @severity = severity
+      @category = category
+      super(message)
+    end
+  end
+
+  class Logger < Log4r::Logger
     include Singleton
 
     LOG_FORMAT = "[%5l - %d] #PREFIX#%m"
diff --git a/lib/cyborghood/objects.rb b/lib/cyborghood/objects.rb
index 70aeede..438d608 100644
--- a/lib/cyborghood/objects.rb
+++ b/lib/cyborghood/objects.rb
@@ -1,68 +1,74 @@
 require 'cyborghood/objects/ldap'
 
-class DnsDomain < Delegator
-  attr_reader :ldap, :name
+module CyborgHood
+  class DnsDomain < Delegator
+    attr_reader :ldap, :name
 
-  def initialize(name)
-    @name = name
-    raise "invalid zone name" unless self.is_valid?
+    def initialize(name)
+      @name = name
+      raise "invalid zone name" unless self.is_valid?
 
-    # may not exist (if creating a new one)
-    begin
-      @ldap = LdapDnsDomain.find(name)
-    rescue
-      @ldap = nil
+      # may not exist (if creating a new one)
+      begin
+        @ldap = LdapDnsDomain.find(name)
+      rescue
+        @ldap = nil
+      end
     end
-  end
 
-  def self.is_valid?(name)
-    name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
-  end
+    def self.is_valid?(name)
+      name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
+    end
 
-  def is_valid?
-    self.class.is_valid?(@name)
-  end
+    def is_valid?
+      self.class.is_valid?(@name)
+    end
 
-  def hosted?
-    not @ldap.nil?
-  end
+    def hosted?
+      not @ldap.nil?
+    end
 
-  def managed_by?(user)
-    @ldap.managers.include? user.ldap.dn
-  end
+    def managed_by?(user)
+      @ldap.managers.include? user.ldap.dn
+    end
 
-  def __getobj__
-    @ldap
-  end
+    def __getobj__
+      @ldap
+    end
 
-  def self.find_by_manager(user)
-    list = LdapDnsDomain.find(:all, :attribute => 'manager', :value => user.ldap.dn)
-    list.collect do |l_dom|
-      domain = allocate
-      domain.instance_variable_set("@ldap", l_dom)
+    def self.find_by_manager(user)
+      list = LdapDnsDomain.find(:all, :attribute => 'manager', :value => user.ldap.dn)
+      list.collect do |l_dom|
+        domain = allocate
+        domain.instance_variable_set("@ldap", l_dom)
+      end
+    rescue ActiveLdap::Error
+      raise Error.new(:unrecoverable, "db/ldap", e.message)
     end
   end
-end
 
-class Person < Delegator
-  attr_reader :ldap
+  class Person < Delegator
+    attr_reader :ldap
 
-  def self.find_by_fingerprint(fingerprint)
-    list = LdapPerson.find(:all, :attribute => 'keyFingerPrint', :value => fingerprint)
-    case list.size
-    when 0
-      nil
-    when 1
-      person = allocate
-      person.instance_variable_set("@ldap", list.first)
-      person
-    else
-      logger.warn "Multiple users match in database, so i guess there is a mistake. It is safer to skip..."
-      nil
+    def self.find_by_fingerprint(fingerprint)
+      list = LdapPerson.find(:all, :attribute => 'keyFingerPrint', :value => fingerprint)
+      case list.size
+      when 0
+        nil
+      when 1
+        person = allocate
+        person.instance_variable_set("@ldap", list.first)
+        person
+      else
+        logger.warn "Multiple users match in database, so i guess there is a mistake. It is safer to skip..."
+        nil
+      end
+    rescue ActiveLdap::Error => e
+      raise Error.new(:unrecoverable, "db/ldap", e.message)
     end
-  end
 
-  def __getobj__
-    @ldap
+    def __getobj__
+      @ldap
+    end
   end
 end
diff --git a/lib/cyborghood/objects/ldap.rb b/lib/cyborghood/objects/ldap.rb
index 5a8d07d..3074758 100644
--- a/lib/cyborghood/objects/ldap.rb
+++ b/lib/cyborghood/objects/ldap.rb
@@ -1,16 +1,16 @@
+module CyborgHood
+  class LdapDnsDomain < ActiveLdap::Base
+    ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
 
-class LdapDnsDomain < ActiveLdap::Base
-  ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
-
-  def managers
-    list = self.manager
-    return [] if list.nil?
-    return list.collect{|dn| dn.to_s } if list.is_a? Array
-    return [list.to_s]
+    def managers
+      list = self.manager
+      return [] if list.nil?
+      return list.collect{|dn| dn.to_s } if list.is_a? Array
+      return [list.to_s]
+    end
   end
-end
 
-class LdapPerson < ActiveLdap::Base
-  ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['person', 'extInetOrgPerson']
+  class LdapPerson < ActiveLdap::Base
+    ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['person', 'extInetOrgPerson']
+  end
 end
-
