Search in sources :

Example 31 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsFacade method registerProperties.

/**
 * Registers properties from file with given name to the configuration service.
 *
 * @param filename  the name of the file with properties
 * @param properties  properties to be registered
 */
protected void registerProperties(String filename, Properties properties) {
    if (configurationService != null) {
        try {
            if (!configurationService.registersProperties(getBundleSymbolicName(), filename)) {
                configurationService.addOrUpdateProperties(getBundleSymbolicName(), getBundleVersion(), filename, properties, defaultConfig.get(filename));
            } else if (configurationService.registersProperties(getBundleSymbolicName(), filename)) {
                configurationService.updatePropertiesAfterReinstallation(getBundleSymbolicName(), getBundleVersion(), filename, defaultConfig.get(filename), properties);
            }
            Properties registeredProps = configurationService.getBundleProperties(getBundleSymbolicName(), filename, defaultConfig.get(filename));
            config.put(filename, registeredProps);
        } catch (IOException e) {
            throw new MotechException("Cant register settings", e);
        }
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 32 with MotechException

use of org.motechproject.commons.api.MotechException 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)

Example 33 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class BundleIconServiceImpl method loadBundleIcon.

private BundleIcon loadBundleIcon(URL iconURL) {
    InputStream is = null;
    try {
        URLConnection urlConn = iconURL.openConnection();
        is = urlConn.getInputStream();
        String mime = urlConn.getContentType();
        byte[] image = IOUtils.toByteArray(is);
        return new BundleIcon(image, mime);
    } catch (IOException e) {
        throw new MotechException("Error loading icon.", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) BundleIcon(org.motechproject.server.web.dto.BundleIcon) IOException(java.io.IOException) URLConnection(java.net.URLConnection)

Aggregations

MotechException (org.motechproject.commons.api.MotechException)33 IOException (java.io.IOException)25 InputStream (java.io.InputStream)9 Properties (java.util.Properties)8 ArrayList (java.util.ArrayList)6 MessageDigest (java.security.MessageDigest)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Map (java.util.Map)3 ObjectName (javax.management.ObjectName)3 SettingsRecord (org.motechproject.config.domain.SettingsRecord)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 DigestInputStream (java.security.DigestInputStream)2 QueueViewMBean (org.apache.activemq.broker.jmx.QueueViewMBean)2 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)2 Bundle (org.osgi.framework.Bundle)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2