/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 Bull S.A.S.
 * 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.web.base;

import java.io.File;
import java.net.URL;
import java.rmi.RemoteException;

import org.ow2.jonas.web.JWebContainerService;
import org.ow2.jonas.web.JWebContainerServiceException;
import org.ow2.util.ee.deploy.api.deployable.IDeployable;
import org.ow2.util.ee.deploy.api.deployable.WARDeployable;
import org.ow2.util.ee.deploy.api.deployer.DeployerException;
import org.ow2.util.ee.deploy.api.deployer.IDeployer;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;
import org.ow2.util.url.URLUtils;

/**
 * Deployer of the WAR deployable.
 * @author Florent BENOIT
 */
public class WARDeployer implements IDeployer {

    /**
     * Logger.
     */
    private Log logger = LogFactory.getLog(WARDeployer.class);

    /**
     * Web container service used by this deployer.
     */
    private JWebContainerService webContainerService = null;

    /**
     * Undeploy the given WAR.
     * @param warDeployable the deployable to remove.
     * @throws DeployerException if the WAR is not undeployed.
     */
    protected void undeployWAR(final WARDeployable warDeployable) throws DeployerException {
        logger.info("Undeploying {0}", warDeployable);

        // Undeploy the WAR file
        try {
            webContainerService.unRegisterWar(getFile(warDeployable).getAbsolutePath());
        } catch (JWebContainerServiceException e) {
            throw new DeployerException("Cannot deploy the War deployable '"  + warDeployable + "'.", e);
        } catch (RemoteException e) {
            throw new DeployerException("Cannot deploy the War deployable '"  + warDeployable + "'.", e);
        }


    }


    /**
     * Deploy the given WAR.
     * @param warDeployable the deployable to add.
     * @throws DeployerException if the WAR is not deployed.
     */
    protected void deployWAR(final WARDeployable warDeployable) throws DeployerException {
        logger.info("Deploying {0}", warDeployable);

        // Deploy
        try {
            webContainerService.registerWar(getFile(warDeployable).getAbsolutePath());
        } catch (JWebContainerServiceException e) {
            throw new DeployerException("Cannot deploy the War deployable '"  + warDeployable + "'.", e);
        } catch (RemoteException e) {
            throw new DeployerException("Cannot deploy the War deployable '"  + warDeployable + "'.", e);
        }
    }



    /**
     * Deploy a WAR deployable.
     * @param deployable a given deployable
     * @throws DeployerException if the deployment is not done.
     */
    public void deploy(final IDeployable<?> deployable) throws DeployerException {
        check(deployable);

        // Deploy the WAR Deployable
        if (deployable instanceof WARDeployable) {
            deployWAR((WARDeployable) deployable);
        }
    }

    /**
     * Checks if the given deployable is deployed or not.
     * @param deployable test if a given deployable is already deployed.
     * @return true if the deployable is deployed.
     * @throws DeployerException if the undeploy operation fails.
     */
    public boolean isDeployed(final IDeployable<?> deployable) throws DeployerException {
        check(deployable);

        // Check if war has been deployed
        return webContainerService.isWarLoaded(getFile(deployable).getAbsolutePath());
    }


    /**
     * Allows to get a File from the given Deployable.
     * @param deployable the given deployable.
     * @return a File object of this deployable
     * @throws DeployerException if the File can't be obtained.
     */
    protected File getFile(final IDeployable<?> deployable) throws DeployerException {
        // Get URL
        URL url = null;
        try {
            url = deployable.getArchive().getURL().toURI().toURL();
        } catch (Exception e) {
            throw new DeployerException("Cannot get URL from deployable '" + deployable + "'.", e);
        }

        // Get File
        return URLUtils.urlToFile(url);
    }


    /**
     * Checks if the given deployable is supported by the Deployer.
     * @param deployable the deployable to be checked
     * @return true if it is supported, else false.
     */
    public boolean supports(final IDeployable<?> deployable) {
        return (deployable instanceof WARDeployable);
    }

    /**
     * Undeploy the given RAR deployable.
     * @param deployable a given deployable to undeploy
     * @throws DeployerException if the undeploy operation fails.
     */
    public void undeploy(final IDeployable<?> deployable) throws DeployerException {
        check(deployable);

        // Undeploy the WAR Deployable
        if (deployable instanceof WARDeployable) {
            undeployWAR((WARDeployable) deployable);
        }
    }

    /**
     * Check if the given deployable is deployable or not.
     * @param deployable the deployable to check.
     * @throws DeployerException if the deployable is not supported.
     */
    private void check(final IDeployable<?> deployable) throws DeployerException {
        if (!supports(deployable)) {
            throw new DeployerException("The deployment of the deployable'" + deployable
                    + "' is not supported by this deployer.");
        }
    }

    /**
     * Sets the WEB container service.
     * @param webContainerService the web container service.
     */
    public void setWebContainerService(final JWebContainerService webContainerService) {
        this.webContainerService = webContainerService;
    }


}
