//
// JmsaMain.java - JMS Adapter Main class
//
// JMS Adapter can be run standalone, which is just a quick and easy way to
// determine its version, and to validate the configuration.
// One day this might also be a way of running a standalone engine which
// gets from one destination, mediates, and puts to another.
// Need to have jms.jar on the classpath to use this.
//

package nyangau.jmsadapter;

//...simports:0:
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import javax.jms.*;
//...e

public class JmsaMain
  {
//...sfatal:2:
protected static void fatal(String error)
  {
  System.err.println("JmsaMain: " + error);
  System.exit(1);
  }
//...e
//...susage:2:
protected static void usage()
  {
  System.out.println("usage: JmsaMain version");
  System.out.println("   or: JmsaMain validate configURL [propertiesURL]");
  System.out.println("   or: JmsaMain flow configURL propertiesURL flow {var=url} [url]");
  System.exit(1);
  }
//...e
//...smain:2:
//...sstringData:2:
protected static String stringData(String url)
  throws IOException
  {
  if ( url.startsWith(":") )
    return url.substring(1);
  else
    return FlowHelper.fetchURL(url);
  }
//...e

public static void main(String[] args)
  {
  if ( args.length < 1 )
    usage();
  if ( args[0].equalsIgnoreCase("version") )
    System.out.println("JMS Adapter "+Jmsa.VERSION);
  else if ( args[0].equalsIgnoreCase("validate") )
    {
    HashMap<String,Object> map = new HashMap<String,Object>();
    if ( args.length < 2 )
      usage();
    map.put(Jmsa.CONFIG, args[1]);
    if ( args.length >= 3 && ! args[2].equals("-") )
      map.put(Jmsa.PROPERTIES, args[2]);
    try
      {
      FlowProcessor fp = new FlowProcessor(map);
      }
    catch ( FlowProcessorException e )
      {
      e.printStackTrace();
      }
    }
  else if ( args[0].equalsIgnoreCase("flow") )
    {
    HashMap<String,Object> map = new HashMap<String,Object>();
    if ( args.length < 4 )
      usage();
    map.put(Jmsa.CONFIG, args[1]);
    if ( ! args[2].equals("-") )
      map.put(Jmsa.PROPERTIES, args[2]);
    try
      {
      FlowProcessor fp = new FlowProcessor(map);
      String flow = args[3];
      Flow f = fp.getFlow(flow);
      if ( f == null )
        throw new FlowProcessorException("flow "+flow+" not defined");
      FlowContext c = new FlowContext(fp, new JmsaMessage());
      for ( int i = 4; i < args.length; i++ )
        {
        int j = args[i].indexOf("=");
        if ( j != -1 )
          {
          String var = args[i].substring(0, j);
          String url = args[i].substring(j+1);
          String val = stringData(url);
          c.setVar(var, val);
          }
        else
          {
          String url = args[i];
          String val = stringData(url);
          c.setMessage(new JmsaTextMessage(val));
          }
        }
      System.out.println("=== BEFORE "+flow);
      System.out.print(FlowHelper.diagForm(c, true, true, null));
      fp.process(f, c);
      System.out.println("=== AFTER "+flow);
      System.out.print(FlowHelper.diagForm(c, true, true, null));
      }
    catch ( FlowProcessorException e )
      {
      e.printStackTrace();
      }
    catch ( JMSException e )
      {
      e.printStackTrace();
      }
    catch ( IOException e )
      {
      e.printStackTrace();
      }
    }
  else
    usage();
  }
//...e
  }
