StreamUtil.copy(rawConfigInputStream, rawConfigFile, true);
String newConfig = StringPropertyReplacer.replaceProperties(rawConfigFile.toString());
ByteArrayInputStream newConfigInputStream = new ByteArrayInputStream(newConfig.getBytes());
Preferences.importPreferences(newConfigInputStream);
AgentConfiguration newAgentConfig = new AgentConfiguration(prefNode);
if (newAgentConfig.getAgentConfigurationVersion() == 0) {
throw new IllegalArgumentException("Bad preferences node");
}
// If we had a security token, restore it so we can maintain our known registration with the server.
// Note that if the configuration file already had a security token defined, it will be used and the old
// token we had will be thrown away.
if (securityToken != null) {
if (newAgentConfig.getAgentSecurityToken() == null) {
log.debug("Restoring embedded agent security token");
newAgentConfig.setAgentSecurityToken(securityToken);
} else {
log.debug("Not restoring embedded agent security token, the config file was preconfigured with one");
}
}
prefNode.flush();
} catch (Exception e) {
// a problem occurred importing the config file; let's restore our original values
try {
Preferences.importPreferences(new ByteArrayInputStream(backup.toByteArray()));
} catch (Exception e1) {
// its conceivable the same problem occurred here as with the original exception (backing store problem?)
// let's throw the original exception, not this one
}
throw e;
}
AgentConfiguration agentConfig = new AgentConfiguration(prefNode);
return agentConfig;
}