/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)SunRIJMSAdmin.java	1.9 02/01/12
 */
 
package com.sun.cts.implementation.sun;

import java.util.*;
import java.io.*;
import com.sun.cts.util.*;
import com.sun.cts.porting.*;

/**
 * This is an implementation of the CTSJMSAdminInterface.  An implementation 
 * of this class must be supplied by any J2EE implementation wishing to
 * have JavaTest (the harness which drives the CTS tests) automatically create 
 * and remove JMS queues and topics.  This particular implementation uses the 
 * create/remove semantics of Sun's J2EE reference implementation.
 *  
 * @author	Kyle Grucci
 */	
public class SunRIJMSAdmin implements CTSJMSAdminInterface 
{
	PrintWriter log = null;
	
	public void init(PrintWriter writer)
	{
		log = writer;
	}
	
	/**
	 * This method creates queues in a J2EE implementation.
	 *
	 * @param queues    queues to remove
	 * 
	 */
	public void createQueues(String[] queues) throws CTSJMSAdminException
	{
		int ii = 0;
		
		try
		{
			for ( ii=0; ii < queues.length; ii++)
			{
				createQueue(queues[ii]);
				log.println("Created JMS queue, queues[" + ii + " = " +
							queues[ii]);  
			}
	    }
		catch (Exception e)
		{
			log.println("Failed to create JMS queue, queues[" + ii + " = " +
							queues[ii]);
			throw new CTSJMSAdminException("Failed to create JMS queue, queues[" + ii + " = " + queues[ii], e);
		}
	}
	
	/**
	 * This method removes topics from a J2EE implementation.
	 *
	 * @param topics    topics to remove
	 * 
	 */
	public void createTopics(String[] topics) throws CTSJMSAdminException
	{
		int ii = 0;
		
		try
		{
			for ( ii=0; ii < topics.length; ii++)
			{
				createTopic(topics[ii]);
				log.println("Created JMS topic, topics[" + ii + " = " +
							topics[ii]);  
			}
	    }
		catch (Exception e)
		{
			log.println("Failed to create JMS topic, topics[" + ii + " = " +
							topics[ii]);
			throw new CTSJMSAdminException("Failed to create JMS topic, topics[" + ii + " = " + topics[ii], e);
		}
	}
	
	/**
	 * This method removes queues from a J2EE implementation.
	 *
	 * @param queues    queues to remove
	 * 
	 */
	public void removeQueues(String[] queues) throws CTSJMSAdminException
	{
		int ii = 0;
		
		try
		{
			for ( ii=0; ii < queues.length; ii++)
			{
				deleteDestination(queues[ii]);
				log.println("Removed JMS queue, queues[" + ii + " = " +
							queues[ii]);
			}
		}
		catch (Exception e)
		{
			log.println("Failed to delete JMS queue, queues[" + ii + " = " +
							queues[ii]);
			throw new CTSJMSAdminException("Failed to delete JMS queue, queues[" + ii + " = " + queues[ii], e);
		}
	}
	
	/**
	 * This method removes topics from a J2EE implementation.
	 *
	 * @param topics    topics to remove
	 * 
	 */
	public void removeTopics(String[] topics) throws CTSJMSAdminException
	{
		int ii = 0;
		
		try
		{
			for ( ii=0; ii < topics.length; ii++)
			{
				deleteDestination(topics[ii]);
				log.println("Removed JMS topic, topics[" + ii + " = " +
							topics[ii]);
			}
		}
		catch (Exception e)
		{
			log.println("Failed to delete JMS topic, topics[" + ii + " = " +
							topics[ii]);
			throw new CTSJMSAdminException("Failed to delete JMS topic, topics[" + ii + " = " + topics[ii], e);
		}
	}
	
	/*
	 * Uses adminTool to create a Queue Destination object
	 *
	 * @param q    JNDI name of the Queue Destination object
	 * 
	 */
 	private void createQueue(String q) throws Exception
	{
		try
		{
			String args[] = new String[3];
			args[0] = "-addJmsDestination";
			args[1] = q;		// name of queue to create
			args[2] = "queue";	// Destination is a queue
			com.sun.enterprise.tools.admin.AdminTool.main(args);
			TestUtil.logHarnessDebug("RIcreateQueue:  finished");
		}
		catch (Exception e)
		{
			TestUtil.logHarness("RIcreateQueue:  threw exception");
			e.printStackTrace();
		}
	}
	
