[thrift] gut Erlang Thrift configuration; start using get_env

Summary: I patterned some config stuff after iserve, but Erlang's built-in stuff is better

Reviewed By: gopher

Test Plan: works with recent ch server

Revert Plan: ok

Other Notes: default config given in thrift.app


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665307 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/erl/src/thrift.erl b/lib/erl/src/thrift.erl
index 5dc8987..ef2c1fa 100644
--- a/lib/erl/src/thrift.erl
+++ b/lib/erl/src/thrift.erl
@@ -6,7 +6,7 @@
 
 -module(thrift).
 
--export([start/0, stop/0, config/1, config/2]).
+-export([start/0, stop/0, config/1, config/2, reconfig/1]).
 
 -include("thrift.hrl").
 
@@ -24,46 +24,42 @@
 %%% configuration
 %%%
 
-%% go to the global file by default
-config(Path) ->
-    config(Path, {file, ?CONFIG_FILE}).
+config(Item) ->
+    config(?MODULE, Item).
 
-config(Path, Source) ->
-    case config1(Path, Source) of
-	{error, file_error, R} ->
-	    ?ERROR("error opening config ~p: ~p", [Source, R]),
-	    false;
-	{error, bad_item, Tuple} ->
-	    ?ERROR("malformed config item ~p found at ~p in ~p", [Tuple, Path, Source]),
-	    false;
-	{error, not_found} ->
-	    ?ERROR("config item ~p not found in ~p", [Path, Source]),
-	    false;
-	{value, V} ->
-	    {value, V}
+config(App, Item) ->
+    case application:get_env(App, Item) of
+        {ok, Value} ->
+            Value;
+        undefined ->
+            ?ERROR("configuration for ~p is unavailable", [Item]),
+            unconfigured_item,
+            exit({missing_config, App, Item})
     end.
 
-%% go to a file
-config1(Path, {file, File}) ->
-    case file:consult(File) of
+reconfig(Config) ->
+    BFName = filename:basename(Config, ".config"),
+    FName  = filename:join(filename:dirname(Config),
+                           BFName ++ ".config"),
+
+    case file:consult(FName) of
 	{error, R={_,_,_}} ->
 	    {error, file_error, file:format_error(R)};
 	{error, Posix} ->
 	    {error, file_error, Posix};
-	{ok, List} when is_list(List) ->
-	    config1(Path, List)
-    end;
+	{ok, [List]} when is_list(List) ->
+            reconfig1(List)
+    end.
 
-%% go through a list from a file or a sublist
-config1([P|Ps], List) when is_list(List) ->
-    case lists:keysearch(P, 1, List) of
-	{value, Tuple} when size(Tuple) == 2 ->
-	    List1 = element(2, Tuple), %% either another list or, if Ps is [], the item itself
-	    config1(Ps, List1);
-	{value, Tuple} ->
-	    {error, bad_item, Tuple};
-	false ->
-	    {error, not_found}
-    end;
-config1([], Item)     -> {value, Item};
-config1(Item, Source) -> config1([Item], Source).
+reconfig1([]) ->
+    ok;
+reconfig1([{App, List}|Tl]) ->
+    reconfig2(List, App, 0),
+    reconfig1(Tl).
+
+reconfig2([], App, Count) ->
+    ?INFO("application ~p reconfigured: ~p keys updated", [App, Count]),
+    ok;
+reconfig2([{Par, Val}|Tl], App, Count) ->
+    application:set_env(App, Par, Val),
+    reconfig2(Tl, App, Count+1).