PageRenderTime 28ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/src/support/z_module_dummy.erl

https://code.google.com/p/zotonic/
Erlang | 122 lines | 34 code | 26 blank | 62 comment | 0 complexity | b8b695ce389061ea1e4b9289aac4bca2 MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2010 Marc Worrell
  3%% Date: 2010-07-05
  4%% @doc Dummy gen_server for modules without any gen_server code.
  5%% We use this dummy gen_server so that we still can use a simple otp supervisor to oversee the 
  6%% running modules.
  7
  8%% Copyright 2010 Marc Worrell
  9%%
 10%% Licensed under the Apache License, Version 2.0 (the "License");
 11%% you may not use this file except in compliance with the License.
 12%% You may obtain a copy of the License at
 13%% 
 14%%     http://www.apache.org/licenses/LICENSE-2.0
 15%% 
 16%% Unless required by applicable law or agreed to in writing, software
 17%% distributed under the License is distributed on an "AS IS" BASIS,
 18%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 19%% See the License for the specific language governing permissions and
 20%% limitations under the License.
 21
 22-module(z_module_dummy).
 23-author("Marc Worrell <marc@worrell.nl>").
 24-behaviour(gen_server).
 25
 26%% gen_server exports
 27-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 28-export([start_link/1]).
 29
 30%% interface functions
 31-export([
 32]).
 33
 34-record(state, {context}).
 35
 36%%====================================================================
 37%% API
 38%%====================================================================
 39%% @spec start_link(Args) -> {ok,Pid} | ignore | {error,Error}
 40%% @doc Starts the server
 41start_link(Args) when is_list(Args) ->
 42    {ok, Pid} = gen_server:start_link(?MODULE, Args, []),
 43    gen_server:cast(Pid, {init, proplists:get_value(module, Args)}),
 44    {ok, Pid}.
 45
 46
 47%%====================================================================
 48%% gen_server callbacks
 49%%====================================================================
 50
 51%% @spec init(Args) -> {ok, State} |
 52%%                     {ok, State, Timeout} |
 53%%                     ignore               |
 54%%                     {stop, Reason}
 55%% @doc Initiates the server.
 56init(Args) ->
 57    process_flag(trap_exit, true),
 58    {context, Context} = proplists:lookup(context, Args),
 59    {ok, #state{context=z_context:new(Context)}}.
 60
 61%% @spec handle_call(Request, From, State) -> {reply, Reply, State} |
 62%%                                      {reply, Reply, State, Timeout} |
 63%%                                      {noreply, State} |
 64%%                                      {noreply, State, Timeout} |
 65%%                                      {stop, Reason, Reply, State} |
 66%%                                      {stop, Reason, State}
 67%% @doc Trap unknown calls
 68handle_call(Message, _From, State) ->
 69    {stop, {unknown_call, Message}, State}.
 70
 71
 72%% @spec handle_cast(Msg, State) -> {noreply, State} |
 73%%                                  {noreply, State, Timeout} |
 74%%                                  {stop, Reason, State}
 75%% @doc Handle the next step in the module initialization.
 76handle_cast({init, Module}, State) ->
 77    dummy_module_init(Module, State#state.context),
 78    {noreply, State};
 79    
 80%% @doc Trap unknown casts
 81handle_cast(Message, State) ->
 82    {stop, {unknown_cast, Message}, State}.
 83
 84
 85
 86%% @spec handle_info(Info, State) -> {noreply, State} |
 87%%                                       {noreply, State, Timeout} |
 88%%                                       {stop, Reason, State}
 89%% @doc Handling all non call/cast messages
 90handle_info(_Info, State) ->
 91    {noreply, State}.
 92
 93%% @spec terminate(Reason, State) -> void()
 94%% @doc This function is called by a gen_server when it is about to
 95%% terminate. It should be the opposite of Module:init/1 and do any necessary
 96%% cleaning up. When it returns, the gen_server terminates with Reason.
 97%% The return value is ignored.
 98terminate(_Reason, _State) ->
 99    ok.
100
101%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
102%% @doc Convert process state when code is changed
103
104code_change(_OldVsn, State, _Extra) ->
105    {ok, State}.
106
107
108%%====================================================================
109%% support functions
110%%====================================================================
111
112
113%% @doc When a module doesn't implement a gen_server then check if it exports an init/1 function,
114%% if so then call that function with a fresh sudo context.
115dummy_module_init(Module, Context) ->
116    case lists:member({init,1}, erlang:get_module_info(Module, exports)) of
117        true -> Module:init(z_acl:sudo(Context));
118        false -> nop
119    end.
120    
121
122