Subversion Repositories XServices

Rev

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

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