<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>1999</year><year>2013</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    
    </legalnotice>

    <title>Resource Skeletons</title>
    <prepared>Niclas Eklund</prepared>
    <docno></docno>
    <date>1999-04-29</date>
    <rev></rev>
    <file>ch_skeletons.xml</file>
  </header>

  <section>
    <title>Resource Skeletons</title>
    <p>This chapter provides a skeleton for application Resources. For more information
      see the Orber documentation.</p>
    <code type="none">
%%%-----------------------------------------------------------
%%% File    : Module_Interface_impl.erl
%%% Author  : 
%%% Purpose : 
%%% Created : 
%%%-----------------------------------------------------------
 
-module('Module_Interface_impl').
 
%%--------------- INCLUDES -----------------------------------
-include_lib("orber/include/corba.hrl").
-include_lib("cosTransactions/include/CosTransactions.hrl").

%%--------------- EXPORTS-------------------------------------
%%- Inherit from CosTransactions::Resource -------------------
-export([prepare/2, 
         rollback/2, 
         commit/2, 
         commit_one_phase/2, 
         forget/2]).

%%- Inherit from CosTransactions::SubtransactionAwareResource 
-export([commit_subtransaction/3,
         rollback_subtransaction/2]).
 
%%--------------- gen_server specific ------------------------
-export([init/1, terminate/2, code_change/3, handle_info/2]).
 
%%------------------------------------------------------------
%% function : gen_server specific
%%------------------------------------------------------------
init(Env) ->
    %% 'trap_exit' optional
    process_flag(trap_exit,true),

    %%--- Possible replies ---
    %% Reply and await next request
    {ok, State}.

    %% Reply and if no more requests within Time the special 
    %% timeout message should be handled in the 
    %% Module_Interface_impl:handle_info/2 call-back function (use the 
    %% IC option {{handle_info, "Module::Interface"}, true}).
    {ok, State, TimeOut}.

     %% Return ignore in order to inform the parent, especially if it is a 
     %% supervisor, that the server, as an example, did not start in 
     %% accordance with the configuration data. 
     ignore.

     %% If the initializing procedure fails, the reason 
     %% is supplied as StopReason.
     {stop, StopReason}.


terminate(Reason, State) ->
    ok.

code_change(OldVsn, State, Extra) ->
    {ok, NewState}.

%% If use IC option {{handle_info, "Module::Interface"}, true}
handle_info(Info, State) ->
     %%--- Possible replies ---
     %% Await the next invocation.
     {noreply, State}.
     %% Stop with Reason.
     {stop, Reason, State}.

 
%%- Inherit from CosTransactions::Resource -------------------
prepare(State) ->

    %%% Do application specific actions here %%%

    %%-- Reply: --
    %% If no data related to the transaction changed.
    {reply, 'VoteReadOnly', State}
    %% .. or (for example): 
    {stop, normal, 'VoteReadOnly', State}.

    %% If able to commit
    {reply, 'VoteCommit', State}

    %% If not able to commit
    {reply, 'VoteRollback', State}
    %% .. or (for example): 
    {stop, normal, 'VoteRollback', State}.

rollback(State) ->

    %%% Do application specific actions here %%%

    %%-- Reply: --
    %% If able to rollback successfully
    {reply, ok, State}
    %% .. or (for example): 
    {stop, normal, ok, State}.

    %% If Heuristic Decision. Raise exception:
    corba:raise(#'CosTransactions_HeuristicMixed' {})
    corba:raise(#'CosTransactions_HeuristicHazard' {})
    corba:raise(#'CosTransactions_HeuristicCommit'{})


commit(State) ->

    %%% Do application specific actions here %%%

    %%-- Reply: --
    %% If able to commit successfully
    {reply, ok, State}
    %% .. or (for example): 
    {stop, normal, ok, State}.

    %% If the prepare operation never been invoked:
    corba:raise(#'CosTransactions_NotPrepared'{})

    %% If Heuristic Decision. Raise exception:
    corba:raise(#'CosTransactions_HeuristicMixed' {})
    corba:raise(#'CosTransactions_HeuristicHazard' {})
    corba:raise(#'CosTransactions_HeuristicRollback'{})

 
commit_one_phase(State) ->

    %%% Do application specific actions here %%%

    %%-- Reply: --
    %% If able to commit successfully
    {reply, ok, State}
    %% .. or (for example): 
    {stop, normal, ok, State}.

    %% If fails. Raise exception:
    corba:raise(#'CosTransactions_HeuristicHazard' {})

    %% If able to rollback successfully
    corba:raise(#'CosTransactions_TransactionRolledBack' {})

 
forget(State) ->

    %%% Do application specific actions here %%%

    %%-- Reply: --
    {reply, ok, State}.
    %% .. or (for example): 
    {stop, normal, ok, State}.
  


%%%%%% If the Resource is also supposed to be a     %%%%%%
%%%%%% SubtransactionAwareResource implement these. %%%%%%

%%- Inherit from CosTransactions::SubtransactionAwareResource
commit_subtransaction(State, Parent) ->
    %%% Do application specific actions here %%%

    %%-- Reply: --
    {reply, ok, State}.
    %% .. or (for example): 
    {stop, normal, ok, State}.

rollback_subtransaction(State) ->
    %%% Do application specific actions here %%%

    %%-- Reply: --
    {reply, ok, State}.
    %% .. or (for example): 
    {stop, normal, ok, State}.
 
%%--------------- END OF MODULE ------------------------------
    </code>
  </section>
</chapter>

