use of net.viperfish.journal.framework.errors.ChangeConfigurationFailException in project vsDiaryWriter by shilongdai.
the class ChangeConfigurationOperation method execute.
@Override
public void execute() {
Map<String, String> old = backUpConfig(config);
// actually load all units
this.refresh();
// save all entries in memory
List<Journal> result = db().getAll();
// save the password in memory
String password = auth().getPassword();
try {
// clear all entries
resetUnits();
// updates configuration
for (Entry<String, String> i : config.entrySet()) {
Configuration.setProperty(i.getKey(), i.getValue());
}
// refresh all providers to reflect changes in configuration
EntryDatabases.INSTANCE.refreshAll();
Indexers.INSTANCE.refreshAll();
AuthManagers.INSTANCE.refreshAll();
JournalTransformers.INSTANCE.refreshAll();
// re-initialize components
this.refresh();
// clear new components
resetUnits();
// set the password on the new Auth manager
auth().setPassword(password);
// configuration, entry database, and indexer
for (Journal i : result) {
i.setId(null);
db().addEntry(i);
indexer().add(i);
}
} catch (Exception e1) {
ChangeConfigurationFailException cf = new ChangeConfigurationFailException("Failed to change components from:" + old + " to:" + config + "message:" + e1.getMessage(), e1);
try {
revert(result, password, old);
} catch (Exception e) {
File userHome = new File(System.getProperty("user.home"));
File export = new File(userHome, "export.txt");
OperationErrorException fr = new OperationErrorException("Failed to revert changes, application not in usable status, please clear all data files. Exporting all entries to " + export.getAbsolutePath());
fr.initCause(cf);
ExportJournalOperation dump = new ExportJournalOperation(export.getAbsolutePath());
dump.execute();
throw fr;
}
throw new RuntimeException(cf);
} finally {
// save the configuration
try {
Configuration.save();
} catch (ConfigurationException e) {
ChangeConfigurationFailException cf = new ChangeConfigurationFailException("Failed to save configuration, change not persistent.", e);
throw new RuntimeException(cf);
}
}
}
Aggregations