Subversion Repositories XServices

Rev

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.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.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Echo;

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

    Project antproject;
    Target anttarget;
    Task anttask;

    public RunTask(Task anttask) {

        antproject = new Project();
        antproject.init();
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
        DefaultLogger log = new DefaultLogger();
        log.setErrorPrintStream(System.err);
        log.setOutputPrintStream(System.out);
        antproject.addBuildListener(log);
        Vector listeners = antproject.getBuildListeners();
        for (Iterator i = listeners.iterator(); i.hasNext(); ) {
            BuildListener listener = (BuildListener) i.next();

            if (listener instanceof BuildLogger) {
                BuildLogger logger = (BuildLogger) listener;
                logger.setMessageOutputLevel(Echo.EchoLevel.VERBOSE.getLevel());
                logger.setOutputPrintStream(System.out);
                logger.setErrorPrintStream(System.err);
            }
        }

        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());
        //anttask.execute();
        return antproject.getProperties();
        } catch (Exception ex) {
            throw new BuildException(ex);
        }
    }
}