📄 apiconfig.java
字号:
*/ public String getProperty(String property) throws PropertyNotFoundException { String val = super.getProperty(property); if (val == null) { throw new PropertyNotFoundException(property); } else { return val; } } /** * Get the value for a property or return a default value if it is not set. * @param property The name of the property to retrieve. * @param defaultValue The value to return if <code>property</code> is not * set. * @return The value for <code>property</code>. */ public String getProperty(String property, String defaultValue) { String val = super.getProperty(property); if (val == null) { val = defaultValue; } return val; } /** * Get the value of a property, parsed as a <code>short</code>. If the * property is not set, the default value is returned. * @param property The name of the property to retrieve the value for. * @param defaultValue The default value to return if the property is * not set. * @return The value for <code>property</code>. * @throws InvalidConfigurationException If the value cannot be parsed as * a <code>short</code>. */ public short getShort(String property, short defaultValue) throws InvalidConfigurationException { short s; try { s = getShort(property); } catch (PropertyNotFoundException x) { s = defaultValue; } return s; } /** * Get the value for a property, parsed as a Java <code>short</code>. * * @param property * the name of the property to retrive. * @throws ie.omk.smpp.util.PropertyNotFoundException * if <code>property</code> is not found in the configuration. * @throws ie.omk.smpp.util.InvalidConfigurationException * if the value is not a valid short. */ public short getShort(String property) throws InvalidConfigurationException, PropertyNotFoundException { int i = getInt(property); if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) { throw new InvalidConfigurationException("Property value exceeds " + "valid short range: " + i, property); } return (short) i; } /** * Get the value for a property, parsed as a Java <code>int</code>. If * the property is not found, the default value is returned. * * @param property * the name of the property to retrive. * @param defaultValue * the value to return if the property does not exist. * @throws ie.omk.smpp.util.InvalidConfigurationException * if the value is not a valid integer. */ public int getInt(String property, int defaultValue) throws InvalidConfigurationException { try { return getInt(property); } catch (PropertyNotFoundException x) { } return defaultValue; } /** * Get the value for a property, parsed as a Java <code>int</code>. * * @param property * the name of the property to retrive. * @throws ie.omk.smpp.util.PropertyNotFoundException * if <code>property</code> is not found in the configuration. * @throws ie.omk.smpp.util.InvalidConfigurationException * if the value is not a valid integer. */ public int getInt(String property) throws InvalidConfigurationException, PropertyNotFoundException { long l; try { String n = getProperty(property); l = convertToNumber(n); if (l < (long) Integer.MIN_VALUE || l > (long) Integer.MAX_VALUE) { throw new InvalidConfigurationException("Property value exceeds" + " valid int range: " + l, property); } } catch (NumberFormatException x) { throw new InvalidConfigurationException(BAD_PROPERTY_VALUE, property); } return (int) l; } /** * Get the value for a property, parsed as a Java <code>long</code>. If * the property is not found, the default value is returned. * * @param property * the name of the property to retrive. * @param defaultValue * the value to return if the property does not exist. * @throws ie.omk.smpp.util.InvalidConfigurationException * if the value is not a valid long. */ public long getLong(String property, long defaultValue) throws InvalidConfigurationException { long l; try { l = getLong(property); } catch (PropertyNotFoundException x) { l = defaultValue; } return l; } /** * Get the value for a property, parsed as a Java <code>long</code>. * * @param property * the name of the property to retrive. * @throws ie.omk.smpp.util.PropertyNotFoundException * if <code>property</code> is not found in the configuration. * @throws ie.omk.smpp.util.InvalidConfigurationException * if the value is not a valid long. */ public long getLong(String property) throws InvalidConfigurationException, PropertyNotFoundException { long l; try { String n = getProperty(property); l = convertToNumber(n); } catch (NumberFormatException x) { throw new InvalidConfigurationException(BAD_PROPERTY_VALUE, property); } return l; } /** * Get a property as a boolean value. Any of 'on', 'yes' or 'true' * (irrelevant of case) will evaluate to <code>true</code>. Any of 'off', * 'no' or 'false' will evaluate to <code>false</code>. Boolean * parameters may also be specified as a number, where zero will equate to * <code>false</code> while non-zero will equate to <code>true</code>. * All other words will result in an InvalidConfigurationException being * thrown. * * @param property * the name of the property to look up. * @param defaultValue * the value to return if the property does not exist. * @throws InvalidConfigurationException * if the property has a value that cannot be parsed or * interpreted as boolean. */ public boolean getBoolean(String property, boolean defaultValue) throws InvalidConfigurationException { try { return getBoolean(property); } catch (PropertyNotFoundException x) { } return defaultValue; } /** * Get a property as a boolean value. Any of 'on', 'yes' or 'true' * (irrelevant of case) will evaluate to <code>true</code>. Any of 'off', * 'no' or 'false' will evaluate to <code>false</code>. Boolean * parameters may also be specified as a number, where zero will equate to * <code>false</code> while non-zero will equate to <code>true</code>. * All other words will result in an exception being thrown. * * @throws ie.omk.smpp.util.PropertyNotFoundException * if <code>property</code> is not found in the configuration. * @throws InvalidConfigurationException * if the property has a value that cannot be parsed or * interpreted as boolean. */ public boolean getBoolean(String property) throws InvalidConfigurationException, PropertyNotFoundException { boolean b = false; String s = getProperty(property).toLowerCase(); try { int n = Integer.parseInt(s); if (n > 0) { b = true; } else { b = false; } } catch (NumberFormatException x) { // It's not a number.. if ("yes".equals(s) || "on".equals(s) || "true".equals(s) || "1".equals(s)) { b = true; } else if ("no".equals(s) || "off".equals(s) || "false".equals(s) || "0".equals(s)) { b = false; } else { throw new InvalidConfigurationException( BAD_PROPERTY_VALUE, property, s); } } return b; } /** * Try to locate the default smppapi properties resource. * @return A URL pointing to the default properties resource, or * <code>null</code> if it cannot be found. */ private URL getDefaultPropertiesResource() { URL url = null; Class c = getClass(); for (int i = 0; i < SEARCH_PATH.length && url == null; i++) { url = c.getResource(SEARCH_PATH[i] + PROPS_RESOURCE); } return url; } /** * Load the properties. */ private void loadAPIProperties() throws IOException { if (propsURL != null) { load(propsURL.openStream()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Loaded API properties from " + propsURL); StringWriter w = new StringWriter(); list(new PrintWriter(w)); LOGGER.debug("\n" + w.toString()); } } } /** * Convert a number string into a <code>long</code>, taking into account * base and multiplication specifiers. * @param num The String representing the number. * @return The parsed number. * @throws NumberFormatException If the String cannot be parsed as a number. */ long convertToNumber(final String num) throws NumberFormatException { int base = 10; long multiplier = 1; String s; if (num.startsWith("0x") || num.startsWith("0X")) { base = 16; s = num.substring(2); } else if (num.endsWith("b")) { base = 2; s = num.substring(0, num.length() - 1); } else if (num.endsWith("k")) { multiplier = 1024L; s = num.substring(0, num.length() - 1); } else if (num.endsWith("m")) { multiplier = 1048576L; s = num.substring(0, num.length() - 1); } else if (num.startsWith("0") && num.length() > 1) { base = 8; s = num.substring(1); } else { s = num; } return Long.parseLong(s, base) * multiplier; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -