/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/RoutingContextImpl.java

http://mobicents.googlecode.com/ · Java · 132 lines · 80 code · 20 blank · 32 comment · 4 complexity · 9cc1c58a58c17d33f9d58de71b4e83ec 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.ss7.m3ua.impl.parameter;
  23. import java.util.Arrays;
  24. import javolution.xml.XMLFormat;
  25. import javolution.xml.XMLSerializable;
  26. import javolution.xml.stream.XMLStreamException;
  27. import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
  28. import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
  29. /**
  30. *
  31. * @author amit bhayani
  32. *
  33. */
  34. public class RoutingContextImpl extends ParameterImpl implements RoutingContext, XMLSerializable {
  35. private static final String ARRAY_SIZE = "size";
  36. private static final String ROUTING_CONTEXT = "rc";
  37. private long[] rcs = null;
  38. private byte[] value;
  39. public RoutingContextImpl(){
  40. this.tag = Parameter.Routing_Context;
  41. }
  42. protected RoutingContextImpl(byte[] value) {
  43. this.tag = Parameter.Routing_Context;
  44. int count = 0;
  45. int arrSize = 0;
  46. rcs = new long[(value.length / 4)];
  47. while (count < value.length) {
  48. rcs[arrSize] = 0;
  49. rcs[arrSize] |= value[count++] & 0xFF;
  50. rcs[arrSize] <<= 8;
  51. rcs[arrSize] |= value[count++] & 0xFF;
  52. rcs[arrSize] <<= 8;
  53. rcs[arrSize] |= value[count++] & 0xFF;
  54. rcs[arrSize] <<= 8;
  55. rcs[arrSize++] |= value[count++] & 0xFF;
  56. }
  57. this.value = value;
  58. }
  59. protected RoutingContextImpl(long[] routingcontexts) {
  60. this.tag = Parameter.Routing_Context;
  61. rcs = routingcontexts;
  62. encode();
  63. }
  64. private void encode() {
  65. // create byte array taking into account data, point codes and
  66. // indicators;
  67. this.value = new byte[(rcs.length * 4)];
  68. int count = 0;
  69. int arrSize = 0;
  70. // encode routing context
  71. while (count < value.length) {
  72. value[count++] = (byte) (rcs[arrSize] >>> 24);
  73. value[count++] = (byte) (rcs[arrSize] >>> 16);
  74. value[count++] = (byte) (rcs[arrSize] >>> 8);
  75. value[count++] = (byte) (rcs[arrSize++]);
  76. }
  77. }
  78. public long[] getRoutingContexts() {
  79. return this.rcs;
  80. }
  81. @Override
  82. protected byte[] getValue() {
  83. return value;
  84. }
  85. @Override
  86. public String toString() {
  87. return String.format("RoutingContext rc=%s", Arrays.toString(rcs));
  88. }
  89. /**
  90. * XML Serialization/Deserialization
  91. */
  92. protected static final XMLFormat<RoutingContextImpl> RC_XML = new XMLFormat<RoutingContextImpl>(
  93. RoutingContextImpl.class) {
  94. @Override
  95. public void read(javolution.xml.XMLFormat.InputElement xml, RoutingContextImpl rc) throws XMLStreamException {
  96. int size = xml.getAttribute(ARRAY_SIZE).toInt();
  97. rc.rcs = new long[size];
  98. size = 0;
  99. while (xml.hasNext()) {
  100. rc.rcs[size++] = xml.get(ROUTING_CONTEXT, Long.class);
  101. }
  102. rc.encode();
  103. }
  104. @Override
  105. public void write(RoutingContextImpl rc, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
  106. xml.setAttribute(ARRAY_SIZE, rc.rcs.length);
  107. for (Long l : rc.rcs) {
  108. xml.add(l, ROUTING_CONTEXT, Long.class);
  109. }
  110. }
  111. };
  112. }