r1298@opsdev009 (orig r69998): cpiro | 2007-11-14 22:26:30 -0800
[amiethrift.git] / lib / erl / src / thrift.erl
blobef2c1fa02c3bf63c51e0a6865226da8a1bab636c
1 %%% Copyright (c) 2007- Facebook
2 %%% Distributed under the Thrift Software License
3 %%%
4 %%% See accompanying file LICENSE or visit the Thrift site at:
5 %%% http://developers.facebook.com/thrift/
7 -module(thrift).
9 -export([start/0, stop/0, config/1, config/2, reconfig/1]).
11 -include("thrift.hrl").
13 %%%
14 %%% behavior definition
15 %%%
17 start() ->
18 application:start(thrift).
20 stop() ->
21 application:stop(thrift).
23 %%%
24 %%% configuration
25 %%%
27 config(Item) ->
28 config(?MODULE, Item).
30 config(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})
38 end.
40 reconfig(Config) ->
41 BFName = filename:basename(Config, ".config"),
42 FName = filename:join(filename:dirname(Config),
43 BFName ++ ".config"),
45 case file:consult(FName) of
46 {error, R={_,_,_}} ->
47 {error, file_error, file:format_error(R)};
48 {error, Posix} ->
49 {error, file_error, Posix};
50 {ok, [List]} when is_list(List) ->
51 reconfig1(List)
52 end.
54 reconfig1([]) ->
55 ok;
56 reconfig1([{App, List}|Tl]) ->
57 reconfig2(List, App, 0),
58 reconfig1(Tl).
60 reconfig2([], App, Count) ->
61 ?INFO("application ~p reconfigured: ~p keys updated", [App, Count]),
62 ok;
63 reconfig2([{Par, Val}|Tl], App, Count) ->
64 application:set_env(App, Par, Val),
65 reconfig2(Tl, App, Count+1).