blob: ef2c1fa02c3bf63c51e0a6865226da8a1bab636c [file] [log] [blame]
Christopher Piro5b3a8f72007-08-01 22:27:37 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Piro68940292007-10-02 00:35:12 +00003%%%
Christopher Piro5b3a8f72007-08-01 22:27:37 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(thrift).
8
Christopher Piro6c46f1a2007-10-23 09:47:15 +00009-export([start/0, stop/0, config/1, config/2, reconfig/1]).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000010
11-include("thrift.hrl").
12
13%%%
14%%% behavior definition
15%%%
16
Christopher Piro68940292007-10-02 00:35:12 +000017start() ->
18 application:start(thrift).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000019
Christopher Piro68940292007-10-02 00:35:12 +000020stop() ->
21 application:stop(thrift).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000022
Christopher Piro68940292007-10-02 00:35:12 +000023%%%
24%%% configuration
25%%%
26
Christopher Piro6c46f1a2007-10-23 09:47:15 +000027config(Item) ->
28 config(?MODULE, Item).
Christopher Piro68940292007-10-02 00:35:12 +000029
Christopher Piro6c46f1a2007-10-23 09:47:15 +000030config(App, Item) ->
31 case application:get_env(App, Item) of
32 {ok, Value} ->
33 Value;
34 undefined ->
35 ?ERROR("configuration for ~p is unavailable", [Item]),
36 unconfigured_item,
37 exit({missing_config, App, Item})
Christopher Piro68940292007-10-02 00:35:12 +000038 end.
39
Christopher Piro6c46f1a2007-10-23 09:47:15 +000040reconfig(Config) ->
41 BFName = filename:basename(Config, ".config"),
42 FName = filename:join(filename:dirname(Config),
43 BFName ++ ".config"),
44
45 case file:consult(FName) of
Christopher Piro68940292007-10-02 00:35:12 +000046 {error, R={_,_,_}} ->
47 {error, file_error, file:format_error(R)};
48 {error, Posix} ->
49 {error, file_error, Posix};
Christopher Piro6c46f1a2007-10-23 09:47:15 +000050 {ok, [List]} when is_list(List) ->
51 reconfig1(List)
52 end.
Christopher Piro68940292007-10-02 00:35:12 +000053
Christopher Piro6c46f1a2007-10-23 09:47:15 +000054reconfig1([]) ->
55 ok;
56reconfig1([{App, List}|Tl]) ->
57 reconfig2(List, App, 0),
58 reconfig1(Tl).
59
60reconfig2([], App, Count) ->
61 ?INFO("application ~p reconfigured: ~p keys updated", [App, Count]),
62 ok;
63reconfig2([{Par, Val}|Tl], App, Count) ->
64 application:set_env(App, Par, Val),
65 reconfig2(Tl, App, Count+1).