Subversion Repositories XServices

Rev

Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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