Module: ActiveLdap::Connection::ClassMethods

Defined in:
lib/active_ldap/connection.rb

Constant Summary collapse

@@active_connections =
{}
@@allow_concurrency =
false

Instance Method Summary collapse

Instance Method Details

#active_connection_nameObject



37
38
39
# File 'lib/active_ldap/connection.rb', line 37

def active_connection_name
  @active_connection_name ||= determine_active_connection_name
end

#allow_concurrency=(threaded) ⇒ Object

:nodoc:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_ldap/connection.rb', line 25

def allow_concurrency=(threaded) #:nodoc:
  logger.debug {"allow_concurrency=#{threaded}"} if logger
  return if @@allow_concurrency == threaded
  clear_all_cached_connections!
  @@allow_concurrency = threaded
  method_prefix = threaded ? "thread_safe" : "single_threaded"
  sing = (class << self; self; end)
  [:active_connections].each do |method|
    sing.send(:alias_method, method, "#{method_prefix}_#{method}")
  end
end

#clear_active_connection_nameObject



55
56
57
58
59
60
61
62
# File 'lib/active_ldap/connection.rb', line 55

def clear_active_connection_name
  @active_connection_name = nil
  ObjectSpace.each_object(Class) do |klass|
    if klass < self and !klass.name.empty?
      klass.instance_variable_set("@active_connection_name", nil)
    end
  end
end

#clear_active_connections!Object



47
48
49
50
51
52
53
# File 'lib/active_ldap/connection.rb', line 47

def clear_active_connections!
  connections = active_connections
  connections.each do |key, connection|
    connection.disconnect!
  end
  connections.clear
end

#connected?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/active_ldap/connection.rb', line 107

def connected?
  active_connections[active_connection_name] ? true : false
end

#connectionObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_ldap/connection.rb', line 64

def connection
  conn = nil
  @active_connection_name ||= nil
  if @active_connection_name
    conn = active_connections[@active_connection_name]
  end
  unless conn
    conn = retrieve_connection
    active_connections[@active_connection_name] = conn
  end
  conn
end

#connection=(adapter) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_ldap/connection.rb', line 77

def connection=(adapter)
  if adapter.is_a?(Adapter::Base)
    active_connections[active_connection_name] = adapter
  elsif adapter.is_a?(Hash)
    config = adapter
    self.connection = instantiate_adapter(config)
  elsif adapter.nil?
    raise ConnectionNotEstablished
  else
    establish_connection(adapter)
  end
end

#establish_connection(config = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/active_ldap/connection.rb', line 140

def establish_connection(config=nil)
  config = ensure_configuration(config)
  remove_connection

  clear_active_connection_name
  key = active_connection_key
  @active_connection_name = key
  define_configuration(key, merge_configuration(config))
end

#instantiate_adapter(config) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_ldap/connection.rb', line 90

def instantiate_adapter(config)
  adapter = (config[:adapter] || "ldap")
  normalized_adapter = adapter.downcase.gsub(/-/, "_")
  adapter_method = "#{normalized_adapter}_connection"
  unless Adapter::Base.respond_to?(adapter_method)
    raise AdapterNotFound.new(adapter)
  end
  if config.has_key?(:ldap_scope)
    logger.warning do
      _(":ldap_scope connection option is deprecated. Use :scope instead.")
    end
    config[:scope] ||= config.delete(:ldap_scope)
  end
  config = remove_connection_related_configuration(config)
  Adapter::Base.send(adapter_method, config)
end

#remove_active_connections!Object



41
42
43
44
45
# File 'lib/active_ldap/connection.rb', line 41

def remove_active_connections!
  active_connections.keys.each do |key|
    remove_connection(key)
  end
end

#remove_connection(klass_or_key = self) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_ldap/connection.rb', line 126

def remove_connection(klass_or_key=self)
  if klass_or_key.is_a?(Module)
    key = active_connection_key(klass_or_key)
  else
    key = klass_or_key
  end
  config = configuration(key)
  conn = active_connections[key]
  remove_configuration_by_configuration(config)
  active_connections.delete_if {|key, value| value == conn}
  conn.disconnect! if conn
  config
end

#retrieve_connectionObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_ldap/connection.rb', line 111

def retrieve_connection
  conn = nil
  name = active_connection_name
  raise ConnectionNotEstablished unless name
  conn = active_connections[name]
  if conn.nil?
    config = configuration(name)
    raise ConnectionNotEstablished unless config
    self.connection = config
    conn = active_connections[name]
  end
  raise ConnectionNotEstablished if conn.nil?
  conn
end

#schemaObject

Return the schema object



151
152
153
# File 'lib/active_ldap/connection.rb', line 151

def schema
  connection.schema
end

#single_threaded_active_connectionsObject Also known as: active_connections



15
16
17
# File 'lib/active_ldap/connection.rb', line 15

def single_threaded_active_connections
  @@active_connections
end

#thread_safe_active_connectionsObject



11
12
13
# File 'lib/active_ldap/connection.rb', line 11

def thread_safe_active_connections
  @@active_connections[Thread.current.object_id] ||= {}
end