Class: Rubeus::Extensions::Java::Sql::DriverManager::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/rubeus/extensions/java/sql/driver_manager.rb,
lib/rubeus/extensions/java/sql/driver_manager.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Loader

Returns a new instance of Loader.



58
59
60
61
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 58

def initialize(name)
  @name = name
  @patterns = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



56
57
58
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 56

def name
  @name
end

#patternsObject (readonly)

Returns the value of attribute patterns.



56
57
58
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 56

def patterns
  @patterns
end

Class Method Details

.entriesObject



28
29
30
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 28

def entries
  @entries ||= []
end

.entry(name, *args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 32

def entry(name, *args, &block)
  result = self.new(name)
  if block_given?
    result.instance_eval(&block)
  else
    pattern, driver, driver_type, options = *args
    result.instance_eval do |db|
      db.pattern(pattern, driver, driver_type || :unknown)
      db.options = options
    end
  end
  entries << result
  result
end

.setup_for(url, setup_options = nil) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 47

def setup_for(url, setup_options = nil)
  entries.each do |entry|
    driver = entry.setup_for(url, setup_options)
    return driver if driver
  end
  raise ArgumentError, "DriverManager catalog not found for #{url}"
end

Instance Method Details

#driver_for(url, driver_type = nil) ⇒ Object



75
76
77
78
79
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 75

def driver_for(url, driver_type = nil)
  patterns = driver_type ? @patterns.select{|pattern| pattern[:driver_type] == driver_type} : @patterns
  pattern = patterns.detect{|pattern| pattern[:pattern] =~ url}
  pattern ? pattern[:driver] : nil
end

#optionsObject



67
68
69
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 67

def options
  @options ||= {}
end

#options=(value) ⇒ Object



71
72
73
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 71

def options=(value)
  @options = value
end

#pattern(pattern, driver, driver_type = :unknown) ⇒ Object



63
64
65
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 63

def pattern(pattern, driver, driver_type = :unknown)
  @patterns << {:pattern => pattern, :driver => driver, :driver_type => driver_type}
end

#setup_for(url, setup_options = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rubeus/extensions/java/sql/driver_manager.rb', line 81

def setup_for(url, setup_options = nil)
  driver_name = driver_for(url)
  return nil unless driver_name
  begin
    return JavaUtilities.get_proxy_class(driver_name)
  rescue NameError => name_error
    requirements = options[:gem_require]
    raise name_error unless requirements
    requirements = [requirements] unless requirements.is_a?(Array)
    load_error_messages = []
    requirements.each do |requirement|
      begin
        require(requirement)
      rescue LoadError => load_error
        msg = "failure to load '#{requirement}' of '#{options[:gem]}'."
        msg << " Try 'jruby -S gem install #{options[:gem]}'" unless /\.jar$/ =~ requirement
        load_error_messages << msg
        next
      end
      begin
        return JavaUtilities.get_proxy_class(driver_name)
      rescue NameError => name_error_again
        msg = "#{driver_name} not found for #{url}"
        msg << ", but loaded '#{requirement}' of '#{options[:gem]}' successfully."
        msg << " It might be a serious problem. Please let us know by email '#{Rubeus::EMAIL_GROUP}'. "
        raise NameError, msg
      end
    end
    raise LoadError, "#{driver_name} not found for #{url} because of " << load_error_messages.join(" AND ")
  end
end