	/*
	 * Uses adminTool to create a Topic Destination object
	 *
	 * @param q    JNDI name of the Topic Destination object
	 * 
	 */
 	private void createTopic(String topic) throws Exception
	{
		try
		{
			String args[] = new String[3];
			args[0] = "-addJmsDestination";
			args[1] = topic;		// name of topic to create
			args[2] = "topic";		// Destination is a topic
			com.sun.enterprise.tools.admin.AdminTool.main(args);
			TestUtil.logHarnessDebug("RIcreateTopic:  finished");
		}
		catch (Exception e)
		{
			TestUtil.logHarness("RIcreateTopic:  threw exeception");
			e.printStackTrace();
		}
	}
	
	private void deleteDestination(String s) throws Exception
	{
		String args[] = new String[2];
		args[0] = "-removejmsdestination";
		args[1] = s;
		com.sun.enterprise.tools.admin.AdminTool.main(args);
	}
  
//===========
  /**
   * This method creates queueConnectionFactories in a J2EE implementation.
   *
   * Arguments passed as queueConnectionFactories[n],prop[n] where n = offset and the props[n] string is equal 
   * to the needed name=value pairs.  If more than one property they should be space separated in the props string. 
   *
   * @param queueConnectionFactories    queueConnectionFactories to create
   * @param props                       properties for the connection if any
   */
  public void createQueueConnectionFactories(String[] queueConnectionFactories, String[] props) throws CTSJMSAdminException
  {
    int ii = 0;
    
    try
    {
      for ( ii=0; ii < queueConnectionFactories.length; ii++)
      {
        createQueueConnectionFactory(queueConnectionFactories[ii],props[ii]);
        log.println("Created JMS queueConnectionFactory, queueConnectionFactories[" + ii + " = " +
               queueConnectionFactories[ii]);  
      }
      }
    catch (Exception e)
    {
      log.println("Failed to create JMS queueConnectionFactory, queueConnectionFactories[" + ii + " = " +
               queueConnectionFactories[ii]);
      throw new CTSJMSAdminException("Failed to create JMS queueConnectionFactory, queueConnectionFactories[" + ii + " = " + queueConnectionFactories[ii], e);
    }
  }
  
  
  /*
   * Uses adminTool to create a QueueConnectionFactory object
   *
   * @param qConnectionFactory    JNDI name of the QueueConnectionFactory object
   * @param props                 properties for the connection if any
   * 
   */
   private void createQueueConnectionFactory(String qConnectionFactory, String props) throws Exception
  {
    try
    {
      String args[] = new String[3];
	  
	  if(props != null && !props.equals(""))
	  {
		args = new String[5];
		args[3] = "-props";
		args[4] = props;	// properties if any, ie clientId
	  }
	  
      args[0] = "-addJmsFactory";
      args[1] = qConnectionFactory;    // name of connection factory
      args[2] = "queue";        // queue connection factory
      
	  if(args.length == 3)
	  	TestUtil.logHarnessDebug("In SunRIJMSAdmin: createQueueConnectionFactory: calling with args: " + args[0] + " " + args[1] + " " + args[2]);
	  else
	  	TestUtil.logHarnessDebug("In SunRIJMSAdmin: createQueueConnectionFactory: calling with args: " + args[0] + " " + args[1] + " " + args[2] + " " + args[3]+ " " + args[4]);
	  com.sun.enterprise.tools.admin.AdminTool.main(args);
      TestUtil.logHarnessDebug("RIcreateQueueConnectionFactory:  finished");
    }
    catch (Exception e)
    {
      TestUtil.logHarness("RIcreateQueueConnectionFactory:  threw exception");
      e.printStackTrace();
    }
  }
  
