Class: LogStash::Plugins::Registry

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/plugins/registry.rb

Defined Under Namespace

Classes: GemRegistry, PluginRawContext, PluginSpecification, UniversalPluginSpecification, UnknownPlugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/logstash/plugins/registry.rb', line 100

def initialize
  @mutex = Mutex.new
  # We need a threadsafe class here because we may perform
  # get/set operations concurrently despite the fact we don't use
  # the special atomic methods. That may not be apparent from this file,
  # but it is the case that we can call lookups from multiple threads,
  # when multiple pipelines are in play, and that a lookup may modify the registry.
  @registry = java.util.concurrent.ConcurrentHashMap.new
  @java_plugins = java.util.concurrent.ConcurrentHashMap.new
  @hooks = HooksRegistry.new
end

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



98
99
100
# File 'lib/logstash/plugins/registry.rb', line 98

def hooks
  @hooks
end

Instance Method Details

#add(type, name, klass) ⇒ Object



229
230
231
232
# File 'lib/logstash/plugins/registry.rb', line 229

def add(type, name, klass)
  logger.debug("Adding plugin to the registry", :name => name, :type => type, :class => klass)
  add_plugin(type, name, klass)
end

#execute_universal_pluginsObject



118
119
120
121
122
# File 'lib/logstash/plugins/registry.rb', line 118

def execute_universal_plugins
  @registry.values
    .select { |specification| specification.is_a?(UniversalPluginSpecification) }
    .each { |specification| specification.register(hooks, LogStash::SETTINGS) }
end

#exists?(type, name) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/logstash/plugins/registry.rb', line 242

def exists?(type, name)
  @registry.include?(key_for(type, name))
end

#get(type, plugin_name) ⇒ Object



238
239
240
# File 'lib/logstash/plugins/registry.rb', line 238

def get(type, plugin_name)
  @registry[key_for(type, plugin_name)]
end

#lazy_add(type, name, klass) ⇒ Object



224
225
226
227
# File 'lib/logstash/plugins/registry.rb', line 224

def lazy_add(type, name, klass)
  logger.debug("On demand adding plugin to the registry", :name => name, :type => type, :class => klass)
  add_plugin(type, name, klass)
end

#legacy_lookup(type, plugin_name) ⇒ Object

The legacy_lookup method uses the 1.5->5.0 file structure to find and match a plugin and will do a lookup on the namespace of the required class to find a matching plugin with the appropriate type.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/logstash/plugins/registry.rb', line 182

def legacy_lookup(type, plugin_name)
  begin
    path = "logstash/#{type}s/#{plugin_name}"

    klass = begin
      namespace_lookup(type, plugin_name)
    rescue UnknownPlugin => e
      # Plugin not registered. Try to load it.
      begin
        require path
        namespace_lookup(type, plugin_name)
      rescue LoadError => e
        logger.error("Tried to load a plugin's code, but failed.", :exception => e, :path => path, :type => type, :name => plugin_name)
        raise
      end
    end

    plugin = lazy_add(type, plugin_name, klass)
  rescue => e
    logger.error("Problems loading a plugin with",
                :type => type,
                :name => plugin_name,
                :path => path,
                :error_message => e.message,
                :error_class => e.class,
                :error_backtrace => e.backtrace)

    raise LoadError, "Problems loading the requested plugin named #{plugin_name} of type #{type}. Error: #{e.class} #{e.message}"
  end

  plugin
end

#load_available_pluginsObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/logstash/plugins/registry.rb', line 133

def load_available_plugins
  require "logstash/plugins/builtin"

  GemRegistry.logstash_plugins.each do |plugin_context|
    if plugin_context.spec..key?('java_plugin')
      jar_files = plugin_context.spec.files.select {|f| f =~ /.*\.jar/}
      expected_jar_name = plugin_context.spec.name + "-" + plugin_context.spec.version.to_s + ".jar"
      if (jar_files.length != 1 || !jar_files[0].end_with?(expected_jar_name))
        raise LoadError, "Java plugin '#{plugin_context.spec.name}' does not contain a single jar file with the plugin's name and version"
      end
      @java_plugins[plugin_context.spec.name] = [plugin_context.spec.loaded_from, jar_files[0]]
    end

    # When a plugin has a HOOK_FILE defined, its the responsibility of the plugin
    # to register itself to the registry of available plugins.
    #
    # Legacy plugin will lazy register themselves
    if plugin_context.has_hooks?
      begin
        logger.debug("Executing hooks", :name => plugin_context.name, :type => plugin_context.type, :hooks_file => plugin_context.hooks_file)
        plugin_context.execute_hooks!
      rescue => e
        logger.error("error occured when loading plugins hooks file", :name => plugin_context.name, :type => plugin_context.type, :exception => e.message, :stacktrace => e.backtrace )
      end
    end
  end
end

#load_xpackObject



128
129
130
131
# File 'lib/logstash/plugins/registry.rb', line 128

def load_xpack
  logger.info("Loading x-pack")
  require("x-pack/logstash_registry")
end

#lookup(type, plugin_name, &block) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/logstash/plugins/registry.rb', line 161

def lookup(type, plugin_name, &block)
  @mutex.synchronize do
    plugin_spec = get(type, plugin_name)
    # Assume that we have a legacy plugin
    if plugin_spec.nil?
      plugin_spec = legacy_lookup(type, plugin_name)
    end

    raise LoadError, "No plugin found with name '#{plugin_name}'" unless plugin_spec

    if block_given? # if provided pass a block to do validation
      raise LoadError, "Block validation fails for plugin named #{plugin_name} of type #{type}," unless block.call(plugin_spec.klass, plugin_name)
    end

    return plugin_spec.klass
  end
end

#lookup_pipeline_plugin(type, name) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/logstash/plugins/registry.rb', line 215

def lookup_pipeline_plugin(type, name)
  LogStash::PLUGIN_REGISTRY.lookup(type, name) do |plugin_klass, plugin_name|
    is_a_plugin?(plugin_klass, plugin_name)
  end
rescue LoadError, NameError => e
  logger.debug("Problems loading the plugin with", :type => type, :name => name)
  raise(LogStash::PluginLoadingError, I18n.t("logstash.pipeline.plugin-loading-error", :type => type, :name => name, :error => e.to_s))
end

#plugins_with_type(type) ⇒ Object



124
125
126
# File 'lib/logstash/plugins/registry.rb', line 124

def plugins_with_type(type)
  @registry.values.select { |specification| specification.type.to_sym == type.to_sym }.collect(&:klass)
end

#remove(type, plugin_name) ⇒ Object



234
235
236
# File 'lib/logstash/plugins/registry.rb', line 234

def remove(type, plugin_name)
  @registry.delete(key_for(type, plugin_name))
end

#setup!Object



112
113
114
115
116
# File 'lib/logstash/plugins/registry.rb', line 112

def setup!
  load_xpack unless LogStash::OSS
  load_available_plugins
  execute_universal_plugins
end

#sizeObject



246
247
248
# File 'lib/logstash/plugins/registry.rb', line 246

def size
  @registry.size
end