欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JRuby LDAP Patch

程序员文章站 2024-03-11 15:56:37
...

解决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。

相关标签: jruby Ruby SUN