 //===========
  /**
   * This method creates topicConnectionFactories in a J2EE implementation.
   *
   * Arguments passed as topicConnectionFactories[n],prop[n] where n = offset and the props[n] string is equal
   * to the needed name=value pairs.  If more than one property they should be space separated in the props string.
   *
   * @param topicConnectionFactories    topic connection  to create
   * @param props                properties if any
   */
  public void createTopicConnectionFactories(String[] topicConnectionFactories, String[] props) throws CTSJMSAdminException
  {
    int ii = 0;
    
    try
    {
      for ( ii=0; ii < topicConnectionFactories.length; ii++)
      {
        createTopicConnectionFactory(topicConnectionFactories[ii],props[ii]);
        log.println("Created JMS topicConnectionFactory, topicConnectionFactories[" + ii + " = " +
              topicConnectionFactories[ii]);  
      }
      }
    catch (Exception e)
    {
      log.println("Failed to create JMS topicConnectionFactory, topicConnectionFactories[" + ii + " = " +
              topicConnectionFactories[ii]);
      throw new CTSJMSAdminException("Failed to create JMS topicConnectionFactory, topicConnectionFactories[" + ii + " = " + topicConnectionFactories[ii], e);
    }
  }
  
  
  /*
   * Uses adminTool to create a TopicConnectionFactory object
   *
   * @param tConnectionFactory    JNDI name of the TopicConnectionFactory object
   * @param props                 properties, if any 
   */
   private void createTopicConnectionFactory(String tConnectionFactory, String props) throws Exception
  {
    try
    {
      String args[] = new String[3];
	  
	  if(props != null && !props.equals(""))
	  {
		args = new String[5];
		args[3] = "-props";
		args[4] = props;	// properties if any, ie clientId
	  }
		
      args[0] = "-addJmsFactory";
      args[1] = tConnectionFactory;    // name of connection factory
      args[2] = "topic";        // topic connection factory
               
	  if(args.length == 3)
	  	TestUtil.logHarnessDebug("In SunRIJMSAdmin: createTopicConnectionFactory: calling with args: " + args[0] + " " + args[1] + " " + args[2]);
	  else
	  	TestUtil.logHarnessDebug("In SunRIJMSAdmin: createTopicConnectionFactory: calling with args: " + args[0] + " " + args[1] + " " + args[2] + " " + args[3]+ " " + args[4]);
      com.sun.enterprise.tools.admin.AdminTool.main(args);
      TestUtil.logHarnessDebug("RIcreateTopicConnectionFactory:  finished");
    }
    catch (Exception e)
    {
      TestUtil.logHarness("RIcreateTopicConnectionFactory:  threw exception");
      e.printStackTrace();
    }
  }
  
   /**
   * This method removes ConnectionFactories from a J2EE implementation.
   *
   * @param topics    JmsConnectionFactoryNames to remove
   * 
   */
  public void removeJmsConnectionFactories(String[] jmsConnectionFactoryNames) throws CTSJMSAdminException
  {
    int ii = 0;
    
    try
    {
      for ( ii=0; ii < jmsConnectionFactoryNames.length; ii++)
      {
        deleteJmsConnectionFactory(jmsConnectionFactoryNames[ii]);
        TestUtil.logHarness("Removed JMS ConnectionFactory, jmsConnectionFactoryNames[" + ii + " = " + jmsConnectionFactoryNames[ii]);
		log.println("Removed JMS ConnectionFactory, jmsConnectionFactoryNames[" + ii + " = " +
              jmsConnectionFactoryNames[ii]);
      }
    }
    catch (Exception e)
    {
      TestUtil.logHarness("Failed to delete JMS ConnectionFactory, jmsConnectionFactoryNames[" + ii + " = " +
              jmsConnectionFactoryNames[ii]);
	  log.println("Failed to delete JMS ConnectionFactory, jmsConnectionFactoryNames[" + ii + " = " +
              jmsConnectionFactoryNames[ii]);
      throw new CTSJMSAdminException("Failed to delete JMS jms ConnectionFactory, jmsConnectionFactoryNames[" + ii + " = " + jmsConnectionFactoryNames[ii], e);
    }
  }
    /**
   * This method will delete a JmsConnectory factory from a J2EE implementation.
   *
   * @param factoryName    JmsConnectionFactoryName to remove
   * 
   */

   private void deleteJmsConnectionFactory(String factoryName) throws Exception
  {
    String args[] = new String[2];
    args[0] = "-removeJmsFactory";
    args[1] = factoryName;
    com.sun.enterprise.tools.admin.AdminTool.main(args);
  }
  
}

