<?xml version="1.0"?>
<!--
  JBoss, Home of Professional Open Source
  Copyright 2006, JBoss Inc., and individual contributors as indicated
  by the @authors tag.  All rights reserved. 
  See the copyright.txt in the distribution for a full listing 
  of individual contributors.
  This copyrighted material is made available to anyone wishing to use,
  modify, copy, or redistribute it subject to the terms and conditions
  of the GNU General Public License, v. 2.0.
  This program is distributed in the hope that it will be useful, but WITHOUT A 
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  You should have received a copy of the GNU General Public License,
  v. 2.0 along with this distribution; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  MA  02110-1301, USA.
  
  (C) 2005-2006,
  @author JBoss Inc.
-->
<page title="Arjuna Technologies Limited: Trail map: Running and Testing Examples" rootpath="../..">
	<header>         Trail map: The banking Application with JTA     </header>
	<content>
		<navigation>
			<links include="includes/www_arjuna_com_navigation.xml"/>
			<links include="includes/trailmap_navigation.xml"/>
			<links title="Navigation">
				<link ref="index.html">Home</link>
				<link ref="part5_testing_jta.html">Previous</link>
			</links>
			<links include="includes/trailmap_additional.xml"/>
		</navigation>
		<area title="Building The Banking Application with JTA ">
			<html>
				
				<p>From an architectural point of view of <a class="linkscoloured" href="JTA_Overview.html">JTA</a>, the bank client is considered as 
 an application program able to manage transactions via the <a class="linkscoloured" href="JTA_Overview.html#UserTransaction">javax.transaction.UserTransaction </a> interface. The following portion of code illustrates how a JTA transaction is
 started and terminated when the client asks to transfer money from one account  to another. This also describes what are ArjunaTS packages that need to be used
  in order to obtain appropriate objects instances (such UserTransaction).</p>
				<p>Note: The code below is a simplified view of the BankClient.java program. Only the transfer operation is illustrated; other operations manage transactions in the same way. 
 (see for details the <a href="../src/com/arjuna/demo/jta/localbank/BankClient.java">BankClient.java</a>)</p>
<pre>
package com.arjuna.demo.jta.localbank;
public class BankClient
{
   private Bank _bank;
   // This operation is used to make a transfer 
   //from an account to another account
   private void makeTransfer()
   {
     System.out.print(&quot;Take money from : &quot;);
     String name_supplier = input();
   
     System.out.print(&quot;Put money to : &quot;);
     String name_consumer = input();
   
     System.out.print(&quot;Transfer amount : &quot;);
     String amount = input();
   
     float famount = 0;
     try
      {
        famount = new Float( amount ).floatValue();
      }
     catch ( java.lang.Exception ex )
      {
        System.out.println(&quot;Invalid float number, abort operation...&quot;);
        return;
      }
   
     try
      {
       //the following instruction asks a specific ArjunaTA 
       //class to obtain a UserTransaction instance
       <strong>javax.transaction.UserTransaction userTran = 
                     com.arjuna.ats.jta.UserTransaction.userTransaction()</strong>;
       System.out.println(&quot;Beginning a User transaction to get balance&quot;);
       <strong>userTran.begin()</strong>;
    
       Account supplier = _bank.get_account( name_supplier );
       Account consumer = _bank.get_account( name_consumer );
       supplier.debit( famount );
       consumer.credit( famount );
    
       <strong>userTran.commit( )</strong>;
      }
     catch (Exception e)
      {
       System.err.println(&quot;ERROR - &quot;+e);
      }
   }
   ......
}
</pre>
<p>The Bank object has mainly two operations: creating an account, which is added in the account list, 
and returning an Account object. No transactional instruction  is performed by the Bank object</p>

<pre>
package com.arjuna.demo.jta.localbank;
public class Bank {
   private java.util.Hashtable _accounts;
   
   public Bank()
   {
     _accounts = new java.util.Hashtable();
   }
   
   public Account create_account( String name )
   {
     Account acc = new Account(name);
     _accounts.put( name, acc );
      return acc;
   }
   
   public Account get_account(String name)
   throws NotExistingAccount
   {
     Account acc = ( Account ) _accounts.get( name );
     if ( acc == null )
       throw new NotExistingAccount(&quot;The Account requested does not exist&quot;);
     return acc;
   }
}
</pre>

