PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/AreaListImpl.java

http://mobicents.googlecode.com/
Java | 248 lines | 128 code | 29 blank | 91 comment | 20 complexity | 358de4dac30bd4de37c1a8ea8d994489 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.ss7.map.service.lsm;
 24
 25import java.io.IOException;
 26import java.util.ArrayList;
 27import java.util.Arrays;
 28import java.util.List;
 29
 30import org.mobicents.protocols.asn.AsnException;
 31import org.mobicents.protocols.asn.AsnInputStream;
 32import org.mobicents.protocols.asn.AsnOutputStream;
 33import org.mobicents.protocols.asn.Tag;
 34import org.mobicents.protocols.ss7.map.api.MAPException;
 35import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 36import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 37import org.mobicents.protocols.ss7.map.api.service.lsm.Area;
 38import org.mobicents.protocols.ss7.map.api.service.lsm.AreaList;
 39import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 40
 41/**
 42 * @author amit bhayani
 43 * 
 44 */
 45public class AreaListImpl implements AreaList, MAPAsnPrimitive {
 46
 47	private Area[] areas = null;
 48
 49	/**
 50	 * 
 51	 */
 52	public AreaListImpl() {
 53		super();
 54	}
 55
 56	/**
 57	 * @param areas
 58	 */
 59	public AreaListImpl(Area[] areas) {
 60		super();
 61		this.areas = areas;
 62	}
 63
 64	/*
 65	 * (non-Javadoc)
 66	 * 
 67	 * @see org.mobicents.protocols.ss7.map.api.service.lsm.AreaList#getAreas()
 68	 */
 69	@Override
 70	public Area[] getAreas() {
 71		return this.areas;
 72	}
 73
 74	/*
 75	 * (non-Javadoc)
 76	 * 
 77	 * @see
 78	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
 79	 */
 80	@Override
 81	public int getTag() throws MAPException {
 82		return Tag.SEQUENCE;
 83	}
 84
 85	/*
 86	 * (non-Javadoc)
 87	 * 
 88	 * @see
 89	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
 90	 * ()
 91	 */
 92	@Override
 93	public int getTagClass() {
 94		return Tag.CLASS_UNIVERSAL;
 95	}
 96
 97	/*
 98	 * (non-Javadoc)
 99	 * 
100	 * @see
101	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
102	 * ()
103	 */
104	@Override
105	public boolean getIsPrimitive() {
106		return false;
107	}
108
109	/*
110	 * (non-Javadoc)
111	 * 
112	 * @see
113	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
114	 * (org.mobicents.protocols.asn.AsnInputStream)
115	 */
116	@Override
117	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
118		try {
119			int length = ansIS.readLength();
120			this._decode(ansIS, length);
121		} catch (IOException e) {
122			throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
123					MAPParsingComponentExceptionReason.MistypedParameter);
124		} catch (AsnException e) {
125			throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
126					MAPParsingComponentExceptionReason.MistypedParameter);
127		}
128	}
129
130	/*
131	 * (non-Javadoc)
132	 * 
133	 * @see
134	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
135	 * (org.mobicents.protocols.asn.AsnInputStream, int)
136	 */
137	@Override
138	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
139		try {
140			this._decode(ansIS, length);
141		} catch (IOException e) {
142			throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
143					MAPParsingComponentExceptionReason.MistypedParameter);
144		} catch (AsnException e) {
145			throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
146					MAPParsingComponentExceptionReason.MistypedParameter);
147		}
148	}
149
150	private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
151		AsnInputStream ais = asnIS.readSequenceStreamData(length);
152		
153		List<Area> arealList = new ArrayList<Area>();
154		
155		while (true) {
156			if (ais.available() == 0)
157				break;
158
159			int tag = ais.readTag();
160
161			if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || ais.isTagPrimitive() || tag != Tag.SEQUENCE) {
162				throw new MAPParsingComponentException("Error while decoding AreaList: bad tag class, tag or not primitive",
163						MAPParsingComponentExceptionReason.MistypedParameter);
164			}
165			
166			Area a = new AreaImpl();
167			((AreaImpl)a).decodeAll(ais);
168			
169			arealList.add(a);
170
171		}//end of while
172		
173		this.areas = new Area[arealList.size()];
174		this.areas = arealList.toArray(this.areas);
175	}
176
177	/*
178	 * (non-Javadoc)
179	 * 
180	 * @see
181	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
182	 * (org.mobicents.protocols.asn.AsnOutputStream)
183	 */
184	@Override
185	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
186		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
187	}
188
189	/*
190	 * (non-Javadoc)
191	 * 
192	 * @see
193	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
194	 * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
195	 */
196	@Override
197	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
198		try {
199			asnOs.writeTag(tagClass, false, tag);
200			int pos = asnOs.StartContentDefiniteLength();
201			this.encodeData(asnOs);
202			asnOs.FinalizeContent(pos);
203		} catch (AsnException e) {
204			throw new MAPException("AsnException when encoding reportSMDeliveryStatusRequest: " + e.getMessage(), e);
205		}
206	}
207
208	/*
209	 * (non-Javadoc)
210	 * 
211	 * @see
212	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
213	 * (org.mobicents.protocols.asn.AsnOutputStream)
214	 */
215	@Override
216	public void encodeData(AsnOutputStream asnOs) throws MAPException {
217		if (this.areas == null || this.areas.length == 0) {
218			throw new MAPException("Error while encoding AreaList the mandatory parameter[Area[]] is not defined");
219		}
220		
221		for (int count = 0; count < areas.length; count++) {
222			((AreaImpl)areas[count]).encodeAll(asnOs);
223		}
224	}
225
226	@Override
227	public int hashCode() {
228		final int prime = 31;
229		int result = 1;
230		result = prime * result + Arrays.hashCode(areas);
231		return result;
232	}
233
234	@Override
235	public boolean equals(Object obj) {
236		if (this == obj)
237			return true;
238		if (obj == null)
239			return false;
240		if (getClass() != obj.getClass())
241			return false;
242		AreaListImpl other = (AreaListImpl) obj;
243		if (!Arrays.equals(areas, other.areas))
244			return false;
245		return true;
246	}
247
248}