Resource Skeletons

## 5 Resource Skeletons

### 5.1 Resource Skeletons

This chapter provides a skeleton for application Resources. For more information see the Orber documentation.

    %%%-----------------------------------------------------------
    %%% 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 failes. 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 ------------------------------

Copyright © 1991-2006 Ericsson AB