Skip to content

Commit

Permalink
Allow to search plugin from multiple prefix directory
Browse files Browse the repository at this point in the history
  • Loading branch information
filepang committed Aug 1, 2016
1 parent 26eebdb commit 9cc919c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
14 changes: 7 additions & 7 deletions lib/fluent/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ module Plugin
# ex: storage, buffer chunk, ...

# first class plugins (instantiated by Engine)
INPUT_REGISTRY = Registry.new(:input, 'fluent/plugin/in_')
OUTPUT_REGISTRY = Registry.new(:output, 'fluent/plugin/out_')
FILTER_REGISTRY = Registry.new(:filter, 'fluent/plugin/filter_')
INPUT_REGISTRY = Registry.new(:input, 'fluent/plugin/in_', 'in_')
OUTPUT_REGISTRY = Registry.new(:output, 'fluent/plugin/out_', 'out_')
FILTER_REGISTRY = Registry.new(:filter, 'fluent/plugin/filter_', 'filter_')

# feature plugin: second class plugins (instanciated by Plugins or Helpers)
BUFFER_REGISTRY = Registry.new(:buffer, 'fluent/plugin/buf_')
PARSER_REGISTRY = Registry.new(:parser, 'fluent/plugin/parser_')
FORMATTER_REGISTRY = Registry.new(:formatter, 'fluent/plugin/formatter_')
STORAGE_REGISTRY = Registry.new(:storage, 'fluent/plugin/storage_')
BUFFER_REGISTRY = Registry.new(:buffer, 'fluent/plugin/buf_', 'buf_')
PARSER_REGISTRY = Registry.new(:parser, 'fluent/plugin/parser_', 'parser_')
FORMATTER_REGISTRY = Registry.new(:formatter, 'fluent/plugin/formatter_', 'formatter_')
STORAGE_REGISTRY = Registry.new(:storage, 'fluent/plugin/storage_', 'storage_')

REGISTRIES = [INPUT_REGISTRY, OUTPUT_REGISTRY, FILTER_REGISTRY, BUFFER_REGISTRY, PARSER_REGISTRY, FORMATTER_REGISTRY, STORAGE_REGISTRY]

Expand Down
28 changes: 15 additions & 13 deletions lib/fluent/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ module Fluent
class Registry
DEFAULT_PLUGIN_PATH = File.expand_path('../plugin', __FILE__)

def initialize(kind, search_prefix)
def initialize(kind, *search_prefixes)
@kind = kind
@search_prefix = search_prefix
@search_prefixes = search_prefixes
@map = {}
@paths = [DEFAULT_PLUGIN_PATH]
end
Expand Down Expand Up @@ -54,18 +54,20 @@ def reverse_lookup(value)
end

def search(type)
path = "#{@search_prefix}#{type}"
@search_prefixes.each do |search_prefix|
path = "#{search_prefix}#{type}"

# prefer LOAD_PATH than gems
[@paths, $LOAD_PATH].each do |paths|
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
# prefer LOAD_PATH than gems
[@paths, $LOAD_PATH].each do |paths|
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
end

Expand Down

0 comments on commit 9cc919c

Please sign in to comment.