Subversion Repositories XServices

Rev

Rev 12 | Blame | Last modification | View Log | Download | RSS feed

/*
 *   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.AntProperty;
import net.brutex.xservices.types.ReturnCode;
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 out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    TimestampedLogger log = null;

    public RunTask(Task anttask) {

        antproject = new Project();
        antproject.init();
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
        log = new TimestampedLogger();
        log.setOutputPrintStream(new PrintStream(out));
        log.setErrorPrintStream(new PrintStream(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);
    }

    public ReturnCode postTask() {
        int returnCode = 0;
        Map<String, String> origMap = new HashMap<String, String>();
        Map<String, String> newMap = null;
        origMap.putAll(antproject.getProperties());
        try {
            antproject.executeTarget(anttarget.getName());
        } catch (BuildException ex) {
            new PrintStream(err).println(ex.getMessage());
            returnCode = 1;
        }
        newMap = antproject.getProperties();

        for (Map.Entry<String, String> e : origMap.entrySet()) {
            newMap.remove(e.getKey());
        }

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

    }
}