Class: Embulk::PluginRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/plugin_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category, search_prefix) ⇒ PluginRegistry

Returns a new instance of PluginRegistry.



7
8
9
10
11
12
# File 'lib/embulk/plugin_registry.rb', line 7

def initialize(category, search_prefix)
  @category = category
  @search_prefix = search_prefix
  @loaded_gems = {}
  @map = {}
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



14
15
16
# File 'lib/embulk/plugin_registry.rb', line 14

def category
  @category
end

Instance Method Details

#lookup(type) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/embulk/plugin_registry.rb', line 21

def lookup(type)
  type = type.to_sym
  if value = @map[type]
    return value
  end
  if search(type)
    if value = @map[type]
      return value
    end
    raise PluginLoadError.new "Unknown #{@category} plugin '#{type}'. #{@search_prefix}#{type}.rb is installed but it does not correctly register plugin."
  else
    raise PluginLoadError.new "Unknown #{@category} plugin '#{type}'. #{@search_prefix}#{type}.rb is not installed. Run 'embulk gem search -rd embulk-#{@category}' command to find plugins."
  end
end

#register(type, value) ⇒ Object



16
17
18
19
# File 'lib/embulk/plugin_registry.rb', line 16

def register(type, value)
  type = type.to_sym
  @map[type] = value
end

#require_and_show(path, spec = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/embulk/plugin_registry.rb', line 80

def require_and_show(path, spec=nil)
  require path
  unless spec
    name, spec = Kernel::RUBYGEMS_ACTIVATION_MONITOR.synchronize do  # this lock is added as a workaround of https://github.com/jruby/jruby/issues/3652
      Gem.loaded_specs.find {|name,spec|
        #spec.files.include?(path)
        spec.contains_requirable_file?(path)
      }
    end
  end
  if spec
    unless @loaded_gems[spec.name]
      Embulk.logger.info "Loaded plugin #{spec.name} (#{spec.version})"
      @loaded_gems[spec.name]
    end
  else
    Embulk.logger.info "Loaded plugin #{path} from a load path"
  end
end

#search(type) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/embulk/plugin_registry.rb', line 36

def search(type)
  name = "#{@search_prefix}#{type}"
  begin
    require_and_show name
    return true
  rescue LoadError => e
    # catch LoadError but don't catch ClassNotFoundException
    raise e if e.to_s =~ /java.lang.ClassNotFoundException/
    raise e if $LOAD_PATH.any? {|dir| File.exists? File.join(dir, "#{name}.rb") }
  end

  # search from $LOAD_PATH
  load_path_files = $LOAD_PATH.map do |lp|
    lpath = File.expand_path(File.join(lp, "#{name}.rb"))
    File.exist?(lpath) ? lpath : nil
  end

  paths = load_path_files.compact.sort  # sort to prefer newer version
  paths.each do |path|
    require_and_show path
    return true
  end

  # search gems
  if defined?(::Gem::Specification) && ::Gem::Specification.respond_to?(:find_all)
    specs = Kernel::RUBYGEMS_ACTIVATION_MONITOR.synchronize do  # this lock is added as a workaround of https://github.com/jruby/jruby/issues/3652
      Gem::Specification.find_all do |spec|
        spec.contains_requirable_file? name
      end
    end

    # prefer newer version
    specs = specs.sort_by {|spec| spec.version }
    if spec = specs.last
      spec.require_paths.each do |lib|
        require_and_show "#{spec.full_gem_path}/#{lib}/#{name}", spec
      end
      return true
    end
  end

  return false
end