/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 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
 *
 * Initial developer: Benoit Pelletier
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.ant;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.PatternSet.NameEntry;

import org.objectweb.jonas.ant.jonasbase.Apps;
import org.objectweb.jonas.ant.jonasbase.Archives;
import org.objectweb.jonas.ant.jonasbase.Ejbjars;
import org.objectweb.jonas.ant.jonasbase.Rars;
import org.objectweb.jonas.ant.jonasbase.Wars;

/**
 * Common constants or methods used by several classes of the package
 * @author Benoit Pelletier
 */
public class JOnASAntTool {

    /**
     * Name of JOnAS configuration file
     */
    public static final String JONAS_CONF_FILE = "jonas.properties";

    /**
     * Name of Joram configuration file
     */
    public static final String JORAM_CONF_FILE = "a3servers.xml";

    /**
     * Name of Joram admin configuration file (resource adaptor)
     */
    public static final String JORAM_ADMIN_CONF_FILE = "joramAdmin.xml";

    /**
     * Name of Carol configuration file
     */
    public static final String CAROL_CONF_FILE = "carol.properties";

    /**
     * Name of Tomcat configuration file
     */
    public static final String TOMCAT_CONF_FILE = "server.xml";

    /**
     * Name of Tomcat configuration file
     */
    public static final String JETTY_CONF_FILE = "jetty5.xml";

    /**
     * Name of P6Spy configuration file
     */
    public static final String P6SPY_CONF_FILE = "spy.properties";

    /**
     * List of Wars to copy for each JONAS_BASE
     */
    public static final String[] WARS_LIST = new String[] {"juddi.war", "autoload/jonasAdmin.war"};

    /**
     * List of Rars to copy for each JONAS_BASE
     */
    public static final String[] RARS_LIST =  new String[] {"autoload/JOnAS_jdbcCP.rar", "autoload/JOnAS_jdbcDM.rar", "autoload/JOnAS_jdbcDS.rar", "autoload/JOnAS_jdbcXA.rar"};

    /**
     * List of EjbJars to copy for each JONAS_BASE
     */
    public static final String[] EJBJARS_LIST = new String[] {""};

    /**
     * List of Apps to copy for each JONAS_BASE
     */
    public static final String[] APPS_LIST = new String[] {"autoload/mejb.ear"};

    /**
     * Private constructor for tool class
     */
    private JOnASAntTool() {

    }

    /**
     * Update the JONAS_BASE directory with ejbjars, webapps, rars, etc.
     * @param antTask target task
     * @param jonasRoot JONAS_ROOT
     * @param destDir destination directory
     **/
    public static void updateJonasBase(Task antTask, File jonasRoot, File destDir) {
        Archives wars = new Wars();
        updateJonasBaseForArchives(antTask, jonasRoot, destDir, wars, "webapps", WARS_LIST);

        Archives ejbjars = new Ejbjars();
        updateJonasBaseForArchives(antTask, jonasRoot, destDir, ejbjars, "ejbjars", EJBJARS_LIST);

        Archives rars = new Rars();
        updateJonasBaseForArchives(antTask, jonasRoot, destDir, rars, "rars", RARS_LIST);

        Archives apps = new Apps();
        updateJonasBaseForArchives(antTask, jonasRoot, destDir, apps, "apps", APPS_LIST);


    }

    /**
     * Update JONAS_BASE with given archives with lists of includes
     * @param antTask target task
     * @param jonasRoot JONAS_ROOT
     * @param destDir destination directory
     * @param archives tasks (webapps, rars, ejbjars)
     * @param folderName where to put files
     * @param listOfIncludes files to include
     */
    public static void updateJonasBaseForArchives(Task antTask, File jonasRoot, File destDir, Archives archives, String folderName, String[] listOfIncludes) {
        configure(antTask, archives);
        archives.setDestDir(destDir);
        FileSet fileSet = new FileSet();
        fileSet.setDir(new File(jonasRoot, folderName));
        for (int f = 0; f < listOfIncludes.length; f++) {
            NameEntry ne = fileSet.createInclude();
            ne.setName(listOfIncludes[f]);
        }
        archives.addFileset(fileSet);
        archives.setOverwrite(true);
        String info = archives.getLogInfo();
        if (info != null) {
            antTask.log(info, Project.MSG_INFO);
        }
        archives.execute();
    }

    /**
     * Configure the given task by setting name, project root, etc
     * @param srcTask source task
     * @param dstTask destination task
     */
    public static void configure(Task srcTask, Task dstTask) {
        dstTask.setTaskName(srcTask.getTaskName());
        dstTask.setProject(srcTask.getProject());
    }

    /**
     * Delete a file. If the file is a directory, delete recursivly all the
     * files inside.
     * @param aFile file to delete.
     */
    public static void deleteAllFiles(File aFile) {
        if (aFile.isDirectory()) {
            File someFiles[] = aFile.listFiles();
            for (int i = 0; i < someFiles.length; i++) {
                deleteAllFiles(someFiles[i]);
            }
        }
        aFile.delete();
    }

}