/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2005 Bull S.A.
 * Contact: jonas-team@objectweb.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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.web.catalina55;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.objectweb.jonas.server.LoaderManager;
import org.objectweb.jonas.jmx.CatalinaObjectName;

/**
 * @author Adriana Danes
 *
 * This is a proxy class which uses the Catalina MBeanFactory to create connectors.
 */
public class CatalinaConnectorFactory implements CatalinaConnectorFactoryMBean {
    /**
     * The reference of the MBeanServer in which are registered both
     * Catalina's MBeanFactory and JOnAS's CatalinaConnectorFactoryMBean
     */
    private MBeanServer myServer = null;

    /**
     * Number of values for creating a connector
     */
    private static final int CONNECTOR_NB_VALUES = 3;


    /**
     * Create Catalina Connector
     * @param type connector type
     * @param address connector's IP address
     * @param port connector's port number
     * @return Stringified ObjectName of the associated MBean
     */
    public String createConnector(String type, String address, int port) {
        String sObjectName = null;
        try {
            // Get Catalina MBeanFactory ObjectName
            ObjectName onFactory = CatalinaObjectName.catalinaFactory();
            // Get Catalina Server ObjectName
            ObjectName onServer = CatalinaObjectName.catalinaServer();
            // Get Services
            ObjectName[] aonServices = (ObjectName[]) myServer.getAttribute(onServer, "serviceNames");
            // Default Service : get the first one ! (see also WhereAreYou class in jonasadmin)
            String currentCatalinaServiceName = aonServices[0].getKeyProperty("serviceName");
            // Domain
            String currentCatalinaDomainName = aonServices[0].getDomain();
            // Get default Catalina Service (called Tomcat-JOnAS in default configuration file
            ObjectName onService = CatalinaObjectName.catalinaService(currentCatalinaDomainName, currentCatalinaServiceName);
            // Prepare arguments to invoke the createXxxConnector method
            Object[] values = new Object[CONNECTOR_NB_VALUES];
            values[0] = onService.toString();
            values[1] = address;
            values[2] = new Integer(port);
            String operation = null;
            if ("HTTP".equalsIgnoreCase(type)) {
                operation = "createHttpConnector"; // HTTP
            } else if ("HTTPS".equalsIgnoreCase(type)) {
                operation = "createHttpsConnector"; // HTTPS
            } else if ("AJP".equalsIgnoreCase(type)) {
                operation = "createAjpConnector"; // AJP(HTTP)
            } else {
              throw new IllegalArgumentException("Unknown type : '" + type + "'.");
            }
            // Set Catalina class loader before making the call
            ClassLoader old = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(LoaderManager.getInstance().getCatalinaLoader());
            // Call the management method createXxxConnector
            sObjectName = (String) myServer.invoke(onFactory, operation, values
                , saCreateStandardConnectorTypes);
            // Restore the class loader
            Thread.currentThread().setContextClassLoader(old);
        } catch (InstanceNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MBeanException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ReflectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sObjectName;
    }

    /**
     * Argument types for createXxxConnector management operation defined by MBeanFactory
     */
    private String[] saCreateStandardConnectorTypes = {
            "java.lang.String", "java.lang.String", "int" // port
        };
    /**
     * @return Returns the myServer.
     */
    public MBeanServer getMyServer() {
        return myServer;
    }
    /**
     * @param myServer The myServer to set.
     */
    public void setMyServer(MBeanServer myServer) {
        this.myServer = myServer;
    }
}
