/**
 * 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_ws.wsgen.generator;

import java.io.File;
import java.util.Enumeration;
import java.util.Vector;

import org.objectweb.common.Cmd;

import org.objectweb.jonas_lib.I18n;
import org.objectweb.jonas_lib.genbase.GenBaseException;
import org.objectweb.jonas_lib.genbase.generator.AbsGenerator;
import org.objectweb.jonas_lib.genbase.generator.Config;

import org.objectweb.jonas_ws.wsgen.WsGenException;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Generators provide a structure to be extended for specific generation
 * mecanisms. Axis will provide an AxisWsClientGenerator that will be in charge
 * of client side generation (WSDL2Java tool) and an AxisWsEndpointGenerator
 * that will bother with server side artifact generation.
 *
 * @author Guillaume Sauthier
 */
public abstract class Generator extends AbsGenerator {

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

    /**
     * Creates a new Generator with the given Config.
     *
     * @param config internal configuration object.
     *
     * @throws GenBaseException When sources and target temporary directory cannot
     *         be created
     */
    public Generator(Config config) throws GenBaseException {
        super(config);
    }

    /**
     * Generate files.
     *
     * @throws WsGenException When generation fails.
     */
    public abstract void generate() throws WsGenException;

    /**
     * Compile generated java files into classes directory.
     *
     * @throws WsGenException When compilation fails
     */
    public void compile() throws WsGenException {
        try {
            // get all java files contained in the sources dir
            Vector srcs = new Vector();
            addJavaSources(getSources(), srcs);

            Cmd cmd = new Cmd(getConfig().getJavaHomeBin() + getConfig().getNameJavac());
            cmd.addArgument("-classpath");
            cmd.addArgument(getConfig().getClasspath());
            cmd.addArgument("-d");
            cmd.addArgument(getClasses().getCanonicalPath());
            cmd.addArguments(getConfig().getJavacOpts());

            if (srcs.size() != 0) {
                for (Enumeration e = srcs.elements(); e.hasMoreElements();) {
                    String srcName = ((File) e.nextElement()).getCanonicalPath();
                    cmd.addArgument(srcName);
                }

                getLogger().log(BasicLevel.DEBUG, "Running '" + cmd.toString() + "'");
                if (cmd.run()) {
                    getLogger().log(BasicLevel.INFO, "WebServices Classes successfully compiled.");
                } else {
                    String err = getI18n().getMessage("Generator.compile.error");
                    getLogger().log(BasicLevel.ERROR, err);
                    throw new WsGenException(err);
                }
            }
        } catch (Exception e) {
            String err = getI18n().getMessage("Generator.compile.error");
            getLogger().log(BasicLevel.ERROR, err);
            throw new WsGenException(err, e);
        }
    }


    /**
     * @return the i18n.
     */
    protected static I18n getI18n() {
        return i18n;
    }
}