Search in sources :

Example 1 with ChangeConfigurationFailException

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);
        }
    }
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) Journal(net.viperfish.journal.framework.Journal) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) File(java.io.File) FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ChangeConfigurationFailException(net.viperfish.journal.framework.errors.ChangeConfigurationFailException) CannotClearPasswordException(net.viperfish.journal.framework.errors.CannotClearPasswordException) ChangeConfigurationFailException(net.viperfish.journal.framework.errors.ChangeConfigurationFailException)

Aggregations

File (java.io.File)1 Journal (net.viperfish.journal.framework.Journal)1 CannotClearPasswordException (net.viperfish.journal.framework.errors.CannotClearPasswordException)1 ChangeConfigurationFailException (net.viperfish.journal.framework.errors.ChangeConfigurationFailException)1 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)1 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)1 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1