1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is VMware, Inc.
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
%%
-module(rabbit_runtime_parameters).
-include("rabbit.hrl").
-export([parse/1, set/3, clear/2, list/0, list/1, list_formatted/0, lookup/2,
value/2, value/3, info_keys/0]).
-import(rabbit_misc, [pget/2, pset/3]).
-define(TABLE, rabbit_runtime_parameters).
%%---------------------------------------------------------------------------
set(AppName, Key, Term) ->
case set0(AppName, Key, Term) of
ok -> ok;
{errors, L} -> {error_string, rabbit_misc:format_many(
[{"Validation failed~n", []} | L])}
end.
set0(AppName, Key, Term) ->
case lookup_app(AppName) of
{ok, Module} -> case flatten_errors(validate(Term)) of
ok -> case flatten_errors(
Module:validate(AppName, Key, Term)) of
ok -> update(AppName, Key, Term),
Module:notify(AppName, Key, Term),
ok;
E -> E
end;
E -> E
end;
E -> E
end.
update(AppName, Key, Term) ->
ok = rabbit_misc:execute_mnesia_transaction(
fun () ->
ok = mnesia:write(?TABLE, c(AppName, Key, Term), write)
end).
clear(AppName, Key) ->
case lookup_app(AppName) of
{ok, Module} ->
ok = rabbit_misc:execute_mnesia_transaction(
fun () ->
ok = mnesia:delete(?TABLE, {AppName, Key}, write)
end),
Module:notify_clear(AppName, Key),
ok;
E ->
E
end.
list() ->
[p(P) || P <- rabbit_misc:dirty_read_all(?TABLE)].
list(Name) ->
[p(P) || P <- mnesia:dirty_match_object(
?TABLE, #runtime_parameters{key = {Name, '_'}, _ = '_'})].
list_formatted() ->
[pset(value, format(pget(value, P)), P) || P <- list()].
lookup(AppName, Key) ->
case lookup0(AppName, Key, rabbit_misc:const(not_found)) of
not_found -> not_found;
Params -> p(Params)
end.
value(AppName, Key) ->
case lookup0(AppName, Key, rabbit_misc:const(not_found)) of
not_found -> not_found;
Params -> Params#runtime_parameters.value
end.
value(AppName, Key, Default) ->
Params = lookup0(AppName, Key,
fun () -> lookup_missing(AppName, Key, Default) end),
Params#runtime_parameters.value.
lookup0(AppName, Key, DefaultFun) ->
case mnesia:dirty_read(?TABLE, {AppName, Key}) of
[] -> DefaultFun();
[R] -> R
end.
lookup_missing(AppName, Key, Default) ->
rabbit_misc:execute_mnesia_transaction(
fun () ->
case mnesia:read(?TABLE, {AppName, Key}) of
[] -> mnesia:write(?TABLE, c(AppName, Key, Default), write),
Default;
[R] -> R
end
end).
c(AppName, Key, Default) -> #runtime_parameters{key = {AppName, Key},
value = Default}.
p(#runtime_parameters{key = {AppName, Key}, value = Value}) ->
[{app_name, AppName},
{key, Key},
{value, Value}].
info_keys() -> [app_name, key, value].
%%---------------------------------------------------------------------------
lookup_app(App) ->
case rabbit_registry:lookup_module(
runtime_parameter, list_to_atom(binary_to_list(App))) of
{error, not_found} -> {errors, [{"application ~s not found", [App]}]};
{ok, Module} -> {ok, Module}
end.
parse(Src0) ->
Src = case lists:reverse(Src0) of
[$. |_] -> Src0;
_ -> Src0 ++ "."
end,
case erl_scan:string(Src) of
{ok, Scanned, _} ->
case erl_parse:parse_term(Scanned) of
{ok, Parsed} ->
Parsed;
{error, E} ->
exit({could_not_parse_value, format_parse_error(E)})
end;
{error, E, _} ->
exit({could_not_scan_value, format_parse_error(E)})
end.
format_parse_error({_Line, Mod, Err}) ->
lists:flatten(Mod:format_error(Err)).
format(Term) ->
list_to_binary(rabbit_misc:format("~p", [Term])).
%%---------------------------------------------------------------------------
%% We will want to be able to biject these to JSON. So we have some
%% generic restrictions on what we consider acceptable.
validate(Proplist = [T | _]) when is_tuple(T) -> validate_proplist(Proplist);
validate(L) when is_list(L) -> validate_list(L);
validate(T) when is_tuple(T) -> {error, "tuple: ~p", [T]};
validate(true) -> ok;
validate(false) -> ok;
validate(null) -> ok;
validate(A) when is_atom(A) -> {error, "atom: ~p", [A]};
validate(N) when is_number(N) -> ok;
validate(B) when is_binary(B) -> ok.
validate_list(L) -> [validate(I) || I <- L].
validate_proplist(L) -> [vp(I) || I <- L].
vp({K, V}) when is_binary(K) -> validate(V);
vp({K, _V}) -> {error, "bad key: ~p", [K]};
vp(H) -> {error, "not two tuple: ~p", [H]}.
flatten_errors(L) ->
case [{F, A} || I <- lists:flatten([L]), {error, F, A} <- [I]] of
[] -> ok;
E -> {errors, E}
end.
|