/*
 * Enhydra Java Application Server
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
 * Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License Version
 * 1.0 (the "Lbicense"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.enhydra.
 * org/software/license/epl.html
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under the
 * License.
 *-----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */

import java.lang.reflect.Field;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.UserTransaction;

/**
 * 
 * a basic example of transaction.
 * 
 * @author  Christophe Ney - Lutris Technologies cney@batisseurs.com
 */
public class BasicExample {

    public static void main(String[] args) {
        if (args.length!=1) {
            System.out.println("usage : BasicExample [userTransactionURL]");
            System.exit(1);
        }


        UserTransaction utc = null;

        try {
            System.out.println("create initial context");
            Context ictx = new InitialContext();
            System.out.println("lookup UserTransaction at : "+args[0]);
            utc = (UserTransaction)ictx.lookup(args[0]);
        } catch (Exception e) {
            System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
            System.out.println("Exception message :"+e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
        
        System.out.println();

        //a simple transaction
        try {
            System.out.println("a simple transaction which is committed:");
            System.out.println("\t- initial status : "+getStatusName(utc.getStatus()));
            utc.begin();
            System.out.println("\t- after begin status : "+getStatusName(utc.getStatus()));
            utc.commit();
            System.out.println("\t- after commit status : "+getStatusName(utc.getStatus()));            
        } catch (Exception e) {
            System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
            System.out.println("Exception message :"+e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println();
        // a transaction with a rollback
        try {
            System.out.println("a simple transaction which is rolled back.");
            System.out.println("we set a transaction timeout to 1 second, begin the transaction, "+ 
                               "and wait 5 seconds before committing it:");
            utc.setTransactionTimeout(1);
            System.out.println("\t- initial status : "+getStatusName(utc.getStatus()));
            utc.begin();
            System.out.println("\t- after begin status : "+getStatusName(utc.getStatus()));
            System.out.println("\t- wait for 5 seconds");
            Thread.sleep(5*1000);
            utc.commit();
            System.out.println("ERROR: the commit method should have failed due to timeout expiration");
            System.exit(1);
        } catch (RollbackException e) {
            try {
                System.out.println("\t- after rollback status : "+ getStatusName(utc.getStatus()));
            } catch (Exception ex) {
                System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
                System.out.println("Exception message :"+e.getMessage());
                e.printStackTrace();
                System.exit(1);
            }                
        } catch (Exception e) {
            System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
            System.out.println("Exception message :"+e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("\nBasic example is OK.");
    }

    public static String getStatusName(int status)  {
        String statusName = null;
        try {
            Field[] flds = Status.class.getDeclaredFields();
            for (int i=0; i<flds.length; i++) {
                if (flds[i].getInt(null) == status)
                    statusName = flds[i].getName();
            }
        } catch (Exception e) {
            statusName = "invalid status value!";
        } finally {
            return statusName;
        }
    }
}
