/*
 * 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 "License"); 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: SimpleJmsXa.java,v 1.2 2002/10/24 14:05:31 jmesnil Exp $
 *-----------------------------------------------------------------------------
 */


import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.TransactionManager;

import org.objectweb.jonas_jms.api.JmsManager;
import org.objectweb.jonas_jms.JmsManagerImpl;
import org.objectweb.jonas_jms.TraceJms;

import java.io.PrintWriter;

/**
 * Created on Mar 4, 2002
 * @author  Christophe Ney - Lutris Technologies cney@batisseurs.com
 */
public class SimpleJmsXa {

    public static void main(String[] args) {

        // parse args
        String jmsAdminClassName = "org.objectweb.jonas_jms.JmsAdminForJoram";
        if (args.length>0) {
            jmsAdminClassName = args[0];
        }

        // get initial context
        Context ctx = null;
        try {
            ctx = new InitialContext();
        } catch (NamingException e) {
            System.err.println("No Initial Context");
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("[SimpleJmsXa] lookup the TransactionManager.");
        // get transaction manager
        TransactionManager tm = null;
        try {
            tm = (TransactionManager) ctx.lookup("TransactionManager");
        } catch (NamingException e) {
            System.err.println("TransactionManager not found");
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("[SimpleJmsXa] start the JMS server.");
        // load the JMS Resource Adaptor using the JOnAS JmsAdministration SPI
        // The SPI is made of one interface org.objectweb.jonas_jms.api.JmsAdministration
        Class cAdmin = null;
        try {
            cAdmin = Class.forName(jmsAdminClassName);
        } catch (ClassNotFoundException e) {
            System.err.println("Cannot load class "+jmsAdminClassName);
            e.printStackTrace();
            System.exit(1);
        }

        // the MOM is collocated in the VM (could be remote)
        boolean collocated = true;
        String url = null;

        // init the JMS Resource Manager using the JOnAS JmsManager API
        // The API is made of only one interface org.objectweb.jonas_jms.api.JmsManager
        TraceJms.setVerbose(false);
        TraceJms.setDebug(false);
        TraceJms.setLogWriter(new PrintWriter(System.out,true));
        JmsManager jmsMg = JmsManagerImpl.getJmsManager();
        try {
            jmsMg.init(cAdmin, collocated, url, tm);
        } catch (Exception e) {
            System.err.println("Cannot initialize JmsManager");
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("[SimpleJmsXa] JMS server started.");                

        System.out.println("[SimpleJmsXa] create JMS objects, register them in JOTM and bind them.");
        try {
            // Rebind theQuueConnectionFactory for JNDI access
            // It is similar to binding a datasource for a given driver
            ctx.rebind("theQueueConnectionFactory", jmsMg.getQueueConnectionFactory());

            // Create and rebind theQueue
            // It is similar to creating a TABLE in a database, but the name
            // space is used to store the name
            Queue queue = jmsMg.createQueue("theQueue");
            ctx.rebind("theQueue", queue);

        } catch (NamingException e) {
            System.err.println("Cannot rebind JMS objects");
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            System.err.println("Cannot create Queue");
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("[SimpleJmsXa] JMS objects available.");

        System.out.println("[SimpleJmsXa] start simple sender.");
        // send a few messages on theQueue (current thread for now)
        new SimpleSender().run();

        System.out.println("[SimpleJmsXa] start simple receiver.");
        // receive messages found in theQueue (SimpleReceiver is a MessageListener)
        new SimpleReceiver().run();

        // stop the JMS Manager
        try {
            ctx.unbind("theQueueConnectionFactory");
            ctx.unbind("theQueue");
            jmsMg.stop();
        } catch (Exception e) {
            System.err.println("Error while shutting down");
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("[SimpleJmsXa] JMS server stopped");
        // stop the VM since ReferenceWarpper used for binding Reference in JNDI
        // are not unexported : jdk bug
        System.exit(0);
    }
}
