📄 apiconfig.java
字号:
InputStream is = c.getResourceAsStream(propsFile); if (is != null) loadAPIPropertiesFromStream(is); else logger.warn("Could not reload API properties. File not found: " + propsFile); } catch (IOException x) { logger.warn("Could not reload API properties.", x); return (false); } return (true); } /** Get the singleton <code>APIConfig</code> instance. */ public static final APIConfig getInstance() { if (instance == null) { instance = new APIConfig(); instance.loadAPIProperties(); } return (instance); } /** Get the value for a property. * @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. */ 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, 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 { short s = 0; try { String n = getProperty(property); if (n != null) { int base = getBase(n); n = stripBaseSpecifiers(n, base); s = Short.parseShort(n, base); } } catch (NumberFormatException x) { throw new InvalidConfigurationException("Bad property value", property); } return (s); } /** 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 { int i = 0; try { String n = getProperty(property); if (n != null) { int base = getBase(n); n = stripBaseSpecifiers(n, base); i = Integer.parseInt(n, base); } } catch (NumberFormatException x) { throw new InvalidConfigurationException("Bad property value", property); } return (i); } /** 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 { try { return (getLong(property)); } catch (InvalidConfigurationException x) { } return (defaultValue); } /** 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 = 0; try { String n = getProperty(property); if (n != null) { int base = getBase(n); n = stripBaseSpecifiers(n, base); l = Long.parseLong(n, base); } } 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 (s.equals("yes") || s.equals("on") || s.equals("true")) b = true; else if (s.equals("no") || s.equals("off") || s.equals("false")) b = false; else throw new InvalidConfigurationException("Bad property value", property); } return (b); } /** Get the base of a number. Numbers can be specified as hex by prefixing * a '0x' or '0X', as binary if they are trailed by a 'b' or as octal if * they are prefixed with a '0'. * @param n the string representing a number with some form of base * specifier. * @return the base of the integer. */ private int getBase(String n) { int base = 10; if (n.startsWith("0x") || n.startsWith("0X")) base = 16; else if (n.startsWith("0")) base = 8; else if (n.endsWith("b")) base = 2; return (base); } /** Strip the base specifiers out of a string number. * @param n the number as a string (including base specifiers). * @param base the base of the number. * @return a string containing only the number, base specifiers are removed. * For example, '0x4a' will cause a return of '4a' if <code>base</code> is * specified as 16. * @see #getBase */ private String stripBaseSpecifiers(String n, int base) { switch (base) { case 2: n = n.substring(0, n.length() - 1); break; case 8: n = n.substring(1); break; case 16: n = n.substring(2); break; } return (n); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -