587
|
1 |
/*
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3 |
* contributor license agreements. See the NOTICE file distributed with
|
|
4 |
* this work for additional information regarding copyright ownership.
|
|
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6 |
* (the "License"); you may not use this file except in compliance with
|
|
7 |
* the License. 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 |
*
|
|
17 |
*/
|
|
18 |
/* package org.apache.tools.ant.taskdefs; */
|
|
19 |
/* * Portion Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
|
|
20 |
|
|
21 |
package com.nokia.helium.core.ant.taskdefs;
|
|
22 |
|
|
23 |
import org.apache.tools.ant.BuildException;
|
|
24 |
import org.apache.tools.ant.Project;
|
|
25 |
import org.apache.tools.ant.Task;
|
|
26 |
import org.apache.tools.ant.TaskContainer;
|
628
|
27 |
import org.apache.tools.ant.taskdefs.Sleep;
|
587
|
28 |
|
|
29 |
/**
|
|
30 |
* Retries the nested task a set number of times
|
|
31 |
*
|
|
32 |
* @since Ant 1.7.1
|
|
33 |
* @ant.task name="retry" category="Core"
|
|
34 |
*/
|
|
35 |
public class RetryTask extends Task implements TaskContainer {
|
|
36 |
/**
|
|
37 |
* task to execute n times
|
|
38 |
*/
|
|
39 |
private Task nestedTask;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* set retryCount to 1 by default
|
|
43 |
*/
|
|
44 |
private int retryCount = 1;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* set sleepTime to 0 by default
|
|
48 |
*/
|
|
49 |
private int sleepTime;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* set the task
|
|
53 |
*
|
|
54 |
* @param t
|
|
55 |
* the task to retry.
|
|
56 |
*/
|
|
57 |
public synchronized void addTask(Task t) {
|
|
58 |
if (nestedTask != null) {
|
|
59 |
throw new BuildException(
|
|
60 |
"The retry task container accepts a single nested task"
|
|
61 |
+ " (which may be a sequential task container)");
|
|
62 |
}
|
|
63 |
nestedTask = t;
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* set the number of times to retry the task
|
|
68 |
*
|
|
69 |
* @param n
|
|
70 |
* the number to use.
|
|
71 |
* @ant.not-required Default is 1.
|
|
72 |
*/
|
|
73 |
public void setRetryCount(int n) {
|
|
74 |
retryCount = n;
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* set the sleep time inbetween each retry
|
|
79 |
*
|
|
80 |
* @param n
|
|
81 |
* the time in ms to sleep between each retry.
|
|
82 |
* @ant.not-required Default is 0.
|
|
83 |
*/
|
|
84 |
public void setSleepTime(int n) {
|
|
85 |
sleepTime = n;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* perform the work
|
|
90 |
*
|
|
91 |
* @throws BuildException
|
|
92 |
* if there is an error.
|
|
93 |
*/
|
|
94 |
public void execute() {
|
|
95 |
if (nestedTask == null) {
|
|
96 |
throw new BuildException("The nested retry task not defined");
|
|
97 |
}
|
|
98 |
|
|
99 |
StringBuffer errorMessages = new StringBuffer();
|
|
100 |
String br = getProject().getProperty("line.separator");
|
|
101 |
for (int i = 0; i <= retryCount; i++) {
|
|
102 |
try {
|
|
103 |
nestedTask.perform();
|
|
104 |
break;
|
|
105 |
} catch (BuildException e) {
|
|
106 |
errorMessages.append(e.getMessage());
|
|
107 |
if (i >= retryCount) {
|
|
108 |
StringBuffer exceptionMessage = new StringBuffer();
|
|
109 |
exceptionMessage.append("Task [").append(
|
|
110 |
nestedTask.getTaskName());
|
|
111 |
exceptionMessage.append("] failed after [").append(
|
|
112 |
retryCount);
|
|
113 |
exceptionMessage.append("] attempts; giving up.")
|
|
114 |
.append(br);
|
|
115 |
exceptionMessage.append("Error messages:").append(br);
|
|
116 |
exceptionMessage.append(errorMessages);
|
|
117 |
throw new BuildException(exceptionMessage.toString(),
|
|
118 |
getLocation());
|
|
119 |
}
|
|
120 |
log("Attempt [" + i + "]: error occurred; retrying...", e,
|
|
121 |
Project.MSG_INFO);
|
|
122 |
errorMessages.append(br);
|
|
123 |
if (sleepTime > 0) {
|
|
124 |
Sleep sleepTimer = new Sleep();
|
|
125 |
sleepTimer.setMilliseconds(sleepTime);
|
|
126 |
sleepTimer.execute();
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
}
|
|
131 |
}
|