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
|
%%--------------------------------------------------------------------
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% 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.
%%
%% %CopyrightEnd%
%%
%%
%%-----------------------------------------------------------------
%% File: orber_interceptors.erl
%%
%% Description:
%% This file contains the code for calling interceptors
%%
%%-----------------------------------------------------------------
-module(orber_interceptors).
-include_lib("orber/include/corba.hrl").
%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([call_send_message_interceptors/2, call_receive_message_interceptors/1,
call_request_interceptors/2]).
-export([push_system_message_interceptor/2, pop_system_message_interceptor/1,
create_interceptor_table/0]).
%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([]).
%%-----------------------------------------------------------------
%% External interface functions
%%-----------------------------------------------------------------
call_receive_message_interceptors(Bytes) ->
case getInMessageInterceptors() of
[] ->
Bytes;
Interceptors ->
apply_message_interceptors(Interceptors, receive_message, corba:create_nil_objref(),
lists:flatten(Bytes))
end.
call_send_message_interceptors(ObjRef, Bytes) ->
case getOutMessageInterceptors() of
[] ->
Bytes;
Interceptors ->
apply_message_interceptors(Interceptors, send_message, ObjRef, lists:flatten(Bytes))
end.
call_request_interceptors(in, Bytes) ->
case getInRequestInterceptors() of
[] ->
Bytes;
Interceptors ->
Bytes
end;
call_request_interceptors(out, Bytes) ->
case getOutRequestInterceptors() of
[] ->
Bytes;
Interceptors ->
Bytes
end.
create_interceptor_table() ->
%% Should be replicated mnesia
ets:new(orber_interceptors, [protected, named_table, set]),
ets:insert(orber_interceptors, {message_in_interceptors, []}),
ets:insert(orber_interceptors, {message_out_interceptors, []}).
push_system_message_interceptor(in, Mod) ->
case ets:lookup(orber_interceptors, message_in_interceptors) of
[{_, Interceptors}] ->
ets:insert(orber_interceptors, {message_in_interceptors, [Mod | Interceptors]});
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end;
push_system_message_interceptor(out, Mod) ->
case ets:lookup(orber_interceptors, message_out_interceptors) of
[{_, Interceptors}] ->
ets:insert(orber_interceptors, {message_out_interceptors, Interceptors ++ [Mod]});
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end.
pop_system_message_interceptor(in) ->
case ets:lookup(orber_interceptors, message_in_interceptors) of
[{_, []}] ->
ok;
[{_, [_ | Interceptors]}] ->
ets:insert(orber_interceptors, {message_in_interceptors, Interceptors});
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end;
pop_system_message_interceptor(out) ->
case ets:lookup(orber_interceptors, message_out_interceptors) of
[{_, []}] ->
ok;
[{_, Interceptors}] ->
ets:insert(orber_interceptors, {message_out_interceptors, remove_last_element(Interceptors)});
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end.
%%-----------------------------------------------------------------
%% Internal functions
%%-----------------------------------------------------------------
getInMessageInterceptors() ->
case ets:lookup(orber_interceptors, message_in_interceptors) of
[{_, Interceptors}] ->
Interceptors;
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end.
getOutMessageInterceptors() ->
case ets:lookup(orber_interceptors, message_out_interceptors) of
[{_, Interceptors}] ->
Interceptors;
_ ->
corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
end.
getInRequestInterceptors() ->
[].
getOutRequestInterceptors() ->
[].
apply_message_interceptors([], F, ObjRef, Bytes) ->
Bytes;
apply_message_interceptors([M | Rest], F, ObjRef, Bytes) ->
apply_message_interceptors(Rest, F, ObjRef, apply(M, F, [ObjRef, Bytes])).
remove_last_element([]) ->
[];
remove_last_element([M]) ->
[];
remove_last_element([M |Tail]) ->
remove_last_element([Tail]).
|