/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/util/AbstractAPIConfig.java

http://mobicents.googlecode.com/ · Java · 247 lines · 156 code · 19 blank · 72 comment · 26 complexity · 300d90df0420ba68504c31dca67245ad MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.util;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * Abstract base class for {@link APIConfig} instances. Provides some basic
  27. * functionality useful across all <tt>APIConfig</tt> instances.
  28. * <p>
  29. * <b>Boolean values:</b> Any of the strings "true", "on" or "yes" will
  30. * evaluate to a boolean <tt>true</tt>. The strings "false", "off" or "no"
  31. * will all evaluate to a boolean <tt>false</tt>. Additionally, booleans
  32. * can be numeric, where zero is evaluated as <tt>false</tt> and a non-zero
  33. * value is evaluated as <tt>true</tt>.
  34. * </p>
  35. *
  36. * <p>
  37. * <b>Numbers</b>: Numbers can be specified in any of decimal, hexadecimal,
  38. * binary or octal notations. Decimal is the default. Prefixing a number
  39. * with "0x" causes it to be parsed as hexadecimal. Prefixing a number with
  40. * '0' causes it to be parsed as octal. Suffixing the number with a 'b'
  41. * causes it to be parsed as binary. For example:
  42. * </p>
  43. * <ul>
  44. * <li><tt>3757</tt> is a decimal number (base 10).</li>
  45. * <li><tt>0xa91</tt> is a hexadecimal number (base 16).</li>
  46. * <li><tt>0731</tt> is an octal number (base 8).</li>
  47. * <li><tt>1001110b</tt> is a binary number (base 2).</li>
  48. * </ul>
  49. *
  50. * <p>
  51. * Decimal numbers may also be modified with a multiplier. Suffixing the
  52. * letters 'k' or 'm' at the end of a decimal number multiples it by
  53. * 1024 and 1048576 respectively. This is useful for specifying a number or
  54. * kilobytes or megabytes. For example
  55. * </p>
  56. * <ul>
  57. * <li><tt>4k</tt> is equivalent to <tt>4096</tt>.</li>
  58. * <li><tt>96m</tt> is equivalent to <tt>100663296</tt>.</li>
  59. * </ul>
  60. * @version $Id: AbstractAPIConfig.java 477 2009-07-12 18:00:20Z orank $
  61. */
  62. public abstract class AbstractAPIConfig implements APIConfig {
  63. private static final Map<String, Boolean> BOOLEANS =
  64. new HashMap<String, Boolean>();
  65. static {
  66. BOOLEANS.put("1", Boolean.TRUE);
  67. BOOLEANS.put("true", Boolean.TRUE);
  68. BOOLEANS.put("on", Boolean.TRUE);
  69. BOOLEANS.put("yes", Boolean.TRUE);
  70. BOOLEANS.put("0", Boolean.FALSE);
  71. BOOLEANS.put("false", Boolean.FALSE);
  72. BOOLEANS.put("off", Boolean.FALSE);
  73. BOOLEANS.put("no", Boolean.FALSE);
  74. }
  75. public boolean isSet(String property) {
  76. try {
  77. getProperty(property);
  78. return true;
  79. } catch (PropertyNotFoundException x) {
  80. return false;
  81. }
  82. }
  83. public String getProperty(String property, String defaultValue) {
  84. try {
  85. return getProperty(property);
  86. } catch (PropertyNotFoundException x) {
  87. return defaultValue;
  88. }
  89. }
  90. public short getShort(String property) throws InvalidConfigurationException, PropertyNotFoundException {
  91. long value = getInt(property);
  92. if (value < (long) Short.MIN_VALUE || value > (long) Short.MAX_VALUE) {
  93. throw new InvalidConfigurationException(
  94. "Property value exceeds valid short range: " + value, property);
  95. }
  96. return (short) value;
  97. }
  98. public short getShort(String property, short defaultValue) {
  99. short s;
  100. try {
  101. s = getShort(property);
  102. } catch (PropertyNotFoundException x) {
  103. s = defaultValue;
  104. }
  105. return s;
  106. }
  107. public int getInt(String property) throws InvalidConfigurationException, PropertyNotFoundException {
  108. long value = convertToNumber(getProperty(property));
  109. if (value < (long) Integer.MIN_VALUE || value > (long) Integer.MAX_VALUE) {
  110. throw new InvalidConfigurationException(
  111. "Property value exceeds valid integer range: " + value,
  112. property);
  113. }
  114. return (int) value;
  115. }
  116. public int getInt(String property, int defaultValue) {
  117. int value;
  118. try {
  119. value = getInt(property);
  120. } catch (PropertyNotFoundException x) {
  121. value = defaultValue;
  122. }
  123. return value;
  124. }
  125. public long getLong(String property) throws InvalidConfigurationException, PropertyNotFoundException {
  126. return convertToNumber(getProperty(property));
  127. }
  128. public long getLong(String property, long defaultValue) {
  129. long l;
  130. try {
  131. l = getLong(property);
  132. } catch (PropertyNotFoundException x) {
  133. l = defaultValue;
  134. }
  135. return l;
  136. }
  137. public boolean getBoolean(String property) throws InvalidConfigurationException, PropertyNotFoundException {
  138. return toBoolean(getProperty(property));
  139. }
  140. public boolean getBoolean(String property, boolean defaultValue) {
  141. boolean value;
  142. try {
  143. value = getBoolean(property);
  144. } catch (PropertyNotFoundException x) {
  145. value = defaultValue;
  146. }
  147. return value;
  148. }
  149. public <T> T getClassInstance(String property, Class<T> type) {
  150. String className = getProperty(property);
  151. try {
  152. Class<?> clazz = Class.forName(className);
  153. if (!type.isAssignableFrom(clazz)) {
  154. throw new InvalidConfigurationException(
  155. className + " is not an instance of " + type, className);
  156. }
  157. @SuppressWarnings("unchecked")
  158. T obj = ((Class<T>) clazz).newInstance();
  159. return obj;
  160. } catch (Exception x) {
  161. throw new InvalidConfigurationException(
  162. "Could not instantiate a " + className, x);
  163. }
  164. }
  165. public <T> T getClassInstance(String property, Class<T> type, T defaultValue) {
  166. try {
  167. return getClassInstance(property, type);
  168. } catch (PropertyNotFoundException x) {
  169. return defaultValue;
  170. }
  171. }
  172. /**
  173. * Convert a string value to a boolean.
  174. * @param value The value to determine a boolean value for.
  175. * @return A boolean value.
  176. * @throws InvalidConfigurationException If <tt>value</tt> is not
  177. * a valid boolean value.
  178. */
  179. protected boolean toBoolean(String value) {
  180. Boolean bool = BOOLEANS.get(value);
  181. if (bool == null) {
  182. try {
  183. if (Integer.parseInt(value) != 0) {
  184. bool = Boolean.TRUE;
  185. } else {
  186. bool = Boolean.FALSE;
  187. }
  188. } catch (NumberFormatException x) {
  189. throw new InvalidConfigurationException(
  190. "Invalid boolean property", value);
  191. }
  192. }
  193. return bool.booleanValue();
  194. }
  195. /**
  196. * Convert a number string into a <code>long</code>, taking into account
  197. * base and multiplication specifiers.
  198. * @param num The String representing the number.
  199. * @return The parsed number.
  200. * @throws NumberFormatException If the String cannot be parsed as a number.
  201. */
  202. protected long convertToNumber(final String num) throws NumberFormatException {
  203. int base = 10;
  204. long multiplier = 1;
  205. String s;
  206. char firstChar = num.charAt(0);
  207. char lastChar = num.charAt(num.length() - 1);
  208. if (num.startsWith("0x") || num.startsWith("0X")) {
  209. base = 16;
  210. s = num.substring(2);
  211. } else if (lastChar == 'b') {
  212. base = 2;
  213. s = num.substring(0, num.length() - 1);
  214. } else if (lastChar == 'k') {
  215. multiplier = 1024L;
  216. s = num.substring(0, num.length() - 1);
  217. } else if (lastChar == 'm') {
  218. multiplier = 1048576L;
  219. s = num.substring(0, num.length() - 1);
  220. } else if (firstChar == '0' && num.length() > 1) {
  221. base = 8;
  222. s = num.substring(1);
  223. } else {
  224. s = num;
  225. }
  226. return Long.parseLong(s, base) * multiplier;
  227. }
  228. }