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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值