package service; import com.typesafe.config.ConfigException; import exceptions.PoseidonException; import play.api.Play; import play.mvc.Http; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PoseidonPropertyService { public static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; private static final Map<String, String> props = new HashMap<String, String>() {{ put("urlSelf", "http://localhost:9000"); put("tokenvalidation","enabled"); put("password.reset.expiration", "3600"); put("urlFrontend","http://localhost:3000"); }}; public static String getProperty(String key) { String value; try { value = Play.unsafeApplication().configuration().underlying().getString(key); } catch (ConfigException e) { value = props.get(key); if (value == null || value.isEmpty()) { throw new PoseidonException(Http.Status.INTERNAL_SERVER_ERROR, "Missing property with key " + key); } } return value; } public static int getIntProperty(String key){ String str = getProperty(key); try { return Integer.parseInt(str); } catch (NumberFormatException e) { throw new PoseidonException(Http.Status.INTERNAL_SERVER_ERROR, "Property value is not an integer value" + key); } } public static double getFloatProperty(String key){ String str = getProperty(key); try { return Double.parseDouble(str); } catch (NumberFormatException e) { throw new PoseidonException(Http.Status.INTERNAL_SERVER_ERROR, "Property value is not an floating point value" + key); } } public static List<String> getStringListProperty(String key){ List<String> values; try { values = Play.unsafeApplication().configuration().underlying().getStringList(key); } catch (ConfigException e) { throw new PoseidonException(Http.Status.INTERNAL_SERVER_ERROR, "Property value is not an integer value" + key); } return values; } public static String getDefaultDatasource() { return Play.unsafeApplication().configuration().underlying().getString("play.ebean.defaultDatasource"); } }