Subversion Repositories XServices

Rev

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

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