/**
 * 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$
 * --------------------------------------------------------------------------
 */

// MsgReceptor.java
// JMS client program that will receive the messages published on the Topic
// by the EJB component
package jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * @author JOnAS team
 */
public class MsgReceptor {

    private static Context ictx = null;

    private static ConnectionFactory cf = null;

    private static Topic topic = null;

    // JNDI name of the connection factory
    private static String conFactName = "JCF";

    // JNDI name of the Topic
    private static String topicName = "sampleTopic";

    public static void main(String[] arg) {

        // Get InitialContext
        try {
            ictx = new InitialContext();
            // lookup the TopicConnectionFactory through its JNDI name
            cf = (ConnectionFactory) ictx.lookup(conFactName);

            System.out.println("JMS client: cf = " + cf.toString());

            // lookup the Topic through its JNDI name
            topic = (Topic) ictx.lookup(topicName);
            System.out.println("JMS Topic topic = " + topic.toString());
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(2);
        }

        try {
            Connection tc = cf.createConnection();
            System.out.println("JMS client: tc = " + tc.toString());
            Session session = tc.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageConsumer mc = session.createConsumer(topic);
            /* create and set a MessageListener */
            MyListenerSimple listener = new MyListenerSimple();
            mc.setMessageListener(listener);

            System.out.println("JMS client: Waiting for messages ...");
            tc.start();

            System.in.read();

            session.close();
            tc.close();
        } catch (Exception e) {
            System.out.println("Exception in message reception operations");
            e.printStackTrace();
        }

    }
}

class MyListenerSimple implements MessageListener {

    MyListenerSimple() {
    }

    public void onMessage(Message msg) {
        try {
            if (msg == null) {
                System.out.println("Message: message null ");
            } else {
                if (msg instanceof ObjectMessage) {
                    String m = (String) ((ObjectMessage) msg).getObject();
                    System.out.println("JMS client: received message ======> " + m);
                } else if (msg instanceof TextMessage) {
                    String m = ((TextMessage) msg).getText();
                    System.out.println("JMS client: received message ======> " + m);
                }
            }
        } catch (Exception exc) {
            System.out.println("Exception caught :" + exc);
            exc.printStackTrace();
        }
    }
}