LOGGER.debug(String.format("Checking if resource %s of type %s is a cleanup candidate against %d rules.",
resource.getId(), resource.getResourceType(), rules.size()));
// We create a clone of the resource each time when we try the rule. In the first iteration of the rules
// we identify the rule with the nearest termination date if there is any rule considers the resource
// as a cleanup candidate. Then the rule is applied to the original resource.
Rule nearestRule = null;
if (rules.size() == 1) {
nearestRule = rules.get(0);
} else {
Date nearestTerminationTime = null;
for (Rule rule : rules) {
Resource clone = resource.cloneResource();
if (!rule.isValid(clone) && (nearestTerminationTime == null
|| nearestTerminationTime.after(clone.getExpectedTerminationTime()))) {
nearestRule = rule;
nearestTerminationTime = clone.getExpectedTerminationTime();
}
}
}
if (nearestRule != null && !nearestRule.isValid(resource)) {
LOGGER.info(String.format("Resource %s is marked as a cleanup candidate.", resource.getId()));
return false;
} else {
LOGGER.info(String.format("Resource %s is not marked as a cleanup candidate.", resource.getId()));
return true;