use of org.motechproject.commons.api.MotechException in project motech by motech.
the class ConfigurationServiceImpl method loadSettingsFromStream.
private SettingsRecord loadSettingsFromStream(org.springframework.core.io.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);
}
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class KeyEvaluator method manipulateValue.
private String manipulateValue(List<String> manipulations, String value) throws TaskHandlerException {
String manipulateValue = value;
for (String manipulation : manipulations) {
if (manipulation.contains("format")) {
String formatElements = manipulation.substring(FORMAT_PATTERN_BEGIN_INDEX, manipulation.length() - 1);
if (isNotBlank(formatElements)) {
String[] items = formatElements.split(",");
for (int i = 0; i < items.length; ++i) {
String item = items[i];
if (item.startsWith("{{") && item.endsWith("}}")) {
item = item.substring(2, item.length() - 2);
KeyInformation subKey = KeyInformation.parse(item);
Object subValue = getValue(subKey);
items[i] = subValue != null ? subValue.toString() : "";
}
}
manipulateValue = String.format(manipulateValue, items);
}
} else {
try {
manipulateValue = manipulate(manipulation, manipulateValue);
} catch (MotechException e) {
String msg = e.getMessage();
if ("task.warning.manipulation".equalsIgnoreCase(msg)) {
taskContext.publishWarningActivity(msg, manipulation);
} else {
throw new TaskHandlerException(TRIGGER, msg, e, manipulation);
}
}
}
}
return manipulateValue;
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class TasksPostExecutionHandler method getRetriesFromSettings.
private int getRetriesFromSettings(Task task, Map<String, Object> params) {
List<Integer> retryInterval = new ArrayList<>();
int numberOfRetries;
try (InputStream retries = settings.getRawConfig(TASK_PROPERTIES_FILE_NAME)) {
if (retries != null) {
Properties props = new Properties();
props.load(retries);
for (Map.Entry<Object, Object> entry : props.entrySet()) {
retryInterval.add(0, Integer.valueOf(entry.getValue().toString()));
}
numberOfRetries = retryInterval.size();
} else {
numberOfRetries = 0;
}
} catch (IOException e) {
throw new MotechException("Error loading raw file config to properties", e);
}
setRetryParameters(task, params, retryInterval, numberOfRetries);
return numberOfRetries;
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class PomReader method getPomDoc.
private Document getPomDoc() {
if (pomDoc == null) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setIgnoringComments(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
pomDoc = documentBuilder.parse(pomFilePath);
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new MotechException("Unable to read the pom file", e);
}
}
return pomDoc;
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class SettingsController method saveSettings.
/**
* Saves the given settings.
*
* @param settings the settings to be saved, not null
*/
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/settings", method = RequestMethod.POST)
public void saveSettings(@RequestBody SettingsDto settings) {
String taskPossibleErrors = settings.getTaskPossibleErrors();
try (OutputStream os = new ByteArrayOutputStream()) {
settings.getTaskRetriesProps().store(os, "TaskRetries");
if (settings.isValid()) {
settingsFacade.setProperty(TASK_POSSIBLE_ERRORS, taskPossibleErrors);
settingsFacade.saveRawConfig(TASK_PROPERTIES_FILE_NAME, new String(os.toString()));
} else {
throw new IllegalArgumentException("Settings are not valid");
}
} catch (IOException e) {
throw new MotechException("Error parsing task retries", e);
}
}
Aggregations