Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config connection, socket classes #135

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ def self.result2string(code) #:nodoc:
attr_accessor :port
attr_accessor :base

class << self
# Public: Returns the connection Class used to manage the network
# connection and perform network operations.
def connection_class
@connection_class ||= Net::LDAP::Connection
end
attr_writer :connection_class
end

# Instantiate an object of type Net::LDAP to perform directory operations.
# This constructor takes a Hash containing arguments, all of which are
# either optional or may be specified later with other methods as
Expand Down Expand Up @@ -580,12 +589,7 @@ def open

instrument "open.net_ldap" do |payload|
begin
@open_connection =
Net::LDAP::Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
@open_connection = new_connection
payload[:connection] = @open_connection
payload[:bind] = @open_connection.bind(@auth)
yield self
Expand All @@ -596,6 +600,16 @@ def open
end
end

# Internal: Returns a new instance of the configured connection class.
def new_connection
self.class.connection_class.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
end
private :new_connection

# Searches the LDAP directory for directory entries. Takes a hash argument
# with parameters. Supported parameters include:
# * :base (a string specifying the tree-base for the search);
Expand Down Expand Up @@ -661,11 +675,7 @@ def search(args = {})
}
else
begin
conn = Net::LDAP::Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
if (@result = conn.bind(args[:auth] || @auth)).result_code == 0
@result = conn.search(args) { |entry|
result_set << entry if result_set
Expand Down Expand Up @@ -749,11 +759,7 @@ def bind(auth = @auth)
payload[:bind] = @result = @open_connection.bind(auth)
else
begin
conn = Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
payload[:connection] = conn
payload[:bind] = @result = conn.bind(auth)
ensure
Expand Down Expand Up @@ -856,11 +862,7 @@ def add(args)
else
@result = 0
begin
conn = Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
if (@result = conn.bind(args[:auth] || @auth)).result_code == 0
@result = conn.add(args)
end
Expand Down Expand Up @@ -960,11 +962,7 @@ def modify(args)
else
@result = 0
begin
conn = Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
if (@result = conn.bind(args[:auth] || @auth)).result_code == 0
@result = conn.modify(args)
end
Expand Down Expand Up @@ -1037,11 +1035,7 @@ def rename(args)
else
@result = 0
begin
conn = Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
if (@result = conn.bind(args[:auth] || @auth)).result_code == 0
@result = conn.rename(args)
end
Expand Down Expand Up @@ -1070,11 +1064,7 @@ def delete(args)
else
@result = 0
begin
conn = Connection.new \
:host => @host,
:port => @port,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
conn = new_connection
if (@result = conn.bind(args[:auth] || @auth)).result_code == 0
@result = conn.delete(args)
end
Expand Down
11 changes: 10 additions & 1 deletion lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ class Net::LDAP::Connection #:nodoc:
LdapVersion = 3
MaxSaslChallenges = 10

class << self
# Public: Returns the socket Class used to manage the network
# connection and perform network operations.
def socket_class
@socket_class ||= TCPSocket
end
attr_writer :socket_class
end

def initialize(server)
@instrumentation_service = server[:instrumentation_service]

begin
@conn = TCPSocket.new(server[:host], server[:port])
@conn = self.class.socket_class.new(server[:host], server[:port])
rescue SocketError
raise Net::LDAP::LdapError, "No such address or other socket error."
rescue Errno::ECONNREFUSED
Expand Down