Search in sources :

Example 11 with SettingsRecord

use of org.motechproject.config.domain.SettingsRecord in project motech by motech.

the class ConfigLoader method loadMotechSettings.

/**
 * Loads MOTECH settings containing core platform settings.
 *
 * @return the {SettingsRecord} object
 */
public SettingsRecord loadMotechSettings() {
    SettingsRecord settingsRecord;
    ConfigLocation configLocation = coreConfigurationService.getConfigLocation();
    Resource configLocationResource = configLocation.toResource();
    try {
        Resource motechSettings = configLocationResource.createRelative(ConfigurationConstants.SETTINGS_FILE_NAME);
        settingsRecord = loadSettingsFromStream(motechSettings);
        settingsRecord.setFilePath(configLocationResource.getURL().getPath());
        checkSettingsRecord(settingsRecord);
    } catch (IOException e) {
        throw new MotechConfigurationException(String.format("Could not read settings from file at location %s", configLocation), e);
    }
    return settingsRecord;
}
Also used : SettingsRecord(org.motechproject.config.domain.SettingsRecord) ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException)

Example 12 with SettingsRecord

use of org.motechproject.config.domain.SettingsRecord in project motech by motech.

the class ConfigLoader method loadSettingsFromStream.

private SettingsRecord loadSettingsFromStream(Resource motechSettings) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        try (DigestInputStream dis = new DigestInputStream(motechSettings.getInputStream(), digest)) {
            // load configFileSettings and calculate MD5 hash
            SettingsRecord settingsRecord = new SettingsRecord();
            settingsRecord.load(dis);
            settingsRecord.setConfigFileChecksum(new String(digest.digest()));
            // startup loaded
            return settingsRecord;
        } catch (IOException e) {
            throw new MotechException("Error loading configuration", e);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new MotechException("MD5 algorithm not available", e);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) DigestInputStream(java.security.DigestInputStream) SettingsRecord(org.motechproject.config.domain.SettingsRecord) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

SettingsRecord (org.motechproject.config.domain.SettingsRecord)12 Transactional (org.springframework.transaction.annotation.Transactional)4 IOException (java.io.IOException)3 MessageDigest (java.security.MessageDigest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 MotechException (org.motechproject.commons.api.MotechException)3 Resource (org.springframework.core.io.Resource)3 DigestInputStream (java.security.DigestInputStream)2 Test (org.junit.Test)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConfigLocation (org.motechproject.config.core.domain.ConfigLocation)1 MotechConfigurationException (org.motechproject.config.core.exception.MotechConfigurationException)1 Caching (org.springframework.cache.annotation.Caching)1