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


package org.objectweb.jonas_lib.genclientstub.modifier;

import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;

import org.objectweb.jonas_lib.I18n;
import org.objectweb.jonas_lib.genbase.GenBaseException;
import org.objectweb.jonas_lib.genbase.archive.Application;
import org.objectweb.jonas_lib.genbase.archive.Archive;
import org.objectweb.jonas_lib.genbase.archive.Client;
import org.objectweb.jonas_lib.genbase.archive.EjbJar;
import org.objectweb.jonas_lib.genbase.archive.FileArchive;
import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
import org.objectweb.jonas_lib.genbase.archive.JarArchive;
import org.objectweb.jonas_lib.genbase.archive.WebApp;
import org.objectweb.jonas_lib.genbase.modifier.AbsModifierFactory;
import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;


/**
 * Used to create the right ArchiveModifier from a given filename (ear, jar,
 * war, ...)
 *
 * @author Guillaume Sauthier
 */
public class ModifierFactory extends AbsModifierFactory {

    /** i18n */
    private static I18n i18n = I18n.getInstance(ModifierFactory.class);

     /**
     * Empty Constructor for utility class
     */
    private ModifierFactory() {
        super();
    }

    /**
     * Returns an <code>ArchiveModifier</code> according to archive type
     * (application, ejbjar, webapp or client).
     *
     * @param filename input filename.
     *
     * @return an <code>ArchiveModifier</code>.
     *
     * @exception GenBaseException when archive creation fails.
     */
    public static ArchiveModifier getModifier(String filename) throws GenBaseException {
        Archive archive = null;
        J2EEArchive j2eeArchive = null;
        ArchiveModifier modifier = null;
        int mode;

        File file = new File(filename);

        if (file.exists()) {
            if (file.isFile()) {
                // should be a jar file
                // test jar file
                JarFile jarfile;

                try {
                    jarfile = new JarFile(file);
                } catch (IOException ioe) {
                    // not a jar file
                    String err = i18n.getMessage("ModifierFactory.getModifier.notjar", filename);
                    throw new ClientStubGenException(err);
                }

                // Create the inner Archive
                archive = new JarArchive(file);

                // now we are sure that filename is a correct jar
                if (isApplication(jarfile)) {
                    // Application J2EE Archive
                    j2eeArchive = new Application(archive);
                    mode = APPLICATION;

                } else if (isEjbJar(jarfile)) {
                    // EjbJar J2EE Archive
                    j2eeArchive = new EjbJar(archive);
                    mode = EJBJAR;

                } else if (isWebApp(jarfile)) {
                    // WebApp J2EE Archive
                    j2eeArchive = new WebApp(archive);
                    mode = WEBAPP;

                } else if (isClient(jarfile)) {
                    // Client J2EE Archive
                    j2eeArchive = new Client(archive);
                    mode = CLIENT;

                } else {
                    // unsupported archive type
                    String err = i18n.getMessage("ModifierFactory.getModifier.unsupported", filename);
                    throw new ClientStubGenException(err);
                }
            } else {
                // directory unpacked
                // Create the inner Archive
                archive = new FileArchive(file);

                // now we are sure that filename is a correct jar
                if (isApplication(file)) {
                    // Application J2EE Archive
                    j2eeArchive = new Application(archive);
                    mode = APPLICATION;

                } else if (isEjbJar(file)) {
                    // EjbJar J2EE Archive
                    j2eeArchive = new EjbJar(archive);
                    mode = EJBJAR;

                } else if (isWebApp(file)) {
                    // WebApp J2EE Archive
                    j2eeArchive = new WebApp(archive);
                    mode = WEBAPP;

                } else if (isClient(file)) {
                    // Client J2EE Archive
                    j2eeArchive = new Client(archive);
                    mode = CLIENT;

                } else {
                    // unsupported archive type
                    String err = i18n.getMessage("ModifierFactory.getModifier.unsupported", filename);
                    throw new ClientStubGenException(err);
                }
            }
        } else {
            String err = i18n.getMessage("ModifierFactory.getModifier.notfound", filename);
            throw new ClientStubGenException(err);
        }

        // init the archive
        j2eeArchive.initialize();

        //create the modifier
        switch (mode) {
            case APPLICATION:
                modifier = new ApplicationModifier((Application) j2eeArchive);

                break;

            case EJBJAR:
                modifier = new EjbJarModifier((EjbJar) j2eeArchive);

                break;

            case WEBAPP:
                modifier = new WebAppModifier((WebApp) j2eeArchive);

                break;

            case CLIENT:
                modifier = new ClientModifier((Client) j2eeArchive);

                break;

            default:
                // cannot happen
        }

        return modifier;

    }
}