Subversion Repositories XServices

Rev

Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.brutex.xservices.util;
18
 
11 brianR 19
import java.io.ByteArrayOutputStream;
20
import java.io.PrintStream;
6 brianR 21
import java.util.Iterator;
22
import java.util.Map;
23
import java.util.Vector;
24
import org.apache.tools.ant.BuildException;
25
import org.apache.tools.ant.BuildListener;
26
import org.apache.tools.ant.BuildLogger;
27
import org.apache.tools.ant.Project;
28
import org.apache.tools.ant.Target;
29
import org.apache.tools.ant.Task;
11 brianR 30
import org.apache.tools.ant.listener.TimestampedLogger;
6 brianR 31
import org.apache.tools.ant.taskdefs.Echo;
32
 
33
/**
34
 *
35
 * @author Brian Rosenberger, bru@brutex.de
36
 */
37
public class RunTask {
38
 
39
    Project antproject;
40
    Target anttarget;
41
    Task anttask;
42
 
11 brianR 43
    ByteArrayOutputStream out = new ByteArrayOutputStream();
44
    ByteArrayOutputStream err = new ByteArrayOutputStream();
45
    TimestampedLogger log = null;
46
 
6 brianR 47
    public RunTask(Task anttask) {
48
 
49
        antproject = new Project();
50
        antproject.init();
51
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
11 brianR 52
        log = new TimestampedLogger();
53
        log.setOutputPrintStream(new PrintStream(out));
54
        log.setErrorPrintStream(new PrintStream(err));
55
        log.setMessageOutputLevel(Echo.EchoLevel.VERBOSE.getLevel());
56
 
6 brianR 57
        antproject.addBuildListener(log);
11 brianR 58
 
6 brianR 59
        anttarget = new Target();
60
        anttarget.setName("XBridgeNGDynamicTarget");
61
        anttarget.setProject(antproject);
62
        antproject.addTarget(anttarget);
63
 
64
        this.anttask = anttask;
65
        prepareTask();
66
    }
67
 
68
    private void prepareTask()
69
            throws BuildException {
70
            anttask.init();
71
            anttask.setProject(antproject);
72
            anttask.setOwningTarget(anttarget);
73
            anttarget.addTask(anttask);
74
            antproject.addOrReplaceTarget(anttarget);
75
    }
76
 
77
    public Map<String, String> postTask()
78
            throws BuildException
79
        {
80
        try {
81
        antproject.executeTarget(anttarget.getName());
82
        } catch (Exception ex) {
11 brianR 83
            new PrintStream(err).println(ex.getMessage());
6 brianR 84
        }
11 brianR 85
        Map<String, String> map = antproject.getProperties();
86
        map.put("System.stdOut", out.toString());
87
        map.put("System.stdErr", err.toString());
88
        //anttask.execute();
89
        return map;
90
 
6 brianR 91
    }
92
}