summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Radestock <matthias@lshift.net>2009-12-18 16:57:28 +0000
committerMatthias Radestock <matthias@lshift.net>2009-12-18 16:57:28 +0000
commit85f6834412d6a6fb715a21553cee4a5a73d7c18b (patch)
tree3d9b7b242bcc9468f30f0dd5cfbbcb94ccb06741
parent46178089e1164b34ff0b07ab3b0807895b569441 (diff)
downloadrabbitmq-server-85f6834412d6a6fb715a21553cee4a5a73d7c18b.tar.gz
Backed out changeset b2ff7bbc3df6
re-introducing pluggable exchange types on a branch
-rw-r--r--src/rabbit.erl1
-rw-r--r--src/rabbit_exchange.erl18
-rw-r--r--src/rabbit_exchange_type.erl107
3 files changed, 117 insertions, 9 deletions
diff --git a/src/rabbit.erl b/src/rabbit.erl
index c6dde385..3293927a 100644
--- a/src/rabbit.erl
+++ b/src/rabbit.erl
@@ -134,6 +134,7 @@ start(normal, []) ->
fun () -> ok = rabbit_mnesia:init() end},
{"core processes",
fun () ->
+ ok = start_child(rabbit_exchange_type),
ok = start_child(rabbit_log),
ok = rabbit_hooks:start(),
diff --git a/src/rabbit_exchange.erl b/src/rabbit_exchange.erl
index 09ea1e96..be73e818 100644
--- a/src/rabbit_exchange.erl
+++ b/src/rabbit_exchange.erl
@@ -134,18 +134,18 @@ declare(ExchangeName, Type, Durable, AutoDelete, Args) ->
end
end).
-typename_to_plugin_module(T) when is_binary(T) ->
- case catch list_to_existing_atom("rabbit_exchange_type_" ++ binary_to_list(T)) of
- {'EXIT', {badarg, _}} ->
+typename_to_plugin_module(T) ->
+ case rabbit_exchange_type:lookup_module(T) of
+ {ok, Module} ->
+ Module;
+ {error, not_found} ->
rabbit_misc:protocol_error(
- command_invalid, "invalid exchange type '~s'", [T]);
- Module ->
- Module
+ command_invalid, "invalid exchange type '~s'", [T])
end.
-plugin_module_to_typename(M) when is_atom(M) ->
- "rabbit_exchange_type_" ++ S = atom_to_list(M),
- list_to_binary(S).
+plugin_module_to_typename(M) ->
+ {ok, TypeName} = rabbit_exchange_type:lookup_name(M),
+ TypeName.
check_type(T) ->
Module = typename_to_plugin_module(T),
diff --git a/src/rabbit_exchange_type.erl b/src/rabbit_exchange_type.erl
new file mode 100644
index 00000000..58dcfbb6
--- /dev/null
+++ b/src/rabbit_exchange_type.erl
@@ -0,0 +1,107 @@
+%% 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 Developers of the Original Code are LShift Ltd,
+%% Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
+%%
+%% Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
+%% Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
+%% are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
+%% Technologies LLC, and Rabbit Technologies Ltd.
+%%
+%% Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
+%% Ltd. Portions created by Cohesive Financial Technologies LLC are
+%% Copyright (C) 2007-2009 Cohesive Financial Technologies
+%% LLC. Portions created by Rabbit Technologies Ltd are Copyright
+%% (C) 2007-2009 Rabbit Technologies Ltd.
+%%
+%% All Rights Reserved.
+%%
+%% Contributor(s): ______________________________________.
+%%
+
+-module(rabbit_exchange_type).
+
+-behaviour(gen_server).
+
+-export([start_link/0]).
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-export([register/2, lookup_module/1, lookup_name/1]).
+
+-define(SERVER, ?MODULE).
+
+%%---------------------------------------------------------------------------
+
+start_link() ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+
+%%---------------------------------------------------------------------------
+
+register(TypeName, ModuleName) ->
+ gen_server:call(?SERVER, {register, TypeName, ModuleName}).
+
+lookup_module(T) when is_binary(T) ->
+ case ets:lookup(rabbit_exchange_type_modules, T) of
+ [{_, Module}] ->
+ {ok, Module};
+ [] ->
+ {error, not_found}
+ end.
+
+lookup_name(M) when is_atom(M) ->
+ [{_, TypeName}] = ets:lookup(rabbit_exchange_type_names, M),
+ {ok, TypeName}.
+
+%%---------------------------------------------------------------------------
+
+internal_register(TypeName, ModuleName)
+ when is_binary(TypeName), is_atom(ModuleName) ->
+ true = ets:insert(rabbit_exchange_type_modules, {TypeName, ModuleName}),
+ true = ets:insert(rabbit_exchange_type_names, {ModuleName, TypeName}),
+ ok.
+
+%%---------------------------------------------------------------------------
+
+init([]) ->
+ rabbit_exchange_type_modules =
+ ets:new(rabbit_exchange_type_modules, [protected, set, named_table]),
+ rabbit_exchange_type_names =
+ ets:new(rabbit_exchange_type_names, [protected, set, named_table]),
+
+ %% TODO: split out into separate boot startup steps.
+ ok = internal_register(<<"direct">>, rabbit_exchange_type_direct),
+ ok = internal_register(<<"fanout">>, rabbit_exchange_type_fanout),
+ ok = internal_register(<<"headers">>, rabbit_exchange_type_headers),
+ ok = internal_register(<<"topic">>, rabbit_exchange_type_topic),
+
+ {ok, none}.
+
+handle_call({register, TypeName, ModuleName}, _From, State) ->
+ ok = internal_register(TypeName, ModuleName),
+ {reply, ok, State};
+handle_call(Request, _From, State) ->
+ {stop, {unhandled_call, Request}, State}.
+
+handle_cast(Request, State) ->
+ {stop, {unhandled_cast, Request}, State}.
+
+handle_info(Message, State) ->
+ {stop, {unhandled_info, Message}, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.