package examples.xml.biztalk;

import java.util.*;
import javax.naming.*;
import javax.jms.*;
import javax.jts.*;
import weblogic.jndi.Environment;

/**
 * BizAdapter is a helper class used by protocol adapter to 
 * queue documents into a Biztalk server. 
 * BizAdapter is provided an output JMS queue connection to the
 * Biztalk server named in the initOutQ method call.
 *
 * @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
 */

public class BizAdapter  {
  public final static String JNDI_FACTORY= "weblogic.jndi.WLInitialContextFactory";
  public final static String JMS_FACTORY= "javax.jms.QueueConnectionFactory";
  private QueueConnectionFactory qconFactory;
  private QueueConnection qconSender;
  private QueueSession qsessionSender;
  private QueueSender qsender;
  private Queue outqueue;
  private TextMessage outmsg;
  private Context context;

  /**
   * Constructor
   *
   */
  public BizAdapter() {
    try {
      context = (new Environment()).getInitialContext();
    }
    catch (NamingException namee) {
      System.err.println("Unable to establish JNDI context.");
    }
  }

  /**
   * Initializer for BizAdapter. Sets up the output queue. The queue name
   * is of the form <TT>biztalk.jms.nameIncoming</TT>, where the
   * name in nameIncoming is the name of a Biztalk server.
   *
   * @param outQ Output queue name 
   *
   * @exception NamingException unable to establish context
   * @exception JMSException unable to setup JMS queue
   */

  public void initOutQ(String outQ) 
                 throws NamingException, JMSException {
    initSender(context, outQ);
  }

   private void initSender(Context ctx, String queueName)
                  throws NamingException, JMSException
  {
    // Look up the ConnectionFactory
    qconFactory = (QueueConnectionFactory)ctx.lookup(JMS_FACTORY);
    
    // Get a QueueConnection from the 
    // QueueConnectionFactory
    qconSender = qconFactory.createQueueConnection();
    
    // Get a QueueSession that is not transacted
    // and acknowledges automatically 
    qsessionSender = qconSender.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    
    // Look up the Queue
    outqueue = (Queue) ctx.lookup(queueName);
    
    // Create a message producer (QueueSender)
    // for the Queue we looked up. 
    qsender = qsessionSender.createSender(outqueue);
    
    // Create a message object 
    outmsg = qsessionSender.createTextMessage();
    
    // Start the connection
    qconSender.start();
  }
  /**
   * Send a message to the queue. Use the QueueSender's default delivery mode, timeToLive and priority.
   *
   * @param message the message to be sent
   * @exception JMSException if JMS fails to send the message due to some internal error.
   * @exception MessageFormatException if invalid message specified
   * @exception InvalidDestinationException if a client uses this method with a Queue sender with an invalid queue.
   */
  public void send(String message) throws JMSException
  {
    outmsg.setText(message);
    System.out.println("sending ");
    qsender.send(outmsg);
  }
  /**
   * Send a message to the queue. Use the QueueSender's default delivery mode, timeToLive and priority.
   *
   * @param message the message to be sent
   * @exception JMSException if JMS fails to send the message due to some internal error.
   * @exception MessageFormatException if invalid message specified
   * @exception InvalidDestinationException if a client uses this method with a Queue sender with an invalid queue.
   */
  public void send(Message message) throws JMSException
  {
    qsender.send(message);
  }
  
  /**
   * Get the queue associated with this queue sender.
   *
   * @return the queue.
   * @exception JMSException if JMS fails to get queue for this queue receiver due to some internal error.
   */
   public final Queue getOutQueue() throws JMSException {
     return outqueue;
   }

  /**
   * Close out queue connections
   *
   * @exception JMSException if JMS fails to close out due to some error.
   */
  public void closeOutQ()
       throws JMSException
  {
    try {
      qsender.close();
      qsessionSender.close();
      qconSender.close();
    }
    catch (JMSException jmse) {}
  }
}
