import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Utility class containing methods for IO related operations
* @param <T> binding Class Type
*
* @return binding Class Object which properties populated from JSON File Location
+ * @throws IOException when fails to do IO operations
*/
- public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) {
+ public static final <T> T convertToJsonObject(String fileLocation, Class<T> bindingClass) throws IOException {
// Load Resource from give path
final InputStream resourceAsStream = loadResourceAsStream(fileLocation);
// If resource is null throw an exception
if (resourceAsStream == null) {
final String errorMessage = String.format("Invalid File location: %s", fileLocation);
- throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));
+ throw new IOException(errorMessage, new FileNotFoundException(errorMessage));
}
// Parse input stream
try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, Charset.forName("UTF-8"))) {
return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStreamReader, bindingClass);
-
} catch (JsonMappingException | JsonParseException e) {
// If parsing fails due to Invalid Json or Json IO Issues throw an exception
fileLocation);
LOG.error(errorMessage);
- throw new RuntimeException(errorMessage, e);
-
+ throw new IOException(errorMessage, e);
} catch (IOException e) {
// If parsing fails due to IO Issues throw an exception
final String errorMessage = String.format("IO Error while parsing Json File location: %s", fileLocation);
LOG.error(errorMessage);
- throw new RuntimeException(errorMessage, e);
-
+ throw new IOException(errorMessage, e);
}
-
}
/**
// If properties file is not present throw an exception
if (propertiesFileInputStream == null) {
final String errorMessage = String.format("Invalid Properties File at location: %s",
- propertiesFileLocation);
+ propertiesFileLocation);
+ //TODO: discuss and change this excpeiton as well.
throw new RuntimeException(errorMessage, new FileNotFoundException(errorMessage));
}
// Load Resource from give path
return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);
}
-
-
}