}
}
}
private void updateCredentials(String application, RunId runId, Credentials updates) throws IOException {
Location credentialsLocation = locationFactory.create(String.format("/%s/%s/%s", application, runId.getId(),
Constants.Files.CREDENTIALS));
// Try to read the old credentials.
Credentials credentials = new Credentials();
if (credentialsLocation.exists()) {
DataInputStream is = new DataInputStream(new BufferedInputStream(credentialsLocation.getInputStream()));
try {
credentials.readTokenStorageStream(is);
} finally {
is.close();
}
}
// Overwrite with the updates.
credentials.addAll(updates);
// Overwrite the credentials.
Location tmpLocation = credentialsLocation.getTempFile(Constants.Files.CREDENTIALS);
// Save the credentials store with user-only permission.
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(tmpLocation.getOutputStream("600")));
try {
credentials.writeTokenStorageToStream(os);
} finally {
os.close();
}
// Rename the tmp file into the credentials location
tmpLocation.renameTo(credentialsLocation);
LOG.debug("Secure store for {} {} saved to {}.", application, runId, credentialsLocation.toURI());
}