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

http://mobicents.googlecode.com/ · Java · 103 lines · 62 code · 15 blank · 26 comment · 0 complexity · 863e2bb2b1f6202fda1022524c693f24 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.Enumeration;
  24. import java.util.MissingResourceException;
  25. import java.util.ResourceBundle;
  26. import java.util.Vector;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.mobicents.protocols.smpp.gsm.GSMError;
  30. import org.mobicents.protocols.smpp.message.MessageState;
  31. /**
  32. * Look up descriptions of various SMPP codes.
  33. * @version $Id: APIMessages.java 457 2009-01-15 17:37:42Z orank $
  34. * @since 0.4.0
  35. */
  36. public class APIMessages {
  37. public static final String BUNDLE_PROPERTY = "smppapi.bundle";
  38. public static final String DEFAULT_BUNDLE_NAME = "smpp_messages";
  39. private static final Logger LOG = LoggerFactory.getLogger(APIMessages.class);
  40. private static final String PACKET_STATUS_PREFIX = "packet.status.";
  41. private static final String MESSAGE_STATE_PREFIX = "message.state.";
  42. private static final String GSM_ERROR_PREFIX = "gsm.errors.";
  43. private ResourceBundle bundle;
  44. public APIMessages() {
  45. loadBundle();
  46. }
  47. public String getPacketStatus(int statusCode) {
  48. StringBuilder resource = new StringBuilder(PACKET_STATUS_PREFIX);
  49. resource.append("0x");
  50. resource.append(Integer.toHexString(statusCode).toLowerCase());
  51. return bundle.getString(resource.toString());
  52. }
  53. public String getMessageState(MessageState state) {
  54. StringBuilder resource = new StringBuilder(MESSAGE_STATE_PREFIX);
  55. resource.append("0x");
  56. resource.append(Integer.toHexString(state.getValue()).toLowerCase());
  57. return bundle.getString(resource.toString());
  58. }
  59. public String getGSMError(GSMError error) {
  60. StringBuilder resource = new StringBuilder(GSM_ERROR_PREFIX);
  61. resource.append(error.name().toLowerCase());
  62. return bundle.getString(resource.toString());
  63. }
  64. private void loadBundle() {
  65. try {
  66. bundle = ResourceBundle.getBundle(getBundleName());
  67. } catch (MissingResourceException x) {
  68. LOG.warn("Cannot load API messages.");
  69. bundle = getDummyBundle();
  70. }
  71. }
  72. private String getBundleName() {
  73. APIConfig cfg = APIConfigFactory.getConfig();
  74. return cfg.getProperty(BUNDLE_PROPERTY, DEFAULT_BUNDLE_NAME);
  75. }
  76. private ResourceBundle getDummyBundle() {
  77. return new ResourceBundle() {
  78. @Override
  79. protected Object handleGetObject(String key) {
  80. return key;
  81. }
  82. @Override
  83. public Enumeration<String> getKeys() {
  84. return new Vector<String>().elements();
  85. }
  86. };
  87. }
  88. }