org.hidetake.groovy.ssh.util.Utility.groovy Maven / Gradle / Ivy
package org.hidetake.groovy.ssh.util
import com.jcraft.jsch.JSchException
import groovy.util.logging.Slf4j
/**
* Provides utility methods.
*
* @author Hidetake Iwata
*/
@Slf4j
class Utility {
static T callWithDelegate(Closure closure, delegate, ... arguments) {
def cloned = closure.clone() as Closure
cloned.resolveStrategy = Closure.DELEGATE_FIRST
cloned.delegate = delegate
cloned.call(*arguments)
}
/**
* Curry a method with self for recursive.
*
* @param closure
* @return curried closure
*/
static Closure currySelf(Closure closure) {
final curried
curried = closure.curry {
closure.call(curried)
}
}
/**
* Execute the closure with retrying.
* This method catches only {@link com.jcraft.jsch.JSchException}s.
*
* @param retryCount
* @param retryWaitSec
* @param closure
*/
static T retry(int retryCount, int retryWaitSec, Closure closure) {
assert closure != null, 'closure should be set'
if (retryCount > 0) {
try {
closure()
} catch (JSchException e) {
log.warn("Retrying: ${e.getClass().name}: ${e.localizedMessage}")
ManagedBlocking.sleep(retryWaitSec * 1000L)
retry(retryCount - 1, retryWaitSec, closure)
}
} else {
closure()
}
}
}