|
14 | 14 | -export([is_strictly_plugin/1, strictly_plugins/2, strictly_plugins/1]). |
15 | 15 | -export([plugins_dir/0, plugin_names/1, plugins_expand_dir/0, enabled_plugins_file/0]). |
16 | 16 | -export([is_enabled/1, is_enabled_on_node/2]). |
| 17 | +-export([which_plugin/1]). |
17 | 18 |
|
18 | 19 | % Export for testing purpose. |
19 | 20 | -export([is_version_supported/2, validate_plugins/2]). |
@@ -286,6 +287,56 @@ running_plugins() -> |
286 | 287 | ActivePlugins = active(), |
287 | 288 | {ok, [{App, Vsn} || {App, _ , Vsn} <- rabbit_misc:which_applications(), lists:member(App, ActivePlugins)]}. |
288 | 289 |
|
| 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 | + |
289 | 340 | %%---------------------------------------------------------------------------- |
290 | 341 |
|
291 | 342 | prepare_plugins(Enabled) -> |
|
0 commit comments