Class: LoadLibraryHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/load_library_handler.rb

Constant Summary collapse

@@loaded_libraries =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeLoadLibraryHandler

Returns a new instance of LoadLibraryHandler.



7
8
9
# File 'lib/javonet-ruby-sdk/core/handler/load_library_handler.rb', line 7

def initialize
  @required_parameters_count = 1
end

Class Method Details

.get_loaded_librariesObject



46
47
48
# File 'lib/javonet-ruby-sdk/core/handler/load_library_handler.rb', line 46

def self.get_loaded_libraries
  @@loaded_libraries
end

Instance Method Details

#process(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/javonet-ruby-sdk/core/handler/load_library_handler.rb', line 11

def process(command)
  begin
    if command.payload.length < @required_parameters_count
      raise ArgumentError.new "Load library parameters mismatch"
    end

    assembly_name =
      if command.payload.length > @required_parameters_count
        command.payload[1]
      else
        command.payload[0]
      end

    # Normalize to absolute path
    expanded_path = File.expand_path(assembly_name)

    # Load only once per absolute path
    if @@loaded_libraries.include?(expanded_path)
      return 0
    end

    # Check if the library exists:
    raise LoadError.new("Library not found: #{assembly_name}") unless File.exist?(expanded_path)

    # Require by path; Ruby will also avoid re-loading the same feature,
    # but we additionally track it ourselves.
    require expanded_path

    @@loaded_libraries << expanded_path
    return 0
  rescue Exception => e
    return e
  end
end