source: release-kits/lirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/New.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 5.4 KB
Line 
1
2/*
3* Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17package ise.antelope.tasks;
18
19import java.util.Enumeration;
20import java.util.Hashtable;
21import java.util.Vector;
22
23import ise.library.PrivilegedAccessor;
24
25import org.apache.tools.ant.*;
26import org.apache.tools.ant.taskdefs.*;
27import org.apache.tools.ant.taskdefs.condition.Condition;
28import org.apache.tools.ant.types.EnumeratedAttribute;
29
30/**
31 * @author Dale Anson
32 * @version $Revision: 1.6 $
33 * @since Ant 1.6
34 */
35public class New extends Task implements TaskContainer {
36
37 // storage for nested tasks
38 private Vector tasks = new Vector();
39
40 // should the build fail if any subtasks fail? Default is no.
41 private boolean failOnError = false;
42
43
44 /**
45 * Add a task.
46 *
47 * @param task A task to execute
48 * @exception BuildException won't happen
49 */
50 public void addTask(Task task) throws BuildException {
51 if (task != null) {
52 tasks.addElement(task);
53 }
54 }
55
56
57 /**
58 * Determines whether the build should fail if the time limit has expired on
59 * this task. Default is no.
60 *
61 * @param fail if true, fail the build if the time limit has been reached.
62 */
63 public void setFailonerror(boolean fail) {
64 failOnError = fail;
65 }
66
67
68 /**
69 * Execute all nested tasks, using a newly configured task each time.
70 *
71 * @exception BuildException only if failOnError is true
72 */
73 public void execute() throws BuildException {
74 log("+++ CAUTION: <new> is experimental and does not work in all situations.");
75 try {
76 for (int i = 0; i < tasks.size(); i++) {
77 Task currentTask = (Task)tasks.get(i);
78 Task replacementTask = getReplacement(currentTask);
79 if (replacementTask == null)
80 replacementTask = currentTask;
81 try {
82 replacementTask.perform();
83 }
84 catch (Exception ex) {
85 if (failOnError)
86 throw ex;
87 else {
88 log(ex.getMessage());
89 ex.printStackTrace();
90 }
91 }
92 }
93 }
94 catch (Exception e) {
95 if (failOnError) {
96 throw new BuildException(e.getMessage());
97 }
98 else {
99 log(e.getMessage());
100 e.printStackTrace();
101 }
102 }
103 }
104
105
106 /**
107 * Creates a new task from the given task.
108 *
109 * @param ue
110 * @return a new copy of the task
111 */
112 private Task getReplacement(Task task) {
113 try {
114 String taskname = task.getTaskName();
115 //if (taskname.indexOf(":") > -1)
116 // taskname = taskname.substring(taskname.indexOf(":") + 1);
117 UnknownElement replacement = new UnknownElement(taskname);
118 replacement.setProject(getProject());
119 replacement.setTaskType(task.getTaskType());
120 replacement.setTaskName(taskname);
121 replacement.setLocation(task.getLocation());
122 replacement.setOwningTarget(getOwningTarget());
123 RuntimeConfigurable wrapper = (RuntimeConfigurable)PrivilegedAccessor.invokeMethod(task, "getWrapper", new Object[]{});
124 replacement.setRuntimeConfigurableWrapper(wrapper);
125 wrapper.setProxy(replacement);
126 replaceChildren(wrapper, replacement);
127 try {
128 replacement.maybeConfigure();
129 }
130 catch (Exception e) {
131 //e.printStackTrace();
132 }
133 if (replacement.getTask() != null) {
134 Target target = (Target) PrivilegedAccessor.getValue(task, "target");
135 PrivilegedAccessor.invokeMethod(target, "replaceChild", new Object[]{task, replacement.getTask()});
136 }
137 return replacement.getTask();
138 }
139 catch (Exception e) {
140 e.printStackTrace();
141 return null;
142 }
143 }
144
145 /**
146 * Recursively adds an UnknownElement instance for each child element of
147 * replacement.
148 *
149 * @param wrapper
150 * @param parentElement
151 * @since Ant 1.5.1
152 */
153 private void replaceChildren(RuntimeConfigurable wrapper, UnknownElement parentElement) {
154 Enumeration e = wrapper.getChildren();
155 while (e.hasMoreElements()) {
156 RuntimeConfigurable childWrapper =
157 (RuntimeConfigurable) e.nextElement();
158 UnknownElement childElement =
159 new UnknownElement(childWrapper.getElementTag());
160 parentElement.addChild(childElement);
161 childElement.setProject(getProject());
162 childElement.setRuntimeConfigurableWrapper(childWrapper);
163 childWrapper.setProxy(childElement);
164 replaceChildren(childWrapper, childElement);
165 }
166 }
167
168}
169
170
Note: See TracBrowser for help on using the repository browser.