Search in sources :

Example 1 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class ConfigPropertiesUtilsTest method shouldThrowExceptionIfNoneOfTheFilesInTheDefaultLocationIsReadable.

@Test(expected = MotechConfigurationException.class)
public void shouldThrowExceptionIfNoneOfTheFilesInTheDefaultLocationIsReadable() {
    List<ConfigLocation> configLocationList = new ArrayList<>();
    ConfigLocation configLocationMock = Mockito.mock(ConfigLocation.class);
    configLocationList.add(configLocationMock);
    when(configLocationMock.getLocation()).thenReturn("location");
    when(configLocationMock.getFile("fileName", ConfigLocation.FileAccessType.READABLE)).thenThrow(new MotechConfigurationException("File is not readable"));
    when(configLocationMock.getLocation()).thenReturn("location");
    ConfigPropertiesUtils.getDefaultPropertiesFile(ConfigLocation.FileAccessType.READABLE, configLocationList, "fileName");
}
Also used : ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) Test(org.junit.Test)

Example 2 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class MockCoreConfigurationService method loadConfig.

public Properties loadConfig(String fileName) {
    Properties properties = new Properties();
    ClassPathResource resource = new ClassPathResource(fileName);
    try {
        try (InputStream is = resource.getInputStream()) {
            properties.load(is);
        }
    } catch (IOException e) {
        throw new MotechConfigurationException("Error when loading properties: " + fileName);
    }
    return properties;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 3 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class CoreConfigurationServiceImpl method getConfigLocation.

@Override
public ConfigLocation getConfigLocation() {
    Iterable<ConfigLocation> locations = configLocationFileStore.getAll();
    StringBuilder sb = new StringBuilder("");
    for (ConfigLocation configLocation : locations) {
        sb.append(configLocation.getLocation()).append(' ');
        Resource configLocationResource = configLocation.toResource();
        try {
            Resource motechSettings = configLocationResource.createRelative(ConfigurationConstants.SETTINGS_FILE_NAME);
            if (motechSettings.isReadable() && locations != null) {
                return configLocation;
            }
            LOGGER.warn("Could not read motech-settings.properties from: " + configLocationResource.toString());
        } catch (IOException e) {
            LOGGER.warn("Problem reading motech-settings.properties from location: " + configLocationResource.toString(), e);
        }
    }
    throw new MotechConfigurationException(String.format("Could not read settings from any of the config locations. Searched directories: %s.", sb));
}
Also used : 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 4 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class ConfigPropertiesUtils method saveConfig.

/**
 * Saves properties to the given {@code File}.
 *
 * @param file the file
 * @param properties
 */
public static void saveConfig(File file, Properties properties) {
    try {
        file.getParentFile().mkdirs();
        file.createNewFile();
        try (Writer writer = new FileWriter(file)) {
            properties.store(writer, String.format("MOTECH %s properties.", file.getName()));
        }
    } catch (IOException e) {
        throw new MotechConfigurationException(String.format("Error saving %s properties to file.", file.getName()), e);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 5 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class ConfigPropertiesUtils method createPropertiesConfiguration.

/**
 * Creates {@code PropertiesConfiguration} in the given path if it does not exist
 * @param basePath path to the file with config
 * @param fileName name of the file with config
 * @return {@code PropertiesConfiguration} from the given path
 */
public static PropertiesConfiguration createPropertiesConfiguration(String basePath, String fileName) {
    createFileIfDoesNotExist(basePath, fileName);
    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setBasePath(basePath);
    propertiesConfiguration.setFileName(fileName);
    try {
        propertiesConfiguration.load();
    } catch (ConfigurationException e) {
        throw new MotechConfigurationException(String.format("Cannot load configuration from: %s", propertiesConfiguration.getPath()), e);
    }
    return propertiesConfiguration;
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Aggregations

MotechConfigurationException (org.motechproject.config.core.exception.MotechConfigurationException)19 IOException (java.io.IOException)6 Test (org.junit.Test)5 Properties (java.util.Properties)4 ConfigLocation (org.motechproject.config.core.domain.ConfigLocation)4 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 URI (java.net.URI)2 URISupport (org.apache.activemq.util.URISupport)2 URISupport.isCompositeURI (org.apache.activemq.util.URISupport.isCompositeURI)2 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 BootstrapConfig (org.motechproject.config.core.domain.BootstrapConfig)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Resource (org.springframework.core.io.Resource)2 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1