import java.util.*;
import javax.jms.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.transaction.UserTransaction;

public final
class Oc4jjmsDemo 
{
    public static void main(String[] argv)
    {
        try
        {
            if ((argv.length != 3)
                && (argv.length != 5))
            {
                System.err.println("Usage: Oc4jjmsDemo <qcfN> <qN>"
                    + " <nmsgs> [<user> <pass>]]");
                return;
            }

            int argc = 0;

            String qcfN  = argv[argc++];
            String qN    = argv[argc++];
            int    nmsgs = Integer.parseInt(argv[argc++]);

            String user  = null;
            String pass  = null;

            if (argv.length == 5)
            {
                user    = argv[argc++];
                pass    = argv[argc++];
            }

	    System.out.println("OC4JJMSDemo: client started...");

            Qrecv.doit(qcfN, qN, user, pass, nmsgs, false);
            Qsend.doit(qcfN, qN, user, pass, nmsgs, false);
            Qbrow.doit(qcfN, qN, user, pass, nmsgs, false);
            Qrecv.doit(qcfN, qN, user, pass, nmsgs, false);

            System.out.println("\n\n");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static Object getQCF(String name)
        throws Exception
    {
        return getCF(name, false);
    }

    private static Object getCF(String name, boolean isT)
        throws Exception
    {
        Object ret = null;

        System.out.println("*** looking up " + name + " via JNDI");

        Context ctx = new InitialContext();
        ret = ctx.lookup(name);
        ctx.close();

        return ret;
    }

    public static Object getQ(String name)
        throws Exception
    {
        return getD(name, false);
    }

    public static Object getD(String name, boolean isT)
        throws Exception
    {
        Object ret = null;

        System.out.println("*** looking up " + name + " via JNDI");

        Context ctx = new InitialContext();
        ret = ctx.lookup(name);
        ctx.close();

        return ret;
    }

    public static QueueConnection getQC(QueueConnectionFactory qcf,
        String user, String pass)
        throws Exception
    {
        return (((user != null) && (pass != null))
            ? qcf.createQueueConnection(user, pass)
            : qcf.createQueueConnection());
    }

}
