Skip to content

Commit

Permalink
introduce generic code for backend modules
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Jan 20, 2015
1 parent 9023378 commit e9d6a6d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 119 deletions.
21 changes: 21 additions & 0 deletions apps/ejabberd/src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

-export([start/0,
start_module/3,
start_backend_module/2,
stop_module/2,
stop_module_keep_config/2,
reload_module/3,
Expand Down Expand Up @@ -104,6 +105,26 @@ start_module(Host, Module, Opts0) ->
end
end.

-spec start_backend_module(module(), list()) -> no_return().
start_backend_module(Module, Opts) ->
ModuleStr = atom_to_list(Module),
BackendModuleStr = ModuleStr ++ "_backend",
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(backend_code(ModuleStr, Backend)),
code:load_binary(Mod, BackendModuleStr ++ ".erl", Code).

-spec backend_code(string(), atom()) -> string().
backend_code(Module, Backend) when is_atom(Backend) ->
BackendModule = Module ++ "_backend",
lists:flatten(
["-module(",BackendModule,").
-export([backend/0]).
-spec backend() -> atom().
backend() ->",
Module,"_",
atom_to_list(Backend),
".\n"]).

-spec is_app_running(_) -> boolean().
is_app_running(AppName) ->
Expand Down
23 changes: 1 addition & 22 deletions apps/ejabberd/src/mod_last.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),

start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
?BACKEND:init(Host, Opts),

gen_iq_handler:add_iq_handler(ejabberd_local, Host,
Expand All @@ -109,27 +109,6 @@ stop(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
?NS_LAST).

%% ------------------------------------------------------------------
%% Dynamic modules

start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_last_backend(Backend)),
code:load_binary(Mod, "mod_last_backend.erl", Code).

-spec mod_last_backend(atom()) -> string().
mod_last_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_last_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_last_",
atom_to_list(Backend),
".\n"]).


%%%
%%% Uptime of ejabberd node
%%%
Expand Down
21 changes: 1 addition & 20 deletions apps/ejabberd/src/mod_offline.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
start(Host, Opts) ->
AccessMaxOfflineMsgs = gen_mod:get_opt(access_max_user_messages, Opts,
max_user_offline_messages),
start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
?BACKEND:init(Host, Opts),
start_worker(Host, AccessMaxOfflineMsgs),
ejabberd_hooks:add(offline_message_hook, Host,
Expand Down Expand Up @@ -110,25 +110,6 @@ stop(Host) ->
ok.


%% Dynamic modules
%% ------------------------------------------------------------------

start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_offline_backend(Backend)),
code:load_binary(Mod, "mod_offline_backend.erl", Code).

-spec mod_offline_backend(atom()) -> string().
mod_offline_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_offline_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_offline_",
atom_to_list(Backend),
".\n"]).

%% Server side functions
%% ------------------------------------------------------------------

Expand Down
21 changes: 1 addition & 20 deletions apps/ejabberd/src/mod_privacy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
%% ------------------------------------------------------------------

start(Host, Opts) ->
start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
?BACKEND:init(Host, Opts),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
ejabberd_hooks:add(privacy_iq_get, Host,
Expand Down Expand Up @@ -151,25 +151,6 @@ stop(Host) ->
?MODULE, remove_user, 50),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY).

%% Dynamic modules
%% ------------------------------------------------------------------

start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_privacy_backend(Backend)),
code:load_binary(Mod, "mod_privacy_backend.erl", Code).

-spec mod_privacy_backend(atom()) -> string().
mod_privacy_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_privacy_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_privacy_",
atom_to_list(Backend),
".\n"]).

%% Handlers
%% ------------------------------------------------------------------

Expand Down
21 changes: 1 addition & 20 deletions apps/ejabberd/src/mod_private.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
%% gen_mod callbacks

start(Host, Opts) ->
start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
?BACKEND:init(Host, Opts),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
ejabberd_hooks:add(remove_user, Host, ?MODULE, remove_user, 50),
Expand All @@ -84,25 +84,6 @@ stop(Host) ->
ejabberd_hooks:delete(anonymous_purge_hook, Host, ?MODULE, remove_user, 50),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVATE).

%% ------------------------------------------------------------------
%% Dynamic modules

start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_private_backend(Backend)),
code:load_binary(Mod, "mod_private_backend.erl", Code).

-spec mod_private_backend(atom()) -> string().
mod_private_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_private_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_private_",
atom_to_list(Backend),
".\n"]).

%% ------------------------------------------------------------------
%% Handlers
Expand Down
19 changes: 1 addition & 18 deletions apps/ejabberd/src/mod_roster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),

start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
?BACKEND:init(Host, Opts),

ejabberd_hooks:add(roster_get, Host,
Expand Down Expand Up @@ -850,20 +850,3 @@ get_jid_info(_, User, Server, JID) ->
end
end
end.

start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_roster_backend(Backend)),
code:load_binary(Mod, "mod_last_backend.erl", Code).

-spec mod_roster_backend(atom()) -> string().
mod_roster_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_roster_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_roster_",
atom_to_list(Backend),
".\n"]).
20 changes: 1 addition & 19 deletions apps/ejabberd/src/mod_vcard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
%% gen_mod callbacks
%%--------------------------------------------------------------------
start(VHost, Opts) ->
start_backend_module(Opts),
gen_mod:start_backend_module(?MODULE, Opts),
Proc = gen_mod:get_module_proc(VHost,?PROCNAME),
ChildSpec = {Proc, {?MODULE, start_link, [VHost,Opts]},
transient, 1000, worker, [?MODULE]},
Expand Down Expand Up @@ -281,24 +281,6 @@ config_change(Acc, Host, ldap, _NewConfig) ->
Acc;
config_change(Acc, _, _, _) ->
Acc.
%% ------------------------------------------------------------------
%% Dynamic modules
%% ------------------------------------------------------------------
start_backend_module(Opts) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
{Mod, Code} = dynamic_compile:from_string(mod_vcard_backend(Backend)),
code:load_binary(Mod, "mod_vcard_backend.erl", Code).

-spec mod_vcard_backend(atom()) -> string().
mod_vcard_backend(Backend) when is_atom(Backend) ->
lists:flatten(
["-module(mod_vcard_backend).
-export([backend/0]).
-spec backend() -> atom().
backend() ->
mod_vcard_",
atom_to_list(Backend),
".\n"]).

%% ------------------------------------------------------------------
%% Internal
Expand Down

0 comments on commit e9d6a6d

Please sign in to comment.