Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | -module(thrift). |
| 8 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 9 | -export([start/0, stop/0, config/1, config/2, reconfig/1]). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 10 | |
| 11 | -include("thrift.hrl"). |
| 12 | |
| 13 | %%% |
| 14 | %%% behavior definition |
| 15 | %%% |
| 16 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 17 | start() -> |
| 18 | application:start(thrift). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 19 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 20 | stop() -> |
| 21 | application:stop(thrift). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 22 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 23 | %%% |
| 24 | %%% configuration |
| 25 | %%% |
| 26 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 27 | config(Item) -> |
| 28 | config(?MODULE, Item). |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 29 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 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}) |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 38 | end. |
| 39 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 40 | reconfig(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 Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 46 | {error, R={_,_,_}} -> |
| 47 | {error, file_error, file:format_error(R)}; |
| 48 | {error, Posix} -> |
| 49 | {error, file_error, Posix}; |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 50 | {ok, [List]} when is_list(List) -> |
| 51 | reconfig1(List) |
| 52 | end. |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 53 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame^] | 54 | reconfig1([]) -> |
| 55 | ok; |
| 56 | reconfig1([{App, List}|Tl]) -> |
| 57 | reconfig2(List, App, 0), |
| 58 | reconfig1(Tl). |
| 59 | |
| 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). |