//
// App.java - The application, speaking in application specific messages
//
// Send an "application specific deal" message in MapMessage form to appdealq
// Receive an "application specific confirmation" TextMessage from appconfirmq
//

import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.jms.*;

public class App
  {
//...sreadFile:2:
public static String readFile(String fn)
  throws IOException
  {
  StringBuffer sb = new StringBuffer();
  BufferedReader br = new BufferedReader(new FileReader(fn));
  String line;
  while ( (line = br.readLine()) != null )
    {
    sb.append(line);
    sb.append("\n");
    }
  br.close();
  return sb.toString();
  }
//...e
  public static void main(String[] args)
    {
    try
      {
      // The JNDI we're going to store fetch JMS AOs from
      InitialContext ic = new InitialContext(
        new Hashtable<String,Object>()
          {{
          // We're going to use the cheap and cheerful filesystem based JNDI
          put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
          put(Context.PROVIDER_URL, "file:fs");
/*
          // Other vendor unique parameters used to talk to JNDI,
          // such as LDAP specific or SSL specific ones, would be added here
          put(...);
*/        
          }}
        );

      QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup("qcf");
      QueueConnection qc = qcf.createQueueConnection("admin", "password");
      QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      qc.start();

      javax.jms.Queue qDeal = (javax.jms.Queue) ic.lookup("appdealq");
      QueueSender s = qs.createSender(qDeal);
      String textDeal = readFile("AppDeal.xml");
      System.out.println("=== SENDING APPLICATION SPECIFIC DEAL\n"+textDeal);
      MapMessage mDeal = qs.createMapMessage();
      mDeal.setString("appNode", textDeal);
      s.send(mDeal);

      javax.jms.Queue qConfirm = (javax.jms.Queue) ic.lookup("appconfirmq");
      QueueReceiver r = qs.createReceiver(qConfirm);
      TextMessage mConfirm = (TextMessage) r.receive();
      String textConfirm = mConfirm.getText();
      System.out.println("=== RECEIVED APPLICATION SPECIFIC CONFIRMATION\n"+textConfirm);

      qc.close();
      }  
    catch ( JMSException e )
      {
      e.printStackTrace();
      if ( e.getLinkedException() != null )
        {
        System.err.println("Linked Exception:");
        e.getLinkedException().printStackTrace();
        }
      }
    catch ( Exception e )
      {
      e.printStackTrace();
      }
    }
  }
