/**
 * JOnAS : Java(TM) OpenSource 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas_lib.genbase.archive;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.objectweb.jonas_lib.files.FileUtils;
import org.objectweb.jonas_lib.files.FileUtilsException;
import org.objectweb.jonas_lib.genbase.GenBaseException;
import org.objectweb.jonas_lib.genbase.utils.TempRepository;


/**
 * DummyWebApp is a wrapper for auto generated webapp archive.
 *
 * @author Guillaume Sauthier
 */
public class DummyWebApp extends WebApp {

    /** webapp name */
    private String name;

    /**
     * Creates a new DummyWebApp archive.
     *
     * @param app the file containing the webapp archive.
     * @param name webapp filename
     *
     * @throws GenBaseException when archive creation fails.
     */
    public DummyWebApp(Application app, String name) throws GenBaseException {
        super(createFileArchive(), app);
        this.name = name;
    }

    /**
     * Create a new FileArchive
     *
     * @return a new Archive
     *
     * @throws GenBaseException When Archive creation fails.
     */
    private static Archive createFileArchive() throws GenBaseException {
        TempRepository tr = TempRepository.getInstance();

        try {
            File tmp = tr.createDir();
            File meta = new File(tmp, "META-INF");
            meta.mkdirs();

            File web = new File(tmp, "WEB-INF");
            web.mkdirs();

            File manifest = new File(meta, "MANIFEST.MF");

            Manifest mf = new Manifest();
            mf.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");

            OutputStream os = new FileOutputStream(manifest);
            mf.write(os);
            os.close();

            String jonasRoot = System.getProperty("install.root");
            File webTemplate = new File(new File(jonasRoot), "templates" + File.separator + "wsgen" + File.separator
                    + "web.xml");
            FileUtils.copyFile(webTemplate, web);

            //Load context.xml, used by Tomcat to determine security realm to use (if any)
            File contextTemplate = new File(new File(jonasRoot), "templates"
                  + File.separator + "wsgen" + File.separator + "context.xml");
            FileUtils.copyFile(contextTemplate, meta);

            //Load web-jetty.xml, used by Jetty to determine security realm to use (if any)
            File webjettyTemplate = new File(new File(jonasRoot), "templates"
                  + File.separator + "wsgen" + File.separator + "web-jetty.xml");
            FileUtils.copyFile(webjettyTemplate, web);

            return new FileArchive(tmp);
        } catch (IOException ioe) {
            throw new GenBaseException(ioe);
        } catch (FileUtilsException fue) {
            throw new GenBaseException(fue);
        }
    }

    /**
     * @return Returns the DummyApplication filename.
     */
    public String getName() {
        return name;
    }

}