Class: Fluent::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/registry.rb

Constant Summary collapse

DEFAULT_PLUGIN_PATH =
File.expand_path('../plugin', __FILE__)
FLUENT_LIB_PATH =
File.dirname(File.dirname(DEFAULT_PLUGIN_PATH))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, search_prefix, dir_search_prefix: nil) ⇒ Registry

Returns a new instance of Registry.



24
25
26
27
28
29
30
# File 'lib/fluent/registry.rb', line 24

def initialize(kind, search_prefix, dir_search_prefix: nil)
  @kind = kind
  @search_prefix = search_prefix
  @dir_search_prefix = dir_search_prefix
  @map = {}
  @paths = []
end

Instance Attribute Details

#dir_search_prefixObject (readonly)

Returns the value of attribute dir_search_prefix.



32
33
34
# File 'lib/fluent/registry.rb', line 32

def dir_search_prefix
  @dir_search_prefix
end

#kindObject (readonly)

Returns the value of attribute kind.



32
33
34
# File 'lib/fluent/registry.rb', line 32

def kind
  @kind
end

#mapObject (readonly)

Returns the value of attribute map.



32
33
34
# File 'lib/fluent/registry.rb', line 32

def map
  @map
end

#pathsObject (readonly)

Returns the value of attribute paths.



32
33
34
# File 'lib/fluent/registry.rb', line 32

def paths
  @paths
end

Instance Method Details

#lookup(type) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fluent/registry.rb', line 39

def lookup(type)
  type = type.to_sym
  if value = @map[type]
    return value
  end
  search(type)
  if value = @map[type]
    return value
  end
  raise ConfigError, "Unknown #{@kind} plugin '#{type}'. Run 'gem search -rd fluent-plugin' to find plugins"  # TODO error class
end

#register(type, value) ⇒ Object



34
35
36
37
# File 'lib/fluent/registry.rb', line 34

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

#reverse_lookup(value) ⇒ Object



51
52
53
54
55
56
# File 'lib/fluent/registry.rb', line 51

def reverse_lookup(value)
  @map.each do |k, v|
    return k if v == value
  end
  nil
end

#search(type) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
112
113
114
# File 'lib/fluent/registry.rb', line 58

def search(type)
  # search from additional plugin directories
  if @dir_search_prefix
    path = "#{@dir_search_prefix}#{type}"
    files = @paths.map { |lp|
      lpath = File.expand_path(File.join(lp, "#{path}.rb"))
      File.exist?(lpath) ? lpath : nil
    }.compact
    unless files.empty?
      # prefer newer version
      require files.sort.last
      return
    end
  end

  path = "#{@search_prefix}#{type}"

  # prefer LOAD_PATH than gems
  files = $LOAD_PATH.map { |lp|
    if lp == FLUENT_LIB_PATH
      nil
    else
      lpath = File.expand_path(File.join(lp, "#{path}.rb"))
      File.exist?(lpath) ? lpath : nil
    end
  }.compact
  unless files.empty?
    # prefer newer version
    require files.sort.last
    return
  end

  # Find from gems and prefer newer version
  specs = Gem::Specification.find_all { |spec|
    if spec.name == 'fluentd'.freeze
      false
    else
      spec.contains_requirable_file? path
    end
  }.sort_by { |spec| spec.version }
  if spec = specs.last
    spec.require_paths.each { |lib|
      file = "#{spec.full_gem_path}/#{lib}/#{path}"
      if File.exist?("#{file}.rb")
        require file
        return
      end
    }
  end

  # Lastly, load built-in plugin
  lpath = File.expand_path(File.join(FLUENT_LIB_PATH, "#{@search_prefix}#{type}.rb"))
  if File.exist?(lpath)
    require lpath
    return
  end
end