/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2003 Bull S.A.
 * Contact: jonas-team@ow2.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */
package org.ow2.jonas.lib.jms;

import java.util.Vector;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.jms.XAConnectionFactory;
import javax.jms.XAQueueConnectionFactory;
import javax.jms.XATopicConnectionFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.ow2.jonas.jms.JmsAdministration;

import org.objectweb.util.monolog.api.BasicLevel;


/**
 * WebSphere MQ administration class.
 * <p>
 * This WebSphere MQ specific class implements the
 * <code>JmsAdministration</code> interface. It supposes that administration
 * is done in parallel within WebSphere MQ (such as creating a queue, 
 * creating and binding the corresponding JMS Queue, etc...).
 *
 * @author Frederic Maistre
 */
public class JmsAdminForWSMQ implements JmsAdministration {

    private static String CONN_FACT_NAME="JCF";
    private static String QUEUE_CONN_FACT_NAME="JQCF";
    private static String TOPIC_CONN_FACT_NAME="JTCF";

    private InitialContext ictx = null;
    private Vector namelist = new Vector();

    private XAConnectionFactory xacf = null;
    private XAQueueConnectionFactory xaqcf = null;
    private XATopicConnectionFactory xatcf = null;
    private ConnectionFactory jcf = null;
    private QueueConnectionFactory jqcf = null;
    private TopicConnectionFactory jtcf = null;


    /**
     * Empty constructor.
     * <p>
     * Class created with <code>newInstance()</code>.
     */
    public JmsAdminForWSMQ() {
    }

    /**
     * Initialization method, setting the connection factories.
     * <p>
     * WebSphere MQ never runs collocated.
     *
     * @param collocated  Not taken into account.
     * @param url  Not taken into account.
     *
     * @exception Exception  If creating a connection factory fails.
     */
    public void start(boolean collocated, String url) throws Exception {

        // Creating an InitialContext.
        ictx = new InitialContext();

        // Getting the connection factories that will be used by the
        // EJB components.
        try {
            xacf = (XAConnectionFactory) ictx.lookup("wsmqXACF");
            ictx.unbind("wsmqXACF");
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ XAConnectionFactory could not be retrieved from JNDI");
        }
        try {
            xaqcf = (XAQueueConnectionFactory) ictx.lookup("wsmqXAQCF");
            ictx.unbind("wsmqXAQCF");
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ XAQueueConnectionFactory could not be retrieved from JNDI");
        }
        try {
            xatcf = (XATopicConnectionFactory) ictx.lookup("wsmqXATCF");
            ictx.unbind("wsmqXATCF");
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ XATopicConnectionFactory could not be retrieved from JNDI");
        }
         
        // Getting the connection factories that will be used by pure
        // JMS clients.
        try {
            ictx.lookup(CONN_FACT_NAME);
            namelist.addElement(CONN_FACT_NAME);
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ ConnectionFactory could not be retrieved from JNDI");
        }
        try {
            ictx.lookup(QUEUE_CONN_FACT_NAME);
            namelist.addElement(QUEUE_CONN_FACT_NAME);
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ QueueConnectionFactory could not be retrieved from JNDI");
        }
        try {
            ictx.lookup(TOPIC_CONN_FACT_NAME);
            namelist.addElement(TOPIC_CONN_FACT_NAME);
        } catch (NamingException exc) {
            TraceJms.logger.log(BasicLevel.ERROR, "WebSphere MQ TopicConnectionFactory could not be retrieved from JNDI");
        }
    }

    /**
     * Terminates the JMS Service.
     */
    public void stop() {
        // Cleaning up JNDI.
        try {		    
            ictx.unbind(CONN_FACT_NAME);
        } catch(Exception ex) {
        }
        try {		    
            ictx.unbind(QUEUE_CONN_FACT_NAME);
        } catch(Exception ex) {
        }
        try {		    
            ictx.unbind(TOPIC_CONN_FACT_NAME);
        } catch(Exception ex) {
        }

        if (TraceJms.isDebug())
            TraceJms.logger.log(BasicLevel.DEBUG, "connection factories unbound");
    }


    /**
     * Returns the XAConnectionFactory.
     */
    public XAConnectionFactory getXAConnectionFactory() {
        return xacf;
    }

    /**
     * Returns the XATopicConnectionFactory.
     */
    public XATopicConnectionFactory getXATopicConnectionFactory() {
        return xatcf;
    }

    /**
     * Returns the XAQueueConnectionFactory.
     */
    public XAQueueConnectionFactory getXAQueueConnectionFactory() {
        return xaqcf;
    }

    /**
     * Non implemented method: creating a WebSphere MQ destination from JOnAS is not possible.
     *
     * @exception Exception  Always thrown.
     */
    public Queue createQueue(String name) throws Exception {
        throw new Exception("WebSphere MQ Queue creation impossible from JOnAS");
    }

    /**
     * Non implemented method: creating a WebSphere MQ destination from JOnAS is not possible.
     *
     * @exception Exception  Always thrown.
     */
    public Topic createTopic(String name) throws Exception {
        throw new Exception("WebSphere MQ Topic creation impossible from JOnAS");
    }

    /**
     * Non implemented method: deleting a WebSphere MQ destination from JOnAS is not possible.
     *
     * @exception Exception  Always thrown.
     */
    public void deleteDestination(String name) throws Exception {
        throw new Exception("WebSphere MQ destination deletion impossible from JOnAS");
    }

    /**
     * Returns -1; did not have time to find out if getting this information
     * is possible from a WSMQ Queue.
     */
    public int getPendingMessages(javax.jms.Queue queue) throws Exception {
        return -1;
    }

    /**
     * Returns -1; did not have time to find out if getting this information
     * is possible from a WSMQ Queue.
     */
    public int getPendingRequests(javax.jms.Queue queue) throws Exception {
        return -1;
    }

    /**
     * Returns -1; did not test the WSMQ Pub/Sub broker.
     */
    public int getSubscriptions(javax.jms.Topic topic) throws Exception {
        return -1;
    }
}
