/*
 * 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 org.objectweb.jonas.jtests.beans.message;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Common code for all Message Driven Beans
 * @author Philippe Durieux, Philippe Coq
 */
public class Listener implements MessageDrivenBean, MessageListener {   
    static protected Logger logger = null;
    protected transient MessageDrivenContext mdbContext;
    protected transient MRecordHome arh = null;
    protected String myname;
    
    /** 
     * Default constructor
     */
    public Listener() {
    }

    // ------------------------------------------------------------------
    // MessageDrivenBean implementation
    // ------------------------------------------------------------------

    /** 
     * Set the associated context. The container call this method
     * after the instance creation. 
     * The enterprise Bean instance should store the reference to the context
     * object in an instance variable. 
     * This method is called with no transaction context.
     *
     * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */

    public void setMessageDrivenContext(MessageDrivenContext ctx) {
	if (logger == null)
	    logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
	logger.log(BasicLevel.DEBUG, "");
	mdbContext = ctx;
    } 

    /**
     * A container invokes this method before it ends the life of the message-driven object. 
     * This happens when a container decides to terminate the message-driven object. 
     *
     * This method is called with no transaction context. 
     *
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */
    public void ejbRemove() {
	logger.log(BasicLevel.DEBUG, "");
    }

    /**
     * The Message driven  bean must define an ejbCreate methods with no args.
     */
    public void ejbCreate() {
	logger.log(BasicLevel.DEBUG, "");

	// Get a ref on MRecordHome
	InitialContext ictx = null;
	try {
	    ictx = new InitialContext();
	    arh = (MRecordHome) PortableRemoteObject.narrow(ictx.lookup("messageMRecordECHome"), MRecordHome.class);
	} catch (NamingException e) {
	    logger.log(BasicLevel.ERROR, "Listener : Cannot get messageMRecordHome:"+e);
	}

	// Accesses the bean env to get the bean name
	// This test also that we can access java:comp/env from here.
	try {
	    myname = (String) ictx.lookup("java:comp/env/mdbname");
	} catch (NamingException e) {
	    logger.log(BasicLevel.ERROR, "Listener : Cannot access java:comp/env/mdbname from ejbCreate:"+e);
	}
    }

    /**
     *  onMessage method
     */
    public void onMessage(Message message) {
	logger.log(BasicLevel.DEBUG, "");

	// Decode the message (MapMessage)
	String uuid = null;
	String dest = null;
	int value = 0;
	MapMessage msg = (MapMessage) message;
	try {
	    uuid = msg.getString("Id");
	    dest = msg.getString("Text");
	    value = msg.getInt("Value");
	} catch (JMSException e) {
	    logger.log(BasicLevel.ERROR, "Listener exception:"+e);
	    return;
	}

	// Create a new Entity bean for this message
	try {
	    arh.create(uuid, dest, value, myname);
	} catch (CreateException e) {
	    logger.log(BasicLevel.ERROR, "Listener exception:"+e);
	} catch (RemoteException e) {
	    logger.log(BasicLevel.ERROR, "Listener exception:"+e);
	}
    }
}
