/*
 * GeneratorBean.java
 *
 * Created on August 10, 2000, 12:03 PM
 */

package com.hitachi.hitel.genesis.em.asynchronous;

import javax.ejb.*;
import javax.rmi.*;
import javax.jms.*;
import javax.naming.*;

/**
 *
 * @author  jjones
 * @version
 */
public class GeneratorBean extends Object implements SessionBean, JMSConstants {

    private Context ctx;
    private SessionContext context;
    private QueueConnection conn;
    private QueueSession session;
    private QueueSender sender;
    private Queue queue;


    /** Creates new GeneratorBean */
    public GeneratorBean() {}

    public void setSessionContext(final javax.ejb.SessionContext p1)
    throws javax.ejb.EJBException {

        context = p1;
    }

    public void ejbRemove() throws javax.ejb.EJBException {
        stopJMS();
    }

    public void ejbActivate() throws javax.ejb.EJBException {}

    public void ejbPassivate() throws javax.ejb.EJBException {}

    public void ejbCreate() throws javax.ejb.EJBException {
        try {
            startJMS();
        }
        catch(NamingException ne) {
            ne.printStackTrace();
        }
        catch(JMSException jmse) {
            jmse.printStackTrace();
        }
    }

   
    public void sendMessage(String ejb, int numCreateArgs, Object[] createArgs,
     String method, int numMethodArgs, Object[] methodArgs) throws JMSException {

      Message msg = session.createMessage();

      msg.setJMSType(ASYNCHRONOUS_REQUEST);
      msg.setStringProperty(EJB_NAME, ejb);
      msg.setStringProperty(CREATE_METHOD, "create");
      msg.setIntProperty(CREATE_METHOD_N_ARGS, numCreateArgs);
      msg.setStringProperty(METHOD_NAME, method);
      msg.setIntProperty(METHOD_N_ARGS, numMethodArgs);

      for ( int i = 0; i < numCreateArgs; i++) {
            msg.setStringProperty (CREATE_METHOD_TYPE + i, createArgs[i].getClass().getName());
            msg.setStringProperty (METHOD_TYPE + i, methodArgs[i].getClass().getName());
      }

      sender.send(msg);

    }

    private void startJMS() throws NamingException, JMSException {
        ctx = new InitialContext();
        Object obj = ctx.lookup(QUEUE_FACTORY_LOCATION);
        QueueConnectionFactory factory = (QueueConnectionFactory)
        PortableRemoteObject.narrow(obj, QueueConnectionFactory.class);
        Object obj2 = ctx.lookup(QUEUE_LOCATION);
        queue = (Queue)PortableRemoteObject.narrow(obj2, Queue.class);
        conn = factory.createQueueConnection();
        conn.start();
        session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        sender = session.createSender(queue);
    }

    private void stopJMS() {
        try {
            sender.close();
            session.close();
            conn.close();
        }
        catch (JMSException jmse) {
            System.err.println("Error closing JMS resources: " + jmse.getMessage());
        }
    }
}