www.arjuna.com news company products standards research library forums contact
Trail map 1. Installation Content 2. The Sample Application 3. Deploying and Testing the Sample Application 4. Making the Sample Application Persistent 5. Recovery From Failure 6. Where Next? Navigation Home Previous Additional Trails Distributed Transaction Processing The X/Open DTP Model The OMG OTS Model Transactions in J2EE Configuring ATS Configuring the Recovery Manager ORB Portability Transactional Object For Java
| print this page
| email this page
Implementing the Bank interface To take benefit from the persistency and locking mechanism provided by ArjunaCore, a user class can inherit from the appropriate class (StateManager for recovery, and LockManager for recovery and concurrency control). The BankImpl class that implements the Bank interface inherits the LockManager and implements the BankOperations interface generated by the CORBA IDL compiler. Since multiple inheritance is not allowed in Java, inheriting the BankPOA class, as made in simple jts remote version, in addition to the LockManager is not possible. That we use in this version a CORBA TIE mechanism to associate a servant to an CORBA object reference. The Java interface definition of the BankImpl class is given below:
    public class BankImpl extends LockManager implements BankOperations
    {
      public BankImpl(OA oa);
      public BankImpl(Uid uid, OA oa);
      public BankImpl(Uid uid);
      public Account create_account( String name );
      public Account get_account( String name );
      public boolean save_state (OutputObjectState os, int ObjectType);
      public boolean restore_state (InputObjectState os, int ObjectType);
      public String type();

      public static final int ACCOUNT_SIZE = 10;
      // ACCOUNT_SIZE is the maximum number of accounts
      private String [] accounts;
      private int numberOfAccounts;
      private ORB _orb;
      private OA _oa;
      private java.util.Hashtable _accounts; //The list of accounts

    }
- Constructors and Destructor To use an existing persistent object requires the use of a special constructor that is required to take the Uid of the persistent object; the implementation of such a constructor is given below:
      public BankImpl(Uid uid)
      {
        super(uid);
        _accounts = new java.util.Hashtable();
        numberOfAccounts = 0;
        accounts = new String[ACCOUNT_SIZE];
      }
  The following constructor is invoked during the first creation of the Bank Object.
      public BankImpl(OA oa)
      { super(ObjectType.ANDPERSISTENT);
        _accounts = new java.util.Hashtable();
        _oa = oa;
        numberOfAccounts = 0;
        accounts = new String[ACCOUNT_SIZE];
      }
  The following constructor is invoked on successive BankServer restart. A bank already exists and should be recreated. Invoking super or the constructor of the inherited class leads to execute the restore_state method, described below, of the BankImpl class to rebuild the list of accounts previously created, if any.
      public BankImpl(Uid uid, OA oa)
      { super(uid);
        _accounts = new java.util.Hashtable();
        _oa = oa;
        numberOfAccounts = 0;
        accounts = new String[ACCOUNT_SIZE];
      }
The destructor of the queue class is only required to call the terminate operation of LockManager.
    public void finalize ()
    {
      super.terminate();
    }
- account management operations
      public Account create_account( String name )
      {
        AccountImpl acc;
        AccountPOA account = null;
        //Attempt to obtain the lock for change
        if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED)
         {
           //Check if the maximum number of accounts is not reached
           if (numberOfAccounts < ACCOUNT_SIZE)
           {
           acc = new AccountImpl(name); //Create a new account
           //Use the TIE mechanism to create a CORBA object
            account = new AccountPOATie(acc);
            //Add the account to the list of accounts that
            //facilitate to retrieve accounts
            _accounts.put( name, acc);
             //The Uid of the created account is put in the array
             accounts[numberOfAccounts] = acc.get_uid().toString();
             numberOfAccounts++;
           }
        }
        return com.arjuna.demo.jts.txojbank.
             AccountHelper.narrow(_oa.corbaReference(account));
      }

      public Account get_account(String name)
        throws NotExistingAccount
      {
        // Only the hashtable list is used to retrieve the account
        AccountImpl acc = ( AccountImpl ) _accounts.get( name );
        AccountPOA account = new AccountPOATie(acc);
        if ( acc == null )
           throw new NotExistingAccount("The Account
              requested does not exist");
        return com.arjuna.demo.jts.txojbank.
          AccountHelper.narrow(_oa.corbaReference(account));
      }
- save_state, restore_state and type
      public boolean save_state (OutputObjectState os, int ObjectType)
      {
         if (!super.save_state(os, ObjectType))
           return false;

         try
         {
           os.packInt(numberOfAccounts);
           if (numberOfAccounts > 0)
           {
            // All Uid located in the array will be saved
            for (int i = 0; i < numberOfAccounts; i++)
              os.packString(accounts[i]);
           }
           return true;
         }
         catch (Exception e)
         {
           return false;
         }
      }
      public boolean restore_state (InputObjectState os, int ObjectType)
      {
         if (!super.restore_state(os, ObjectType))
         {
           return false;
         }
         try
         {
            numberOfAccounts = os.unpackInt();

            if (numberOfAccounts > 0)
            {
              for (int i = 0; i < numberOfAccounts; i++)
              {
                accounts[i] = os.unpackString();
                //each stored Uid is re-used to recreate
                //a stored account object
                AccountImpl acc = new AccountImpl(new Uid(accounts[i]));
                acc.activate();
                //Once recreated the account object
                //is activated and added to the list.
               _accounts.put( acc.getName(), acc);
              }
            }
            return true;
         }
         catch (Exception e)
         {
            return false;
         }
      }
      public String type ()
      {
         return "/StateManager/LockManager/BankServer";
      }
##### Sample Application Source Code Full source code for the BankImpl class is included to provide you with a starting point for experimentation.
Copyright 2002-2005 Arjuna Technologies All Rights Reserved. info@arjuna.com +44 191 243 0676