PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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