aboutsummaryrefslogtreecommitdiffstats
path: root/lib/cosTransactions/src/CosTransactions_TransactionFactory_impl.erl
blob: 36e37e2d5f6a10917bb551494b7035bd55ed5a5c (plain) (blame)
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
%%--------------------------------------------------------------------
%%
%% %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    : CosTransactions_TransactionFactory_impl.erl
%% Purpose : Is provided to allow the transaction originator to begin
%%           a transaction.
%%----------------------------------------------------------------------

-module('CosTransactions_TransactionFactory_impl').

%%--------------- INCLUDES -----------------------------------
-include_lib("orber/include/corba.hrl").

%% Local
-include_lib("ETraP_Common.hrl").
-include_lib("CosTransactions.hrl").
%%--------------- IMPORTS-------------------------------------
-import('ETraP_Common', [get_option/3]).

%%--------------- EXPORTS-------------------------------------
-export([create/3, recreate/3, init/1, terminate/2]).
-export([handle_call/3, handle_cast/2, handle_info/2, code_change/3]).

%%--------------- LOCAL DATA ---------------------------------
-record(factory, {hashMax, subtrOK, typeCheck, maxRetries, comFailWait}).

%%--------------- LOCAL DEFINITIONS --------------------------

%%------------------------------------------------------------
%% function : init
%% Arguments: 
%% Returns  : 
%% Effect   : Functions demanded by the module ic. Used to initiate
%%            a gen_server.
%%------------------------------------------------------------

init(Options) when is_list(Options) ->
    ?debug_print("Factory:init(~p)~n", [Options]),
    process_flag(trap_exit,true),
    DefaultValues  = [{maxRetries, ?tr_max_retries},
		     {comFailWait, ?tr_comm_failure_wait}|?tr_FAC_DEF],
    Hash           = get_option(hash_max, Options, DefaultValues),
    SubtrOK        = get_option(allow_subtr, Options, DefaultValues),
    TypeCheck      = get_option(typecheck, Options, DefaultValues),
    TTY            = get_option(tty, Options, DefaultValues),
    LogFile        = get_option(logfile, Options, DefaultValues),
    MaxRetries     = get_option(maxRetries, Options, DefaultValues),
    ComFailWait    = get_option(comFailWait, Options, DefaultValues),
    error_logger:tty(TTY),
    case LogFile of
	false ->
	    ok;
	_->
	    error_logger:logfile({open, LogFile})
    end,
    {ok, #factory{typeCheck = TypeCheck, hashMax = Hash, subtrOK = SubtrOK,
		  maxRetries = MaxRetries, comFailWait = ComFailWait}};

init(Options) ->
    ?tr_error_msg("TransactionFactory~nBad argument: ~p~n", [Options]),
    corba:raise(?tr_badparam).
    

%%------------------------------------------------------------
%% function : terminate
%% Arguments: 
%% Returns  : 
%% Effect   : Function demanded by the module ic. Used to 
%%            terminate a gen_server.
%%------------------------------------------------------------

terminate(_Reason, _State) ->
    ?debug_print("Factory:terminate(~p)~n", [_Reason]),
    ok.

%%------------------------------------------------------------
%% function : handle_call, handle_cast, handle_info, code_change
%% Arguments: 
%% Returns  : 
%% Effect   : Functions demanded by the module ic. 
%%------------------------------------------------------------

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

handle_call(_,_, State) ->
    {noreply, State}.


handle_cast(_, State) ->
    {noreply, State}.
    

handle_info({'EXIT',_From,shutdown}, State) ->
    ?debug_print("Factory:handle_info(~p)~n", [shutdown]),
    {stop, shutdown, State};
handle_info(_Info, State) ->
    ?debug_print("Factory:handle_info(~p)~n", [_Info]),
    {noreply, State}.

%%------------------------------------------------------------
%% function : create
%% Arguments: TimeOut - rollback the transaction after TimeOut 
%%            seconds. If 0 no timeout.
%% Returns  : a Control object
%% Effect   : Creates a new top-level transaction. The Control
%%            can be used to manage or control participation
%%            in the new transaction. Used for direct context
%%            management.
%%------------------------------------------------------------

create(_Self, State, TimeOut) when is_integer(TimeOut) ->
    %% Generate objectnames.
    ETraPName    = 'ETraP_Common':create_name("root"),
    TermName     = 'ETraP_Common':create_name("term"),
    EState       = ?tr_create_context(ETraPName, TermName,
				      State#factory.typeCheck, 
				      State#factory.hashMax, 
				      State#factory.subtrOK,
				      State#factory.maxRetries,
				      State#factory.comFailWait),

    case TimeOut of
	0 ->
	    ETraP = ?tr_start_child(?SUP_ETRAP(EState)),
	    {reply, ETraP, State};
	_ ->
	    if
		TimeOut > 0 ->
		    {MegaSecs, Secs, _Microsecs} = erlang:now(),
		    EState2 = ?tr_set_alarm(EState, MegaSecs*1000000+Secs+TimeOut),
		    EState3 = ?tr_set_timeout(EState2, TimeOut*1000),
		    ETraP = ?tr_start_child(?SUP_ETRAP(EState3)),
		    {reply, ETraP, State};
		true ->
		    ?tr_error_msg("TransactionFactory:create( Integer >= 0 )~nBad argument. Not an integer.~n", []),
		    corba:raise(?tr_badparam)
	    end
    end;

create(_Self, _State, _TimeOut) ->
    ?tr_error_msg("TransactionFactory:create( Integer >= 0 )~nBad argument. Not an integer.~n", []),
    corba:raise(?tr_badparam).


%%------------------------------------------------------------
%% function : recreate
%% Arguments: PropagationContext
%% Returns  : a Control object
%% Effect   : 
%%------------------------------------------------------------

recreate(_Self, _State, #'CosTransactions_PropagationContext'{current = _C}) ->
    corba:raise(#'NO_IMPLEMENT'{completion_status=?COMPLETED_YES}).
%recreate(Self, State, #'CosTransactions_PropagationContext'{current = C}) ->
%    {reply, C#'CosTransactions_TransIdentity'.coord, State}.


%%--------------- END OF MODULE ------------------------------