Skip to content

Commit a8bef77

Browse files
committed
rabbit_plugins: Add which_plugin/1 to query which plugin provides a module
[Why] This will be used in a later commit to find the auth backend plugin that provides a configured auth backend module. [How] We go through the list of available plugins, regardless if they are enabled or not, then look up the given module in the list of modules associated with each plugin's application.
1 parent 6d3d297 commit a8bef77

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

deps/rabbit/src/rabbit_plugins.erl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
-export([is_strictly_plugin/1, strictly_plugins/2, strictly_plugins/1]).
1515
-export([plugins_dir/0, plugin_names/1, plugins_expand_dir/0, enabled_plugins_file/0]).
1616
-export([is_enabled/1, is_enabled_on_node/2]).
17+
-export([which_plugin/1]).
1718

1819
% Export for testing purpose.
1920
-export([is_version_supported/2, validate_plugins/2]).
@@ -286,6 +287,56 @@ running_plugins() ->
286287
ActivePlugins = active(),
287288
{ok, [{App, Vsn} || {App, _ , Vsn} <- rabbit_misc:which_applications(), lists:member(App, ActivePlugins)]}.
288289

290+
-spec which_plugin(Module) -> Ret when
291+
Module :: module(),
292+
Ret :: {ok, PluginName} | {error, Reason},
293+
PluginName :: atom(),
294+
Reason :: no_provider.
295+
%% @doc Returns the name of the plugin that provides the given module.
296+
%%
297+
%% If no plugin provides the module, `{error, no_provider}' is returned.
298+
%%
299+
%% The returned plugin might not be enabled, thus using the given module might
300+
%% not work until the plugin is enabled.
301+
%%
302+
%% @returns An `{ok, PluginName}' tuple with the name of the plugin providing
303+
%% the module, or `{error, no_provider}'.
304+
305+
which_plugin(Module) ->
306+
Plugins = list(),
307+
which_plugin(Plugins, Module).
308+
309+
which_plugin([#plugin{name = Name} | Rest], Module) ->
310+
%% Get the list of modules belonging to this plugin.
311+
ModulesKey = case application:get_key(Name, modules) of
312+
{ok, _} = Ret ->
313+
Ret;
314+
undefined ->
315+
%% The plugin application might not be loaded. Load
316+
%% it temporarily and try again.
317+
case application:load(Name) of
318+
ok ->
319+
Ret = application:get_key(Name, modules),
320+
_ = application:unload(Name),
321+
Ret;
322+
{error, _Reason} ->
323+
undefined
324+
end
325+
end,
326+
case ModulesKey of
327+
{ok, Modules} ->
328+
case lists:member(Module, Modules) of
329+
true ->
330+
{ok, Name};
331+
false ->
332+
which_plugin(Rest, Module)
333+
end;
334+
undefined ->
335+
which_plugin(Rest, Module)
336+
end;
337+
which_plugin([], _Module) ->
338+
{error, no_provider}.
339+
289340
%%----------------------------------------------------------------------------
290341

291342
prepare_plugins(Enabled) ->

0 commit comments

Comments
 (0)