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

package jms;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * EjbCompBean.java Stateful Session Bean sending and synchronously receiving
 * JMS messages.
 * @author Fran?ois Exertier Contributor(s):
 */
public class EjbCompBean implements SessionBean {

    private SessionContext ejbContext;

    // JNDI will be used to get the JMS administered objects
    private InitialContext ictx;

    private ConnectionFactory cf = null;

    private Topic topic = null;

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------

    public void setSessionContext(SessionContext ctx) {
        ejbContext = ctx;
    }

    public void ejbRemove() {
    }

    public void ejbCreate() throws CreateException {

        // initialisation of JMS administered objects (obtained through JNDI)
        // used by the bean
        initJMSObjects();
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }

    // ------------------------------------------------------------------
    // EjbComp implementation
    // ------------------------------------------------------------------

    /**
     * Send a JMS message
     * @param s - the string that will be contained by the message.
     */
    public void sendMsg(java.lang.String s) {

        javax.jms.Connection tc = null;
        Session session = null;
        MessageProducer mp = null;

        System.out.println("Method sendMsg(" + s + ")");

        // create Connection, Session and MessageProducer
        try {
            tc = cf.createConnection();
            session = tc.createSession(true, Session.AUTO_ACKNOWLEDGE);
            mp = session.createProducer((Destination) topic);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }

        // send the message to the topic
        try {
            ObjectMessage message;
            message = session.createObjectMessage();
            message.setObject(s);
            mp.send(message);
            session.close();
            tc.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }

    }

    // ------------------------------------------------------------------
    // Internal methods
    // ------------------------------------------------------------------

    /**
     * Initialize the ConnectionFactory and Topic JMS objects that are used by
     * the bean (through JNDI lookups).
     */
    private void initJMSObjects() throws CreateException {

        // Get InitialContext
        try {
            ictx = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
            System.exit(2);
        }
        if (cf == null) {

            // Lookup the ConnectionFactory JMS object
            // that will be used to send messages
            try {
                cf = (ConnectionFactory) ictx.lookup("java:comp/env/jms/conFactSender");
                System.out.println("EjbComp : ConnectionFactory = " + cf.toString());
            } catch (javax.naming.NameNotFoundException ne) {
                throw new CreateException(ne.toString());
            } catch (Exception e) {
                e.printStackTrace();
                throw new CreateException();
            }

            // Lookup the topic, the JMS Topic object on which messages
            // will be sent.
            try {
                topic = (Topic) ictx.lookup("java:comp/env/jms/topiclistener");
                System.out.println("EjbComp : Topic (jms/topiclistener) = " + topic.toString());
            } catch (javax.naming.NameNotFoundException ne) {
                throw new CreateException(ne.toString());
            } catch (Exception e) {
                e.printStackTrace();
                throw new CreateException();
            }
        }

    }
}