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");
}
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;
}
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));
}
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);
}
}
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;
}
Aggregations