📄 isomsg.java
字号:
/* * Copyright (c) 2000 jPOS.org. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the jPOS project * (http://www.jpos.org/)". Alternately, this acknowledgment may * appear in the software itself, if and wherever such third-party * acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse * or promote products derived from this software without prior * written permission. For written permission, please contact * license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", * nor may "jPOS" appear in their name, without prior written * permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project. For more * information please see <http://www.jpos.org/>. */package org.jpos.iso;import java.io.*;import java.util.*;import org.jpos.util.Loggeable;import org.jpos.util.LogSource;import org.jpos.iso.packager.XMLPackager;import org.jpos.iso.packager.ISO93BPackager;import org.jpos.iso.header.BaseHeader;/** * implements <b>Composite</b> * whithin a <b>Composite pattern</b> * * @author apr@cs.com.uy * @version $Id: ISOMsg.java,v 1.50 2002/08/06 14:26:30 apr Exp $ * @see ISOComponent * @see ISOField */public class ISOMsg extends ISOComponent implements Cloneable, Loggeable, Externalizable{ protected Hashtable fields; protected int maxField; protected ISOPackager packager; protected boolean dirty, maxFieldDirty; protected int direction; protected ISOHeader header; protected int fieldNumber = -1; public static final int INCOMING = 1; public static final int OUTGOING = 2; private static final long serialVersionUID = 4306251831901413975L; /** * Creates an ISOMsg */ public ISOMsg () { fields = new Hashtable (); maxField = -1; dirty = true; maxFieldDirty=true; direction = 0; header = null; } /** * Creates a nested ISOMsg */ public ISOMsg (int fieldNumber) { this(); setFieldNumber (fieldNumber); } /** * changes this Component field number<br> * Use with care, this method does not change * any reference held by a Composite. * @param fieldNumber new field number */ public void setFieldNumber (int fieldNumber) { this.fieldNumber = fieldNumber; } /** * Creates an ISOMsg with given mti * @param mti Msg's MTI */ public ISOMsg (String mti) { this(); try { setMTI (mti); } catch (ISOException e) { // should never happen } } /** * Sets the direction information related to this message * @param direction can be either ISOMsg.INCOMING or ISOMsg.OUTGOING */ public void setDirection(int direction) { this.direction = direction; } /** * Sets an optional message header image * @param b header image */ public void setHeader(byte[] b) { header = new BaseHeader (b); } public void setHeader (ISOHeader header) { this.header = header; } /** * get optional message header image * @return message header image (may be null) */ public byte[] getHeader() { return (header != null) ? header.pack() : null; } /** * Return this messages ISOHeader */ public ISOHeader getISOHeader() { return header; } /** * @return the direction (ISOMsg.INCOMING or ISOMsg.OUTGOING) * @see ISOChannel */ public int getDirection() { return direction; } /** * @return true if this message is an incoming message * @see ISOChannel */ public boolean isIncoming() { return direction == INCOMING; } /** * @return true if this message is an outgoing message * @see ISOChannel */ public boolean isOutgoing() { return direction == OUTGOING; } /** * @return the max field number associated with this message */ public int getMaxField() { if (maxFieldDirty) recalcMaxField(); return maxField; } private void recalcMaxField() { ISOComponent c; maxField = 0; for (Enumeration e = fields.keys(); e.hasMoreElements(); ) { Object obj = e.nextElement(); if (obj instanceof Integer) maxField = Math.max (maxField, ((Integer)obj).intValue()); } maxFieldDirty = false; } /** * @param p - a peer packager */ public void setPackager (ISOPackager p) { packager = p; } /** * @return the peer packager */ public ISOPackager getPackager () { return packager; } /** * Set a field within this message * @param c - a component */ public void set (ISOComponent c) throws ISOException { Integer i = (Integer) c.getKey(); fields.put (i, c); if (i.intValue() > maxField) maxField = i.intValue(); dirty = true; } /** * Creates an ISOField associated with fldno within this ISOMsg * @param fldno field number * @param value field value */ public void set (int fldno, String value) throws ISOException { set (new ISOField (fldno, value)); } /** * Creates an ISOBinaryField associated with fldno within this ISOMsg * @param fldno field number * @param value field value */ public void set (int fldno, byte[] value) throws ISOException { set (new ISOBinaryField (fldno, value)); } /** * Unset a field if it exists, otherwise ignore. * @param fldno - the field number */ public void unset (int fldno) { if (fields.remove (new Integer (fldno)) != null) dirty = maxFieldDirty = true; } /** * Unsets several fields at once * @param flds - array of fields to be unset from this ISOMsg */ public void unset (int[] flds) { for (int i=0; i<flds.length; i++) unset (flds[i]); } /** * In order to interchange <b>Composites</b> and <b>Leafs</b> we use * getComposite(). A <b>Composite component</b> returns itself and * a Leaf returns null. * * @return ISOComponent */ public ISOComponent getComposite() { return this; } /** * setup BitMap * @exception ISOException */ public void recalcBitMap () throws ISOException { ISOComponent c; if (!dirty) return; if(maxField>128) { BitSet bmap=new BitSet(64); for (int i=1; i<=64; i++) if((c=(ISOComponent) fields.get(new Integer (i+128))) != null) bmap.set (i); set (new ISOBitMap (65, bmap)); } BitSet bmap = new BitSet (getMaxField() > 64 ? 128 : 64); int tmpMaxField=maxField > 128 ? 128 : maxField; for (int i=1; i<=tmpMaxField; i++) if ((c = (ISOComponent) fields.get (new Integer (i))) != null) bmap.set (i); set (new ISOBitMap (-1, bmap)); dirty = false; } /** * clone fields */ public Hashtable getChildren() { return (Hashtable) fields.clone(); } /** * pack the message with the current packager * @return the packed message * @exception ISOException */ public byte[] pack() throws ISOException { synchronized (this) { recalcBitMap(); return packager.pack(this); } } /** * unpack a message * @param b - raw message * @return consumed bytes * @exception ISOException */ public int unpack(byte[] b) throws ISOException { synchronized (this) { return packager.unpack(this, b); } } public void unpack (InputStream in) throws IOException, ISOException { synchronized (this) { packager.unpack(this, in); } } /** * dump the message to a PrintStream. The output is sorta * XML, intended to be easily parsed. * <br> * Each component is responsible for its own dump function, * ISOMsg just calls dump on every valid field. * @param p - print stream * @param indent - optional indent string */ public void dump (PrintStream p, String indent) { ISOComponent c; p.print (indent + "<" + XMLPackager.ISOMSG_TAG); switch (direction) { case INCOMING: p.print (" direction=\"incoming\""); break; case OUTGOING: p.print (" direction=\"outgoing\""); break; } if (fieldNumber != -1) p.print (" "+XMLPackager.ID_ATTR +"=\""+fieldNumber +"\""); p.println (">"); String newIndent = indent + " "; for (int i=0; i<=maxField; i++) { if ((c = (ISOComponent) fields.get (new Integer (i))) != null) c.dump (p, newIndent); //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -