Browse Source

new plugin mechanism

Feng Lee 10 years ago
parent
commit
9b71f9f6c0
1 changed files with 12 additions and 27 deletions
  1. 12 27
      src/emqttd_plugins.erl

+ 12 - 27
src/emqttd_plugins.erl

@@ -70,10 +70,10 @@ stop_plugins(Names) ->
 %% @doc List all available plugins
 %% @doc List all available plugins
 -spec(list() -> [mqtt_plugin()]).
 -spec(list() -> [mqtt_plugin()]).
 list() ->
 list() ->
-    case env(plugins_dir) of
-        {ok, PluginsDir} -> 
-            AppFiles = filelib:wildcard("*/ebin/*.app", PluginsDir),
-            Plugins = [plugin(PluginsDir, AppFile) || AppFile <- AppFiles],
+    case env(plugins_etc) of
+        {ok, PluginsEtc} -> 
+            CfgFiles = filelib:wildcard("*.config", PluginsEtc),
+            Plugins = [plugin(PluginsEtc, CfgFile) || CfgFile <- CfgFiles],
             StartedApps = names(started_app),
             StartedApps = names(started_app),
             lists:map(fun(Plugin = #mqtt_plugin{name = Name}) ->
             lists:map(fun(Plugin = #mqtt_plugin{name = Name}) ->
                           case lists:member(Name, StartedApps) of
                           case lists:member(Name, StartedApps) of
@@ -85,21 +85,13 @@ list() ->
             []
             []
     end.
     end.
 
 
-plugin(PluginsDir, AppFile0) ->
-    AppFile = filename:join(PluginsDir, AppFile0),
-    {ok, [{application, Name, Attrs}]} = file:consult(AppFile),
-    CfgFile = filename:join([PluginsDir, Name, "etc/plugin.config"]),
-    AppsEnv1 =
-    case filelib:is_file(CfgFile) of
-        true ->
-            {ok, [AppsEnv]} = file:consult(CfgFile),
-            AppsEnv;
-        false ->
-            []
-    end,
+plugin(PluginsEtc, CfgFile0) ->
+    CfgFile = filename:join(PluginsEtc, CfgFile0),
+    {ok, [[{AppName, AppEnv} | _]]} = file:consult(CfgFile),
+    {ok, Attrs} = application:get_all_key(AppName),
     Ver = proplists:get_value(vsn, Attrs, "0"),
     Ver = proplists:get_value(vsn, Attrs, "0"),
     Descr = proplists:get_value(description, Attrs, ""),
     Descr = proplists:get_value(description, Attrs, ""),
-    #mqtt_plugin{name = Name, version = Ver, config = AppsEnv1, descr = Descr}.
+    #mqtt_plugin{name = AppName, version = Ver, config = AppEnv, descr = Descr}.
 
 
 %% @doc Load a Plugin
 %% @doc Load a Plugin
 -spec(load(atom()) -> ok | {error, any()}).
 -spec(load(atom()) -> ok | {error, any()}).
@@ -126,23 +118,16 @@ load_plugin(#mqtt_plugin{name = Name, config = Config}, Persistent) ->
             {error, Error}
             {error, Error}
     end.
     end.
 
 
-load_app(App, Config) ->
+load_app(App, _Config) ->
     case application:load(App) of
     case application:load(App) of
         ok ->
         ok ->
-            set_config(Config);
+            ok;
         {error, {already_loaded, App}} ->
         {error, {already_loaded, App}} ->
-            set_config(Config);
+            ok;
         {error, Error} ->
         {error, Error} ->
             {error, Error}
             {error, Error}
     end.
     end.
 
 
-%% This trick is awesome:)
-set_config([]) ->
-    ok;
-set_config([{AppName, Envs} | Config]) ->
-    [application:set_env(AppName, Par, Val) || {Par, Val} <- Envs],
-    set_config(Config).
-
 start_app(App, SuccFun) ->
 start_app(App, SuccFun) ->
     case application:ensure_all_started(App) of
     case application:ensure_all_started(App) of
         {ok, Started} ->
         {ok, Started} ->