/*
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.brutex.xservices.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.types.ant.AntProperty;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.listener.TimestampedLogger;
import org.apache.tools.ant.taskdefs.Echo;

/**
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
public class RunTask {

    Project antproject;
    Target anttarget;
    Task anttask;
    ByteArrayOutputStream myout = new ByteArrayOutputStream();
    ByteArrayOutputStream myerr = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(myout);
    PrintStream err = new PrintStream(myerr);
    TimestampedLogger log = null;

    public RunTask(Task anttask) {

        antproject = new Project();
        antproject.init();
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
        log = new TimestampedLogger();
        log.setOutputPrintStream(out);
        log.setErrorPrintStream(err);
        log.setMessageOutputLevel(Echo.EchoLevel.WARN.getLevel());

        antproject.addBuildListener(log);

        anttarget = new Target();
        anttarget.setName("XBridgeNGDynamicTarget");
        anttarget.setProject(antproject);
        antproject.addTarget(anttarget);

        this.anttask = anttask;
        prepareTask();
    }

    private void prepareTask()
            throws BuildException {
        anttask.init();
        anttask.setProject(antproject);
        anttask.setOwningTarget(anttarget);
        anttarget.addTask(anttask);
        antproject.addOrReplaceTarget(anttarget);
    }

    /**
     * @return ReturnCode type {@link ReturnCode}
     * @throws BuildException
     */
    public ReturnCode postTask() throws BuildException {
        int returnCode = 0;
        Map<String, Object> origMap = new HashMap<String, Object>();
        Map<String, Object> newMap = null;
        origMap.putAll(antproject.getProperties());
        antproject.executeTarget(anttarget.getName());
        newMap = antproject.getProperties();
        newMap.putAll(antproject.getUserProperties());
        
        for (Map.Entry<String, Object> e : origMap.entrySet()) {
            newMap.remove(e.getKey());
        }

        //anttask.execute();
        return new ReturnCode(returnCode,
                myout.toString(),
                myerr.toString(),
                AntProperty.createAntPropertyList(newMap));

    }
}
