<?xml version="1.0"?>
<!--
  JBoss, Home of Professional Open Source
  Copyright 2006, Red Hat Middleware LLC, and individual contributors
  as indicated by the @author tags.
  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 Lesser General Public License, v. 2.1.
  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 Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License,
  v.2.1 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="JBossTS Trail map: Running and Testing Examples" rootpath="../..">
	<header>         Trail map: Running and Testing Examples     </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_jts.html">Previous</link>
			</links>
			<links include="includes/trailmap_additional.xml"/>
		</navigation>
		<area title="Running and Testing examples">
			<html>
				<h4>How the Banking Application is build using JTS interfaces</h4>
				<p>From an architectural point of view of JTS, the bank client is considered as
  an application program able to manage transactions either in a <a class="linkscoloured" href="ots_overview.html#program_models">direct or indirect
  management mode</a>, respectively with the interfaces <a class="linkscoloured" href="ots_overview.html#TransactionFactory">org.omg.CosTransactions.TransactionFactory</a>
  and <a class="linkscoloured" href="ots_overview.html#Terminator">org.omg.CosTransactions.Terminator</a> or with the <a class="linkscoloured" href="ots_overview.html#Current">org.omg.CosTransactions.Current</a>
  interface. Transactions created by the client in the Banking application are
  done in the indirect mode. </p>
				<p> The following portion of code illustrates how a JTS transaction is started
  and terminated when the client asks to transfer money from one account to another.
  This also describes what are JBossTS packages that need to be used in order
  to obtain appropriate objects instances (such Current).</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/jts/localbank/BankClient.java">BankClient.java</a>)</p>
<blockquote>
<pre>
package com.arjuna.demo.jta.localbank;
<strong>import com.arjuna.ats.jts.OTSManager;</strong>

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;Transfert 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
       // JBossTS class to obtain a Current instance
       <strong>Current current = OTSManager.get_current(); </strong>
       System.out.println(&quot;Beginning a User
                     transaction to get balance&quot;);
       <strong>current.begin()</strong>;

       Account supplier = _bank.get_account( name_supplier );
       Account consumer = _bank.get_account( name_consumer );
       supplier.debit( famount );
       consumer.credit( famount );

       <strong>current.commit( )</strong>;
      }
     catch (Exception e)
      {
       System.err.println(&quot;ERROR - &quot;+e);
      }
   }
   </pre>
<p>Since JTS is used invocations against an ORB are needed, such ORB and Object
    Adapter instantiation and initialisation. To ensure a better portability,
    the <a class="linkscoloured" href="JBossTS_ORBPortability.html"> ORB Portability API</a> provides a set of
    methods that can be used as described below. </p>
<pre>
public static void main( String [] args )
{
  try
   { <strong>
    // Create an ORB instance
    myORB = ORB.getInstance(&quot;test&quot;);
    //Obtain the Root POA
    myOA = OA.getRootOA(myORB);
    //Initialise the ORB
    myORB.initORB(args, null);
    //Initialise the POA
    myOA.initOA();
     ....
     </strong>
   }
   catch(Exception e)
   { ....}
}
</pre>
				</blockquote>
				<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="ots_overview.html#Resource">org.omg.CosTransactions.Resource</a>,
  then able to participate to the transaction commitment. For this aim, the Account
  object has to register the AccountResource object as a participant, after having
  obtaining the reference of the <a class="linkscoloured" href="ots_overview.html#Coordinator">org.omg.CosTransactions.Coordinator</a> object ,
  itself obtained via the <a class="linkscoloured" href="ots_overview.html#Control">org.omg.CosTransactions.Control</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 getResource().balance();;
 }

 public void credit( float value )
 {
   getResource().credit( value );
 }

 public void debit( float value )
 {
   getResource().debit( value );
 }

 public AccountResource getResource()
 {
   try
   {
    if (accRes == null) {
     accRes = new AccountResource(this, _name) ;
     <strong>Resource  ref = org.omg.CosTransactions.ResourceHelper.
      narrow(OA.getRootOA(ORB.getInstance("test")).corbaReference(accRes));</strong>
     <strong>RecoveryCoordinator recoverycoordinator = OTSManager.get_current().
      get_control().get_coordinator().register_resource(ref);</strong>
    }
  }
  catch (Exception e)
   {...}
   return accRes;
 }
  ...
}</pre>

				<p>To be considered as a org.omg.CosTransactions.Resource, the AccountResource
  class shall extends the class org.omg.CosTransactions.ResourcePOA generated
  by the CORBA IDL compiler. The AccountRessource provides similar methods as
  the Account class (credit, withdraw and balance) with the appropriate methods
  to participate to the <a class="linkscoloured" href="part2.html#2pc">2PC protocol</a>. 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>extends org.omg.CosTransactions.ResourcePOA</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 org.omg.CosTransactions.Vote prepare() </strong>
	   throws org.omg.CosTransactions.HeuristicMixed,
	   org.omg.CosTransactions.HeuristicHazard<br/>    {<br/>	  if ( _initial_balance == _current_balance )<br/>       return org.omg.CosTransactions.Vote.VoteReadOnly;<br/>     if ( _current_balance &lt; 0 )
       return org.omg.CosTransactions.Vote.VoteRollback;<br/>     return org.omg.CosTransactions.Vote.VoteCommit;<br/>    }

   <strong>public void rollback()</strong>
     throws org.omg.CosTransactions.HeuristicCommit,
     org.omg.CosTransactions.HeuristicMixed,
     org.omg.CosTransactions.HeuristicHazard
   {
     //Nothing to do
   }

   <strong>public void commit()</strong>
     throws org.omg.CosTransactions.NotPrepared,
     org.omg.CosTransactions.HeuristicRollback,
     org.omg.CosTransactions.HeuristicMixed,
     org.omg.CosTransactions.HeuristicHazard
   {
      _account._balance = _current_balance;
   }

   public void commit_one_phase()
     throws org.omg.CosTransactions.HeuristicHazard
   {
     _account._balance = _current_balance;
   }
   .....
   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 is included to provide you with
  a starting point for experimentation. </p>
				<ul>
					<li>JTS Version
    <ul>
							<li>
								<a class="linkscoloured" href="../src/com/arjuna/demo/jts/localbank/BankClient.java">BankClient.java</a>
							</li>
							<li>
								<a class="linkscoloured" href="../src/com/arjuna/demo/jts/localbank/Bank.java">Bank.java</a>
							</li>
							<li>
								<a class="linkscoloured" href="../src/com/arjuna/demo/jts/localbank/Account.java">Account.java</a>
							</li>
							<li>
								<a class="linkscoloured" href="../src/com/arjuna/demo/jts/localbank/AccountResource.java">AccountRessource.java</a>
							</li>
							<li>
								<a class="linkscoloured" href="../src/com/arjuna/demo/jts/localbank/NotExistingAccount.java">NotExistingAccount.java</a>
							</li>
						</ul>
					</li>
				</ul>
			</html>
		</area>
	</content>
	<footer/>
</page>
