Skip to content

Commit a5ff87e

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. (cherry picked from commit a8bef77)
1 parent 54d3c6e commit a5ff87e

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
@@ -13,6 +13,7 @@
1313
-export([is_strictly_plugin/1, strictly_plugins/2, strictly_plugins/1]).
1414
-export([plugins_dir/0, plugin_names/1, plugins_expand_dir/0, enabled_plugins_file/0]).
1515
-export([is_enabled/1, is_enabled_on_node/2]).
16+
-export([which_plugin/1]).
1617

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

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

290341
prepare_plugins(Enabled) ->

0 commit comments

Comments
 (0)