/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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 java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.SessionSynchronization;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.NotSupportedException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

// the bean will use JMS !
import javax.jms.*;

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

    SessionContext ejbContext;

    // JNDI will be used to get the JMS administered objects
    InitialContext ictx;
    TopicConnectionFactory tcf = null;
    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) {

	TopicConnection tc = null;
	TopicSession session = null;
	TopicPublisher tp = null;

	System.out.println ("Method sendMsg("+ s +")");
	// create TopicConnection, TopicSession and TopicPublisher
	try {
	    tc = tcf.createTopicConnection();	
	    session = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
	    tp = session.createPublisher(topic);
	}
	catch (Exception e) {
	    e.printStackTrace();
	    System.exit(2);
	}

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

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

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

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

	    // Lookup the TopicConnectionFactory JMS object
	    // that will be used to send messages
	    try {
		tcf = (TopicConnectionFactory) ictx.lookup("java:comp/env/jms/conFactSender");
		System.out.println("EjbComp : TopicConnectionFactory = "
				   + tcf.toString());
	    } catch(Exception e) {
		e.printStackTrace();
		System.exit(2);
	    }
	    
	    // 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(Exception e) {
		e.printStackTrace();
		System.exit(2);
	    }	
	}

    }
}
