Subversion Repositories XServices

Rev

Rev 65 | 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
package net.brutex.xservices.util;
17
 
11 brianR 18
import java.io.ByteArrayOutputStream;
19
import java.io.PrintStream;
12 brianR 20
import java.util.HashMap;
6 brianR 21
import java.util.Map;
12 brianR 22
import net.brutex.xservices.types.ReturnCode;
83 brianR 23
import net.brutex.xservices.types.ant.AntProperty;
24
 
6 brianR 25
import org.apache.tools.ant.BuildException;
26
import org.apache.tools.ant.Project;
27
import org.apache.tools.ant.Target;
28
import org.apache.tools.ant.Task;
11 brianR 29
import org.apache.tools.ant.listener.TimestampedLogger;
6 brianR 30
import org.apache.tools.ant.taskdefs.Echo;
31
 
32
/**
33
 *
34
 * @author Brian Rosenberger, bru@brutex.de
35
 */
36
public class RunTask {
37
 
38
    Project antproject;
39
    Target anttarget;
40
    Task anttask;
20 brianR 41
    ByteArrayOutputStream myout = new ByteArrayOutputStream();
42
    ByteArrayOutputStream myerr = new ByteArrayOutputStream();
43
    PrintStream out = new PrintStream(myout);
44
    PrintStream err = new PrintStream(myerr);
11 brianR 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();
20 brianR 53
        log.setOutputPrintStream(out);
54
        log.setErrorPrintStream(err);
18 brianR 55
        log.setMessageOutputLevel(Echo.EchoLevel.WARN.getLevel());
11 brianR 56
 
6 brianR 57
        antproject.addBuildListener(log);
12 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 {
12 brianR 70
        anttask.init();
71
        anttask.setProject(antproject);
72
        anttask.setOwningTarget(anttarget);
73
        anttarget.addTask(anttask);
74
        antproject.addOrReplaceTarget(anttarget);
6 brianR 75
    }
76
 
65 brianR 77
    /**
78
     * @return ReturnCode type {@link ReturnCode}
79
     * @throws BuildException
80
     */
81
    public ReturnCode postTask() throws BuildException {
12 brianR 82
        int returnCode = 0;
83
        Map<String, String> origMap = new HashMap<String, String>();
84
        Map<String, String> newMap = null;
85
        origMap.putAll(antproject.getProperties());
65 brianR 86
        antproject.executeTarget(anttarget.getName());
12 brianR 87
        newMap = antproject.getProperties();
54 brianR 88
        newMap.putAll(antproject.getUserProperties());
89
 
12 brianR 90
        for (Map.Entry<String, String> e : origMap.entrySet()) {
91
            newMap.remove(e.getKey());
92
        }
93
 
11 brianR 94
        //anttask.execute();
12 brianR 95
        return new ReturnCode(returnCode,
20 brianR 96
                myout.toString(),
97
                myerr.toString(),
12 brianR 98
                AntProperty.createAntPropertyList(newMap));
99
 
6 brianR 100
    }
101
}