Subversion Repositories XServices

Rev

Details | 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;
181 brianR 22
 
12 brianR 23
import net.brutex.xservices.types.ReturnCode;
83 brianR 24
import net.brutex.xservices.types.ant.AntProperty;
25
 
6 brianR 26
import org.apache.tools.ant.BuildException;
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;
20 brianR 42
    ByteArrayOutputStream myout = new ByteArrayOutputStream();
43
    ByteArrayOutputStream myerr = new ByteArrayOutputStream();
44
    PrintStream out = new PrintStream(myout);
45
    PrintStream err = new PrintStream(myerr);
11 brianR 46
    TimestampedLogger log = null;
47
 
6 brianR 48
    public RunTask(Task anttask) {
49
 
50
        antproject = new Project();
51
        antproject.init();
52
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
11 brianR 53
        log = new TimestampedLogger();
20 brianR 54
        log.setOutputPrintStream(out);
55
        log.setErrorPrintStream(err);
18 brianR 56
        log.setMessageOutputLevel(Echo.EchoLevel.WARN.getLevel());
11 brianR 57
 
6 brianR 58
        antproject.addBuildListener(log);
12 brianR 59
 
6 brianR 60
        anttarget = new Target();
61
        anttarget.setName("XBridgeNGDynamicTarget");
62
        anttarget.setProject(antproject);
63
        antproject.addTarget(anttarget);
64
 
65
        this.anttask = anttask;
66
        prepareTask();
67
    }
68
 
69
    private void prepareTask()
70
            throws BuildException {
12 brianR 71
        anttask.init();
72
        anttask.setProject(antproject);
73
        anttask.setOwningTarget(anttarget);
74
        anttarget.addTask(anttask);
75
        antproject.addOrReplaceTarget(anttarget);
6 brianR 76
    }
77
 
65 brianR 78
    /**
79
     * @return ReturnCode type {@link ReturnCode}
80
     * @throws BuildException
81
     */
82
    public ReturnCode postTask() throws BuildException {
12 brianR 83
        int returnCode = 0;
181 brianR 84
        Map<String, Object> origMap = new HashMap<String, Object>();
85
        Map<String, Object> newMap = null;
12 brianR 86
        origMap.putAll(antproject.getProperties());
65 brianR 87
        antproject.executeTarget(anttarget.getName());
12 brianR 88
        newMap = antproject.getProperties();
54 brianR 89
        newMap.putAll(antproject.getUserProperties());
90
 
181 brianR 91
        for (Map.Entry<String, Object> e : origMap.entrySet()) {
12 brianR 92
            newMap.remove(e.getKey());
93
        }
94
 
11 brianR 95
        //anttask.execute();
12 brianR 96
        return new ReturnCode(returnCode,
20 brianR 97
                myout.toString(),
98
                myerr.toString(),
12 brianR 99
                AntProperty.createAntPropertyList(newMap));
100
 
6 brianR 101
    }
102
}