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
 
17
package net.brutex.xservices.util;
18
 
19
import java.util.Iterator;
20
import java.util.Map;
21
import java.util.Vector;
22
import org.apache.tools.ant.BuildException;
23
import org.apache.tools.ant.BuildListener;
24
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.Target;
28
import org.apache.tools.ant.Task;
29
import org.apache.tools.ant.taskdefs.Echo;
30
 
31
/**
32
 *
33
 * @author Brian Rosenberger, bru@brutex.de
34
 */
35
public class RunTask {
36
 
37
    Project antproject;
38
    Target anttarget;
39
    Task anttask;
40
 
41
    public RunTask(Task anttask) {
42
 
43
        antproject = new Project();
44
        antproject.init();
45
        antproject.setBasedir(System.getProperty("java.io.tmpdir"));
46
        DefaultLogger log = new DefaultLogger();
47
        log.setErrorPrintStream(System.err);
48
        log.setOutputPrintStream(System.out);
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());
57
                logger.setOutputPrintStream(System.out);
58
                logger.setErrorPrintStream(System.err);
59
            }
60
        }
61
 
62
        anttarget = new Target();
63
        anttarget.setName("XBridgeNGDynamicTarget");
64
        anttarget.setProject(antproject);
65
        antproject.addTarget(anttarget);
66
 
67
        this.anttask = anttask;
68
        prepareTask();
69
    }
70
 
71
    private void prepareTask()
72
            throws BuildException {
73
            anttask.init();
74
            anttask.setProject(antproject);
75
            anttask.setOwningTarget(anttarget);
76
            anttarget.addTask(anttask);
77
            antproject.addOrReplaceTarget(anttarget);
78
    }
79
 
80
    public Map<String, String> postTask()
81
            throws BuildException
82
        {
83
        try {
84
        antproject.executeTarget(anttarget.getName());
85
        //anttask.execute();
86
        return antproject.getProperties();
87
        } catch (Exception ex) {
88
            throw new BuildException(ex);
89
        }
90
    }
91
}