/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 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
 *
 * Initial developer(s): Benoit PELLETIER
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas_lib.deployment.work;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Vector;

import org.objectweb.jonas.service.ServiceManager;
import org.objectweb.jonas.web.JWebContainerService;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * JOnAS War unused copy clean task class. This class provides a way for
 * removing copies which are inconsistent for war files.
 * @author Florent BENOIT
 * @author Benoit PELLETIER
 */
public class WarCleanTask extends AbsCleanTask {

    /**
     * reference to the deployerLog
     */
    private static DeployerLog warDeployerLog = null;

    /**
     * Default constructor : Construct a new cleaner.
     * @param warDeployerLog the deployer logger
     */
    public WarCleanTask(DeployerLog warDeployerLog) {
        super();
        WarCleanTask.warDeployerLog = warDeployerLog;
    }

    /**
     * Return true if the work copy exists and is up to date
     * @param logEntry entry in a deploy log
     * @return true if the work copy exists and is up to date
     * @throws CleanerException if it fails
     */
    protected boolean isValidLogEntry(LogEntry logEntry) throws CleanerException {
        String fTimeStamp = null;
        File warOriginalFile = logEntry.getOriginal();
        File warCopyFile = logEntry.getCopy();

        if (getLogger().isLoggable(BasicLevel.DEBUG)) {
            getLogger().log(BasicLevel.DEBUG, "LogEntry exist :" + warOriginalFile.exists());
        }

        // if the file doesn't exist, return
        if (!warOriginalFile.exists()) {
            return false;
        }

        //get the timestamp
        try {
            fTimeStamp = FileManager.fileToTimeStampDir(warOriginalFile.toURL());
        } catch (FileManagerException efme) {
            throw new CleanerException("Can't get the timestamp of the file " + warOriginalFile + " : "
                    + efme.getMessage());
        } catch (MalformedURLException mue) {
            throw new CleanerException("Can't get the timestamp of the file " + warOriginalFile + " : "
                    + mue.getMessage());
        }

        if (getLogger().isLoggable(BasicLevel.DEBUG)) {
            getLogger().log(BasicLevel.DEBUG, "LogEntry fTimeStamp :" + fTimeStamp);
            getLogger().log(BasicLevel.DEBUG, "LogEntry isValid :" + fTimeStamp.equalsIgnoreCase(warCopyFile.getName()));
        }

        //compare
        return (fTimeStamp.equalsIgnoreCase(warCopyFile.getName()));

    }

    /**
     * Remove the work copy specified in the log entry and the log entry
     * @param logEntry entry in a deploy log
     * @return true if the log entry has been removed successfully
     * @throws CleanerException if it fails
     */
    protected boolean removeLogEntry(LogEntry logEntry) throws CleanerException {
        // it's a file but this should work
        if (removeRecursiveDirectory(logEntry.getCopy())) {
            try {
                warDeployerLog.removeEntry(logEntry);
                return true;
            } catch (DeployerLogException edle) {
                throw new CleanerException("Can't remove an entry" + edle.getMessage());
            }
        }
        return false;
    }

    /**
     * Gets the log entries
     * @return the log entries
     */
    protected Vector getLogEntries() {
        return warDeployerLog.getEntries();
    }

    /**
     * Check if the package pointed by the log entry is currently deploy
     * @param logEntry entry in a deploy log
     * @return true if the package pointed by the log entry is currently deployed
     * @throws CleanerException if it fails
     */
    protected boolean isDeployLogEntry(LogEntry logEntry) throws CleanerException {

        // get the web container service
        ServiceManager sm = null;

        try {
            sm = ServiceManager.getInstance();
        } catch (Exception e) {
            throw new CleanerException("Cannot get ServiceManager instance");
        }
        JWebContainerService webContainerService = (JWebContainerService) sm.getWebContainerService();

        // check if the war file is deployed
        return webContainerService.isWarDeployedByWorkName(logEntry.getCopy().getName());
        
    }
}