<p>The Account object provides mainly three methods <font face="Courier New, Courier, mono">balance</font>, 
  <font face="Courier New, Courier, mono">credit</font> and <font face="Courier New, Courier, mono">withdraw</font>. 
  However, in order to provide the transactional behaviour, rather than to modify 
  the current account directly (according to credit or withdraw) this task is 
  delegated to an AccountResource object that is able, according to the transaction 
  outcome, to set the account value either to its initial state or its final state.<br/>
  The AccountResource object is in fact an object that implements the <a class="linkscoloured" href="JTA_Overview.html#XAResource">javax.transactions.XAResource</a>, 
  then able to participate to the transaction commitment. For this aim, the Account 
  object has to register or enlist the AccountResource object as a participant 
  after having obtaining the reference of the <a class="linkscoloured" href="JTA_Overview.html#Transaction">javax.transaction.Transaction</a> object 
  via the <a class="linkscoloured" href="JTA_Overview.html#TransactionManager">javax.transaction.TransactionManager</a> object</p>

<pre>
package com.arjuna.demo.jta.localbank;

public class Account
{
   float _balance;
   AccountResource accRes = null;
   
   public Account(String name)
   {
     _name = name;
     _balance = 0;
   }
   
   public float balance()
   {
     return getXAResource().balance();;
   }
   
   public void credit( float value )
   {
     getXAResource().credit( value );
   }
   
   public void debit( float value )
   {
     getXAResource().debit( value );
   }
   
   public AccountResource getXAResource()
   {
   
     try
     {
       <strong>javax.transaction.TransactionManager transactionManager = 
         com.arjuna.ats.jta.TransactionManager.transactionManager();
       javax.transaction.Transaction currentTrans = 
          transactionManager.getTransaction();</strong>
   
       if (accRes == null) {
         <strong>currentTrans.enlistResource( 
            </strong>accRes = new AccountResource(this, _name)<strong> );</strong>
       }
   
       currentTrans.delistResource( accRes, XAResource.TMSUCCESS );
    
     }
     catch (Exception e)
     {
       System.err.println(&quot;ERROR - &quot;+e);
     }
     return accRes;
   }
   ...
}
</pre>

<p>The AccountResource class that implements the javax.transaxtion.XAResource 
  interface provides similar methods as the Account class (credit, withdraw and 
  balance) but also all methods specified by the javax.transaxtion.XAResource. 
  The following portion of code describes how the methods <font face="Courier New, Courier, mono">prepare</font>, 
  <font face="Courier New, Courier, mono">commit</font> and <font face="Courier New, Courier, mono">rollback</font> 
  are implemented.</p>

<pre>
public class AccountResource <strong>implements XAResource</strong>
{
   public AccountResource(Account account, String name )
   {
     _name = name;
     _account = account;
     _initial_balance = account._balance;
     _current_balance = _initial_balance;
   }
   
   public float balance()
   {
     return _current_balance;
   }
   
   public void credit( float value )
   {
     _current_balance += value;
   }
   
   public void debit( float value )
   {
     _current_balance -= value;
   }
   
   <strong>public void commit(Xid id, boolean onePhase) throws XAException</strong>
   {
     //The value of the associated Account object is modified      
     _account._balance = _current_balance;
   }
   
   <strong>public int prepare(Xid xid) throws XAException</strong>
   {
     if ( _initial_balance == _current_balance ) //account not modified
        return (XA_RDONLY);
     if ( _current_balance &lt; 0 )
        throw new XAException(XAException.XA_RBINTEGRITY);
        //If the integrity of the account is corrupted then vote rollback
     return (XA_OK); //return OK 
   }
   </pre>
<pre>
<strong>public void rollback(Xid xid) throws XAException</strong>
   {
     //Nothing is done
   }
   </pre>
<pre>   private float _initial_balance;
   private float _current_balance;
   private Account _account;
   
   }
 }</pre>

				<h5>Sample Application Source Code</h5>
				<p>Full source code for the banking application with JTA is included to provide 
  you with a starting point for experimentation. </p>
				<ul>
					<li>
						<a class="linkscoloured" href="../src/com/arjuna/demo/jta/localbank/BankClient.java">BankClient.java</a>
					</li>
					<li>
						<a class="linkscoloured" href="../src/com/arjuna/demo/jta/localbank/Bank.java">Bank.java</a>
					</li>
					<li>
						<a class="linkscoloured" href="../src/com/arjuna/demo/jta/localbank/Account.java">Account.java</a>
					</li>
					<li>
						<a class="linkscoloured" href="../src/com/arjuna/demo/jta/localbank/AccountResource.java">AccountResource.java</a>
					</li>
					<li>
						<a class="linkscoloured" href="../src/com/arjuna/demo/jta/localbank/NotExistingAccount.java">NotExistingAccount.java</a>
					</li>
				</ul>
			</html>
		</area>
	</content>
	<footer/>
</page>
