/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2009 Bull S.A.S
 * Contact: jonas-team@ow2.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 1any 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.ow2.jonas.cluster.test.common;

import java.io.File;
import java.lang.reflect.Method;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

public class ClientTask implements RunnableTask {
    /**
     * Logger.
     */
    protected static final Log LOGGER = LogFactory.getLog(ClientTask.class);
    /**
     * Any exception thrown on task execution.
     */
    protected Exception exception = null;
    /**
     * Name of the task.
     */
    protected String name = null;
    /**
     * The class to run
     */
    private String class2Run = null;
    /**
     * True if the task is terminated
     */
    protected boolean terminated = false;

    /**
     * Directory where the test outputs will be printed.
     */
    String outputDir = null;

    /**
     * Directory where the test outputs will be printed.
     */
    String outputFile = null;
    /**
     * return exception thrown on execution.
     */
    public Exception getException() {
        return exception;
    }
    /**
     * default constructor.
     */
    public ClientTask () {

    }

    /**
     * Constructor using fields.
     */
    public ClientTask(final String name, final String class2run, final String outputDir, final String outputFileName) {
        this.name = name;
        this.class2Run = class2run;
        this.outputDir = outputDir;
        this.outputFile = outputDir + File.separator + outputFileName;
    }
    /**
     * Get the name.
     */
    public String getName() {
        return name;
    }
    /**
     * Return true if the task has ended.
     */
    public boolean isTerminated() {
        return terminated;
    }

    public void setName(final String name) {
        this.name = name;

    }

    public void stop() {
        Thread.currentThread().interrupt();
    }
    /**
     * Executes this task.
     */
    public void run() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        File f = new File(outputFile);
        if(f.exists()) {
            LOGGER.info("Running {0}, deleting {1}", name, f.getAbsolutePath());
            f.delete();
        }
        Class<?> clazz;
        try {
            //create the output.
            f.createNewFile();
            LOGGER.info("Running {0}, creating {1}", name, f.getAbsolutePath());
            clazz = loader.loadClass(class2Run);
            String[] newArgs = new String[]{"-clientName", name, "-out", outputFile,
                                            };
            LOGGER.info("Running {0}, invoking {1} with args: {2}, {3} {4} {5} ", name, class2Run,
                    newArgs[0], newArgs[1], newArgs[2], newArgs[3]);
            Class<?>[] argList = new Class[] {newArgs.getClass()};
            Method meth = clazz.getMethod("main", argList);
            meth.invoke(null, new Object[] {newArgs});
        } catch (Exception e) {
            //Consider the task is terminated.
            terminated = true;
            exception = e;
        }
        LOGGER.info("Running {0}, {1} terminated.", name, class2Run);
        terminated = true;
    }

}
