~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/Task.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
14
 * 2. Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in
 
16
 *    the documentation and/or other materials provided with the
 
17
 *    distribution.
 
18
 *
 
19
 * 3. The end-user documentation included with the redistribution, if
 
20
 *    any, must include the following acknowlegement:
 
21
 *       "This product includes software developed by the
 
22
 *        Apache Software Foundation (http://www.apache.org/)."
 
23
 *    Alternately, this acknowlegement may appear in the software itself,
 
24
 *    if and wherever such third-party acknowlegements normally appear.
 
25
 *
 
26
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 
27
 *    Foundation" must not be used to endorse or promote products derived
 
28
 *    from this software without prior written permission. For written
 
29
 *    permission, please contact apache@apache.org.
 
30
 *
 
31
 * 5. Products derived from this software may not be called "Apache"
 
32
 *    nor may "Apache" appear in their names without prior written
 
33
 *    permission of the Apache Group.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
36
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
37
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
38
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
41
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
42
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
43
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
44
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
45
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
49
 * This software consists of voluntary contributions made by many
 
50
 * individuals on behalf of the Apache Software Foundation.  For more
 
51
 * information on the Apache Software Foundation, please see
 
52
 * <http://www.apache.org/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant;
 
56
 
 
57
/**
 
58
 * Base class for all tasks.
 
59
 *
 
60
 * <p>Use {@link Project#createTask Project.createTask} to create a new Task.
 
61
 */
 
62
 
 
63
public abstract class Task extends ProjectComponent {
 
64
 
 
65
    protected Target target = null;
 
66
    protected String description=null;
 
67
    protected Location location = Location.UNKNOWN_LOCATION;
 
68
    protected String taskName = null;
 
69
    protected String taskType = null;
 
70
    protected RuntimeConfigurable wrapper;
 
71
 
 
72
    /**
 
73
     * Sets the target object of this task.
 
74
     *
 
75
     * @param target Target in whose scope this task belongs.
 
76
     */
 
77
    public void setOwningTarget(Target target) {
 
78
        this.target = target;
 
79
    }
 
80
 
 
81
    /**
 
82
     * Get the Target to which this task belongs
 
83
     *
 
84
     * @return the task's target.
 
85
     */
 
86
    public Target getOwningTarget() {
 
87
        return target;
 
88
    }
 
89
    
 
90
    /**
 
91
     * Set the name to use in logging messages.
 
92
     *
 
93
     * @param name the name to use in logging messages.
 
94
     */
 
95
    public void setTaskName(String name) {
 
96
        this.taskName = name;
 
97
    }
 
98
 
 
99
    /**
 
100
     * Get the name to use in logging messages.
 
101
     *
 
102
     * @return the name to use in logging messages.
 
103
     */
 
104
    public String getTaskName() {
 
105
        return taskName;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Set the name with which the task has been invoked.
 
110
     *
 
111
     * @param type the name the task has been invoked as.
 
112
     */
 
113
    void setTaskType(String type) {
 
114
        this.taskType = type;
 
115
    }
 
116
 
 
117
    /** Sets a description of the current action. It will be usefull in commenting
 
118
     *  what we are doing.
 
119
     */
 
120
    public void setDescription( String desc ) {
 
121
        description=desc;
 
122
    }
 
123
 
 
124
    public String getDescription() {
 
125
        return description;
 
126
    }
 
127
 
 
128
    /**
 
129
     * Called by the project to let the task initialize properly. 
 
130
     *
 
131
     * @throws BuildException if someting goes wrong with the build
 
132
     */
 
133
    public void init() throws BuildException {}
 
134
 
 
135
    /**
 
136
     * Called by the project to let the task do it's work. This method may be 
 
137
     * called more than once, if the task is invoked more than once. For example, 
 
138
     * if target1 and target2 both depend on target3, then running 
 
139
     * "ant target1 target2" will run all tasks in target3 twice.
 
140
     *
 
141
     * @throws BuildException if someting goes wrong with the build
 
142
     */
 
143
    public void execute() throws BuildException {}
 
144
 
 
145
    /**
 
146
     * Returns the file location where this task was defined.
 
147
     */
 
148
    public Location getLocation() {
 
149
        return location;
 
150
    }
 
151
 
 
152
    /**
 
153
     * Sets the file location where this task was defined.
 
154
     */
 
155
    public void setLocation(Location location) {
 
156
        this.location = location;
 
157
    }
 
158
 
 
159
    /**
 
160
     * Returns the wrapper class for runtime configuration.
 
161
     */
 
162
    public RuntimeConfigurable getRuntimeConfigurableWrapper() {
 
163
        if (wrapper == null) {
 
164
            wrapper = new RuntimeConfigurable(this, getTaskName());
 
165
        }
 
166
        return wrapper;
 
167
    }
 
168
 
 
169
    protected void setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper) {
 
170
        this.wrapper = wrapper;
 
171
    }
 
172
 
 
173
    /**
 
174
     * Configure this task - if it hasn't been done already.
 
175
     */
 
176
    public void maybeConfigure() throws BuildException {
 
177
        if (wrapper != null) {
 
178
            wrapper.maybeConfigure(project);
 
179
        }
 
180
    }
 
181
 
 
182
    protected void handleOutput(String line) {
 
183
        log(line, Project.MSG_INFO);
 
184
    }
 
185
    
 
186
    protected void handleErrorOutput(String line) {
 
187
        log(line, Project.MSG_ERR);
 
188
    }
 
189
    
 
190
    
 
191
    /**   
 
192
     * Log a message with the default (INFO) priority.   
 
193
     *   
 
194
     * @param the message to be logged.   
 
195
     */   
 
196
    public void log(String msg) {   
 
197
        log(msg, Project.MSG_INFO);   
 
198
    }   
 
199
    
 
200
    /**   
 
201
     * Log a mesage with the give priority.   
 
202
     *   
 
203
     * @param the message to be logged.   
 
204
     * @param msgLevel the message priority at which this message is to be logged.   
 
205
     */   
 
206
    public void log(String msg, int msgLevel) {   
 
207
        project.log(this, msg, msgLevel);   
 
208
    }   
 
209
    
 
210
    /**
 
211
     * Perform this task
 
212
     */
 
213
    public final void perform() {
 
214
        try {
 
215
            project.fireTaskStarted(this);
 
216
            maybeConfigure();
 
217
            execute();
 
218
            project.fireTaskFinished(this, null);
 
219
        }
 
220
        catch(RuntimeException exc) {
 
221
            if (exc instanceof BuildException) {
 
222
                BuildException be = (BuildException) exc;
 
223
                if (be.getLocation() == Location.UNKNOWN_LOCATION) {
 
224
                    be.setLocation(getLocation());
 
225
                }
 
226
            }
 
227
            project.fireTaskFinished(this, exc);
 
228
            throw exc;
 
229
        }
 
230
    }
 
231
}
 
232