package dk.topsecurity;
//import javax.jms.*;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MessageEOFException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ExceptionListener;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
public class ToMQ implements ExceptionListener {
/** 
 * Setting up authorisation username. Used by classes in com.ibm.mqjms.jar
 * Setting up queue connection factory (QCF)
 * Setting up name of queue, as defined in the MQ setup
 * Setting up url for file-based, external JNDI provider 
 * Setting up context factory to use with external JNDI
 */
  public String mq_username = "Administrator";
  public String mq_qcf = "TOPSECURITY_QCF";
  public String mq_qname = "TOPSECURITY.QUEUE";
  public String mq_url = "file:/C:/jms-jndi-directory";
  public String mq_jndi =  "com.sun.jndi.fscontext.RefFSContextFactory";
/**
 * main method - binding to the Websphere MQ installation - and delivering a
 * number of messages.
 *
 * Make sure you've run the MQSC commands on MQ before deploying examples
 *
 * define qcf(TOPSECURITY_QCF) qmgr(BEAQM) channel(SYSTEM.DEF.SVRCONN) hostname(semp20) port(1421) transport(CLIENT)
 * define q(TOPSECURITY.QUEUE) queue(TOPSECURITY.QUEUE) qmgr(BEAQM)
 */
 
  public static void main(String[] args) {
    ToMQ sender = null;
    try {
      sender = new ToMQ();
      sender.setupQueueConnection();
      for(int i=0;;i++) {
        sender.send(sender.setMessage("Hello no "+i+" from client at "+new Date()));
        Thread.sleep(1000L);
      }
    }
    catch(JMSException je) {
      System.out.println("Caught JMSException: "+je);
      Exception le = je.getLinkedException();
      if (le != null) 
        System.out.println("Linked exception: "+le);
      je.printStackTrace();
    } catch(Exception e) {
       e.printStackTrace();
     } finally {
      try {
      if (sender != null)
        sender.cleanup();
      } catch (Exception e) { }
    }
  }
  private void setupQueueConnection() throws Exception {
    //set the username which to use for authentication
    System.out.println("Initial user.name="+System.getProperty("user.name"));
    System.setProperty("user.name",mq_username);
    System.out.println("Authrorisation required user.name="+System.getProperty("user.name"));
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, mq_jndi);
    env.put(Context.PROVIDER_URL, mq_url);
    InitialDirContext ctx = new InitialDirContext(env);
    com.ibm.mq.jms.MQQueueConnectionFactory factory =
            (com.ibm.mq.jms.MQQueueConnectionFactory)ctx.lookup(mq_qcf);
    System.out.println("Factory = " +factory);
    /* Create a QueueConnection, QueueSession
     * When a connection is made, use the createQueueSession method on the
     * QueueConnection to obtain a session. Parameters: 
     * boolean= determines whether the session is transacted or non-transacted.
     * int =  that determines the acknowledge mode.
     * Simplest case is that of the non-transacted session with AUTO_ACKNOWLEDGE
     * - p319 in the IBM redbook.
     */
    connection = factory.createQueueConnection();
    session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    ioQueue = (Queue)ctx.lookup(mq_qname);
    connection.start();
    connection.setExceptionListener(this);
    queueSender = session.createSender(ioQueue);
  }
  /**
   * For test purpose, create one of many possible types of messages: 
   * BytesMessage, MapMessage, ObjectMessage, StreamMessage
   */
  TextMessage setMessage(String text) throws Exception {
    TextMessage msg = session.createTextMessage();
    msg.setText(text);
    return(msg);
  }
  /**
   * Send message off to MQ
   */
  void send(TextMessage msg) throws Exception {
    System.out.println("To " + ioQueue.getQueueName() + " sending message=" + msg.getText());
    queueSender.send(msg);
  }
  /**
   * required in the ExceptionListener interface
   */
  public void onException(JMSException jms) {
    System.out.println("onException: jms" + jms);
    jms.printStackTrace();
  }
  /**
   * do away with connections to MQ
   */
  void cleanup() throws Exception {
    if (session != null) {
      session.close();
      session = null;
    }
    if (connection != null)
      connection.close();
      connection = null;
  }
  private Queue ioQueue;
  private QueueSession session;
  private QueueConnection connection;
  private QueueConnectionFactory factory;
  private QueueSender queueSender;
  private InitialContext ctx;
  private TextMessage msg;
}