/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY 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 along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): Fran?ois Exertier
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

// EjbCompClient.java
// Client for accessing session bean EjbComp and entity bean eb.Account,
// call sendMsg (send a JMS message) on EjbComp and create an instance
// of Account (database record creation).
package jms;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;

import eb.Account;
import eb.AccountHome;

/**
 *
 */
public class EjbCompClient {

    private static Context initialContext = null;

    private static EjbCompHome home1 = null;

    private static AccountHome home2 = null;

    private static UserTransaction utx = null;

    public static void main(String[] arg) {

        // Get InitialContext
        try {
            initialContext = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(2);
        }

        // For starting transactions from client: get UserTransaction
        System.out.println("Getting a UserTransaction object from JNDI");
        try {
            utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
        } catch (Exception e) {
            System.err.println("Cannot lookup UserTransaction: " + e);
            System.exit(2);
        }

        // Lookup EjbComp bean home
        try {
            home1 = (EjbCompHome) PortableRemoteObject.narrow(initialContext.lookup("EjbCompHome"), EjbCompHome.class);
        } catch (Exception e) {
            System.err.println("Cannot lookup EjbCompHome: " + e);
            System.exit(2);
        }

        // Lookup Account bean home
        try {
            home2 = (AccountHome) PortableRemoteObject.narrow(initialContext.lookup("AccountImplHome"),
                    AccountHome.class);
        } catch (Exception e) {
            System.err.println("Cannot lookup AccountImplHome: " + e);
            // no entity bean to use for running the jms sample
            System.out.println("Sample will work without entity bean Account");
            home2 = null;
        }

        // Delete account that may have been created by a previous run
        try {
            if (home2 != null) { // use entity bean account in the sample
                Account b = home2.findByNumber(222);
                b.remove();
            }
        } catch (Exception e) {
        }

        // EjbComp creation
        EjbComp aJmsBean = null;
        try {
            System.out.println("Creating an EjbComp bean");
            aJmsBean = home1.create();
        } catch (Exception e) {
            System.err.println("Cannot create EjbComp: " + e);
            System.exit(2);
        }

        // Calling EjbComp method that sends a message and eventually creating
        // the corresponding account (i.e. in the database), in a
        // transaction that succeeds !
        Account aDataBean = null;
        try {
            utx.begin();
            System.out.println("Sending a message (will be commited)");
            aJmsBean.sendMsg("Hello commit");
            if (home2 != null) {
                System.out.println("Creating an Account bean (will be commited)");
                aDataBean = home2.create(222, "JMS Sample OK", 0);
            }
            utx.commit();
        } catch (Exception e) {
            System.err.println("Exception .... : " + e);
            e.printStackTrace();
            System.exit(2);
        }

        // Calling EjbComp method that sends a message and eventually creating
        // the corresponding account (i.e. in the database), in a
        // transaction that fails !
        try {
            utx.begin();
            System.out.println("Sending a message (will be rolled back)");
            aJmsBean.sendMsg("Hello rollback");
            if (home2 != null) {
                System.out.println("Creating a Account bean (will be rolled back)");
                aDataBean = home2.create(223, "JMS Sample KO", 0);
            }
            utx.rollback();
        } catch (Exception e) {
            System.err.println("Exception .... : " + e);
            System.exit(2);
        }

        System.out.println("If your JMS MsgReceptor windows prints");
        System.out.println("      received message ======> Hello commit");
        System.out.println("this test is successful.");
        System.out.println("You may also check that the record 222 has been");
        System.out.println("created in the database in the table accountsample");
        System.out.println("(if the sample was working with the entity bean Account).");
    }
}