/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2003-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.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.objectweb.jonas.server.LoaderManager;

/**
 * @author Adriana Danes
 *
 * This is a proxy class which uses the Catalina WebModule MBean to
 * invoke start/stop/reload operations.
 */
public class WebModuleProxy implements WebModuleProxyMBean {
    /**
     * The reference of the MBeanServer in which are registered both
     * Catalina's WebModule and JOnAS's WebModuleProxy MBeans
     */
    private MBeanServer myServer = null;

    /**
     * Start Web module
     * @param webModuleName WebModule MBean's ObjectName
     */
    public void start(String webModuleName) {
        try {
            ObjectName onWebModule = ObjectName.getInstance(webModuleName);
            // Set Catalina class loader before making the invoke
            ClassLoader old = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(LoaderManager.getInstance().getCatalinaLoader());
            myServer.invoke(onWebModule, "start", null, null);
            // Restore the class loader
            Thread.currentThread().setContextClassLoader(old);
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 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();
        }
    }

    /**
     * Stop Web module
     * @param webModuleName WebModule MBean's ObjectName
     */
    public void stop(String webModuleName) {
        try {
            ObjectName onWebModule = ObjectName.getInstance(webModuleName);
            // Set Catalina class loader before making the invoke
            ClassLoader old = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(LoaderManager.getInstance().getCatalinaLoader());
            myServer.invoke(onWebModule, "stop", null, null);
            // Restore the class loader
            Thread.currentThread().setContextClassLoader(old);
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 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();
        }
    }

    /**
     * Reload Web module
     * @param webModuleName WebModule MBean's ObjectName
     */
    public void reload(String webModuleName) {
        try {
            ObjectName onWebModule = ObjectName.getInstance(webModuleName);
            // Set Catalina class loader before making the invoke
            ClassLoader old = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(LoaderManager.getInstance().getCatalinaLoader());
            myServer.invoke(onWebModule, "reload", null, null);
            // Restore the class loader
            Thread.currentThread().setContextClassLoader(old);
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 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();
        }
    }
    /**
     * @param myServer The myServer to set.
     */
    protected void setMyServer(MBeanServer myServer) {
        this.myServer = myServer;
    }
}
