private static Document parseDocument(URL resourceUrl) throws CoreConfigException { try { InputStream inputSource = resourceUrl.openStream(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(inputSource); } catch(ParserConfigurationException e) { throw new CoreConfigException("No XML Parser was provided", e); } catch (SAXException e) { throw new CoreConfigException("Unable to parse the XML document", e); } catch (IOException e) { throw new CoreConfigException("Unable to read input source document", e); } } }
@Override public long convertToLong(final char type, final long value) throws CoreConfigException { long unit = value; switch(type) { case 'g': unit = unit * 1024; case 'm': unit = unit * 1024; case 'k': unit = unit * 1024; return unit; default : if(Character.isDigit(type)) { return unit * 1024 * 1024; } else { throw new CoreConfigException("Unrecognized quanitifier on memory spec: " + type); } } }
@Override public long convertToLong(final char type, final long value) throws CoreConfigException { long unit = value; switch(type) { case 'd': unit = unit * 24; case 'h': unit = unit * 60; case 'm': unit = unit * 60; case 's': unit = unit * 1000; return unit; default : if(Character.isDigit(type)) { return unit * 1000 * 60; } else { throw new CoreConfigException("Unrecognized quanitifier on time unit: " + type); } } }
private static long convertUnit(String unit, ValueConverterType converter) throws CoreConfigException { if(StringUtils.stringNotEmpty(unit)) { try { char lastDigit = unit.charAt(unit.length() - 1); lastDigit = Character.toLowerCase(lastDigit); long longUnit = -1; if(Character.isDigit(lastDigit)) { longUnit = Long.valueOf(unit); } else { String value = unit.substring(0, unit.length() - 1); longUnit = Long.valueOf(value); } return converter.convertToLong(lastDigit, longUnit); } catch(NumberFormatException e) { throw new CoreConfigException("Unable to parse value in unit: " + unit, e); } } else { throw new CoreConfigException("Could not parse empty unit"); } }