Subversion Repositories XServices

Rev

Rev 6 | 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.Iterator;
import java.util.Map;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.BuildLogger;
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.VERBOSE.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 Map<String, String> postTask()
            throws BuildException
        {
        try {
        antproject.executeTarget(anttarget.getName());
        } catch (Exception ex) {
            new PrintStream(err).println(ex.getMessage());
        }
        Map<String, String> map = antproject.getProperties();
        map.put("System.stdOut", out.toString());
        map.put("System.stdErr", err.toString());
        //anttask.execute();
        return map;
        
    }
}