Module MCollective::PluginManager
In: lib/mcollective/pluginmanager.rb

A simple plugin manager, it stores one plugin each of a specific type the idea is that we can only have one security provider, one connector etc.

Methods

<<   []   delete   grep   include?   loadclass   pluginlist  

Public Class methods

Adds a plugin to the list of plugins, we expect a hash like:

   {:type => "base",
    :class => foo.new}

or like:

   {:type => "base",
    :class => "Foo::Bar"}

In the event that we already have a class with the given type an exception will be raised.

If the :class passed is a String then we will delay instantiation till the first time someone asks for the plugin, this is because most likely the registration gets done by inherited() hooks, at which point the plugin class is not final.

If we were to do a .new here the Class initialize method would get called and not the plugins, we there for only initialize the classes when they get requested via []

[Source]

    # File lib/mcollective/pluginmanager.rb, line 25
25:         def self.<<(plugin)
26:             type = plugin[:type]
27:             klass = plugin[:class]
28:     
29:             raise("Plugin #{type} already loaded") if @plugins.include?(type)
30:     
31: 
32:             # If we get a string then store 'nil' as the instance, signalling that we'll
33:             # create the class later on demand.
34:             if klass.is_a?(String)
35:                 @plugins[type] = {:loadtime => Time.now, :class => klass, :instance => nil}
36:                 Log.instance.debug("Registering plugin #{type} with class #{klass}")
37:             else
38:                 @plugins[type] = {:loadtime => Time.now, :class => klass.class, :instance => klass}
39:                 Log.instance.debug("Registering plugin #{type} with class #{klass.class}")
40:             end
41:         end

Gets a plugin by type

[Source]

    # File lib/mcollective/pluginmanager.rb, line 59
59:         def self.[](plugin)
60:             raise("No plugin #{plugin} defined") unless @plugins.include?(plugin)
61: 
62:             klass = @plugins[plugin][:class]
63: 
64:             # Create an instance of the class if one hasn't been done before
65:             if @plugins[plugin][:instance] == nil
66:                 begin
67:                     @plugins[plugin][:instance] = eval("#{klass}.new")
68:                 rescue Exception => e
69:                     raise("Could not create instance of plugin #{plugin}: #{e}")
70:                 end
71:             end
72: 
73:             Log.instance.debug("Returning plugin #{plugin} with class #{klass}")
74: 
75:             @plugins[plugin][:instance]
76:         end

Removes a plugim the list

[Source]

    # File lib/mcollective/pluginmanager.rb, line 44
44:         def self.delete(plugin)
45:             @plugins.delete(plugin) if @plugins.include?(plugin)
46:         end

Grep‘s over the plugin list and returns the list found

[Source]

    # File lib/mcollective/pluginmanager.rb, line 91
91:         def self.grep(regex)
92:             @plugins.grep regex
93:         end

Finds out if we have a plugin with the given name

[Source]

    # File lib/mcollective/pluginmanager.rb, line 49
49:         def self.include?(plugin)
50:             @plugins.include?(plugin)
51:         end

Loads a class from file by doing some simple search/replace on class names and then doing a require.

[Source]

    # File lib/mcollective/pluginmanager.rb, line 80
80:         def self.loadclass(klass)
81:             fname = klass.gsub("::", "/").downcase + ".rb"
82: 
83:             Log.instance.debug("Loading #{klass} from #{fname}")
84:     
85:             load fname
86:         rescue Exception => e
87:             Log.instance.error("Failed to load #{klass}: #{e}")
88:         end

Provides a list of plugins we know about

[Source]

    # File lib/mcollective/pluginmanager.rb, line 54
54:         def self.pluginlist
55:             @plugins.keys
56:         end

[Validate]