Subversion Repositories XServices

Rev

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

Rev 6 Rev 11
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
 
16
 
17
package net.brutex.xservices.util;
17
package net.brutex.xservices.util;
-
 
18
 
-
 
19
import java.io.ByteArrayOutputStream;
18
 
20
import java.io.PrintStream;
19
import java.util.Iterator;
21
import java.util.Iterator;
20
import java.util.Map;
22
import java.util.Map;
21
import java.util.Vector;
23
import java.util.Vector;
22
import org.apache.tools.ant.BuildException;
24
import org.apache.tools.ant.BuildException;
23
import org.apache.tools.ant.BuildListener;
25
import org.apache.tools.ant.BuildListener;
24
import org.apache.tools.ant.BuildLogger;
26
import org.apache.tools.ant.BuildLogger;
25
import org.apache.tools.ant.DefaultLogger;
-
 
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;
-
 
30
import org.apache.tools.ant.listener.TimestampedLogger;
29
import org.apache.tools.ant.taskdefs.Echo;
31
import org.apache.tools.ant.taskdefs.Echo;
30
 
32
 
31
/**
33
/**
32
 *
34
 *
33
 * @author Brian Rosenberger, bru@brutex.de
35
 * @author Brian Rosenberger, bru@brutex.de
34
 */
36
 */
35
public class RunTask {
37
public class RunTask {
36
 
38
 
37
    Project antproject;
39
    Project antproject;
38
    Target anttarget;
40
    Target anttarget;
39
    Task anttask;
41
    Task anttask;
-
 
42
 
-
 
43
    ByteArrayOutputStream out = new ByteArrayOutputStream();
-
 
44
    ByteArrayOutputStream err = new ByteArrayOutputStream();
-
 
45
    TimestampedLogger log = null;
40
 
46
 
41
    public RunTask(Task anttask) {
47
    public RunTask(Task anttask) {
42
 
48
 
43
        antproject = new Project();
49
        antproject = new Project();
44
        antproject.init();
50
        antproject.init();
45
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
51
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
46
        DefaultLogger log = new DefaultLogger();
52
        log = new TimestampedLogger();
47
        log.setErrorPrintStream(System.err);
53
        log.setOutputPrintStream(new PrintStream(out));
48
        log.setOutputPrintStream(System.out);
54
        log.setErrorPrintStream(new PrintStream(err));
49
        antproject.addBuildListener(log);
-
 
50
        Vector listeners = antproject.getBuildListeners();
-
 
51
        for (Iterator i = listeners.iterator(); i.hasNext(); ) {
-
 
52
            BuildListener listener = (BuildListener) i.next();
-
 
53
 
-
 
54
            if (listener instanceof BuildLogger) {
-
 
55
                BuildLogger logger = (BuildLogger) listener;
-
 
56
                logger.setMessageOutputLevel(Echo.EchoLevel.VERBOSE.getLevel());
55
        log.setMessageOutputLevel(Echo.EchoLevel.VERBOSE.getLevel());
57
                logger.setOutputPrintStream(System.out);
-
 
58
                logger.setErrorPrintStream(System.err);
-
 
59
            }
-
 
60
        }
-
 
-
 
56
 
-
 
57
        antproject.addBuildListener(log);
61
 
58
        
62
        anttarget = new Target();
59
        anttarget = new Target();
63
        anttarget.setName("XBridgeNGDynamicTarget");
60
        anttarget.setName("XBridgeNGDynamicTarget");
64
        anttarget.setProject(antproject);
61
        anttarget.setProject(antproject);
65
        antproject.addTarget(anttarget);
62
        antproject.addTarget(anttarget);
66
 
63
 
67
        this.anttask = anttask;
64
        this.anttask = anttask;
68
        prepareTask();
65
        prepareTask();
69
    }
66
    }
70
 
67
 
71
    private void prepareTask()
68
    private void prepareTask()
72
            throws BuildException {
69
            throws BuildException {
73
            anttask.init();
70
            anttask.init();
74
            anttask.setProject(antproject);
71
            anttask.setProject(antproject);
75
            anttask.setOwningTarget(anttarget);
72
            anttask.setOwningTarget(anttarget);
76
            anttarget.addTask(anttask);
73
            anttarget.addTask(anttask);
77
            antproject.addOrReplaceTarget(anttarget);
74
            antproject.addOrReplaceTarget(anttarget);
78
    }
75
    }
79
 
76
 
80
    public Map<String, String> postTask()
77
    public Map<String, String> postTask()
81
            throws BuildException
78
            throws BuildException
82
        {
79
        {
83
        try {
80
        try {
84
        antproject.executeTarget(anttarget.getName());
81
        antproject.executeTarget(anttarget.getName());
85
        //anttask.execute();
-
 
86
        return antproject.getProperties();
-
 
87
        } catch (Exception ex) {
82
        } catch (Exception ex) {
88
            throw new BuildException(ex);
83
            new PrintStream(err).println(ex.getMessage());
89
        }
84
        }
-
 
85
        Map<String, String> map = antproject.getProperties();
-
 
86
        map.put("System.stdOut", out.toString());
-
 
87
        map.put("System.stdErr", err.toString());
-
 
88
        //anttask.execute();
-
 
89
        return map;
-
 
90
        
90
    }
91
    }
91
}
92
}