`
andyhu1007
  • 浏览: 193944 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

JRuby LDAP Patch

阅读更多

解决jruby-ldap 跟Active Directory兼容问题的patch。

 

# config/initializers/jruby-ldap_patch.rb
require 'ldap'
module JrubyLdapPatches
  module AddToHashToEntry
    def to_hash
      self
    end
  end

  module AlwaysUseLdapCtxFactory
    def self.included(base)
      base.extend ClassMethods
      base.class_eval do
        class << self
          alias_method_chain :configuration, :fixed_factory
        end
      end
    end

    module ClassMethods
      def configuration_with_fixed_factory(attrs = {})
        configuration_without_fixed_factory(attrs).update(javax.naming.Context::INITIAL_CONTEXT_FACTORY => 'com.sun.jndi.ldap.LdapCtxFactory')
      end
    end
  end

  module MoreInformationForWrappedErrors
    def self.included(base)
      base.extend ClassMethods
      base.class_eval do
        class << self
          alias_method_chain :wrap, :more_information
        end
      end
    end

    module ClassMethods
      def wrap_with_more_information(message, java_exception)
        returning wrap_without_more_information(message, java_exception) do
          show_cause(java_exception.cause) if $DEBUG || $VERBOSE_LDAP_ERRORS
        end
      end

      def show_cause(exception)
        unless exception.nil?
          puts exception.to_s
          show_cause(exception.cause)
        end
      end
    end
  end
end

LDAP::Entry.send :include, JrubyLdapPatches::AddToHashToEntry
LDAP.send :include, JrubyLdapPatches::AlwaysUseLdapCtxFactory
LDAP::Error.send :include, JrubyLdapPatches::MoreInformationForWrappedErrors

module LDAP
  class SSLConn
    # LDAP::SSLConn in jruby-ldap doesn't have the same arglist as ruby-ldap
    # does, and activedirectory tries to create one with three arguments.
    def initialize(host, port, *dontcare)
      super(host, port)
      @use_ssl = true
    end
  end
end

 

从这个patch也可以一窥ruby patch的设计:alias_method_chain和mixin。

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics