}
});
}
private Optional<String> rewriteJob(JobConfigRewrite jobRewrite, JobStore.Mutable jobStore) {
IJobConfiguration existingJob = IJobConfiguration.build(jobRewrite.getOldJob());
IJobConfiguration rewrittenJob;
Optional<String> error = Optional.absent();
try {
rewrittenJob = ConfigurationManager.validateAndPopulate(
IJobConfiguration.build(jobRewrite.getRewrittenJob()));
} catch (TaskDescriptionException e) {
// We could add an error here, but this is probably a hint of something wrong in
// the client that's causing a bad configuration to be applied.
throw Throwables.propagate(e);
}
if (existingJob.getKey().equals(rewrittenJob.getKey())) {
if (existingJob.getOwner().equals(rewrittenJob.getOwner())) {
Multimap<String, IJobConfiguration> matches = jobsByKey(jobStore, existingJob.getKey());
switch (matches.size()) {
case 0:
error = Optional.of(
"No jobs found for key " + JobKeys.canonicalString(existingJob.getKey()));
break;
case 1:
Map.Entry<String, IJobConfiguration> match =
Iterables.getOnlyElement(matches.entries());
IJobConfiguration storedJob = match.getValue();
if (storedJob.equals(existingJob)) {
jobStore.saveAcceptedJob(match.getKey(), rewrittenJob);
} else {
error = Optional.of(
"CAS compare failed for " + JobKeys.canonicalString(storedJob.getKey()));
}
break;
default:
error = Optional.of("Multiple jobs found for key "