use of org.jpos.util.LogEvent in project jPOS by jpos.
the class ISOBasePackager method pack.
/**
* pack method that works in conjunction with {@link #unpack(ISOComponent, byte[])}.
* <p>
* Handles a tertiary bitmap possibly appearing in Data Element {@code thirdBitmapField}.<br>
*
* @param m the Component to pack
* @return Message image
* @exception ISOException
*/
@Override
public byte[] pack(ISOComponent m) throws ISOException {
LogEvent evt = null;
if (logger != null)
evt = new LogEvent(this, "pack");
try {
if (m.getComposite() != m)
throw new ISOException("Can't call packager on non Composite");
ArrayList<byte[]> v = new ArrayList<byte[]>(128);
byte[] b;
byte[] hdr = null;
int len = 0;
Map fields = m.getChildren();
ISOComponent c = (ISOComponent) fields.get(0);
int first = getFirstField();
// pre-read header, if it exists, and advance total len
if (m instanceof ISOMsg && headerLength > 0) {
hdr = ((ISOMsg) m).getHeader();
if (hdr != null)
len += hdr.length;
}
if (first > 0 && c != null) {
b = fld[0].pack(c);
len += b.length;
v.add(b);
}
// will store primary and secondary part of bitmap
BitSet bmap12 = null;
// will store tertiary part of bitmap
BitSet bmap3 = null;
if (emitBitMap()) {
// The ISOComponent stores a single bitmap in field -1, which could be up to
// 192 bits long. If we have a thirdBitmapField, we may need to split the full
// bitmap into 1 & 2 at the beginning (16 bytes), and 3rd inside the Data Element
c = (ISOComponent) fields.get(-1);
// the full bitmap (up to 192 bits long)
bmap12 = (BitSet) c.getValue();
if (// we may need to split it!
thirdBitmapField >= 0 && fld[thirdBitmapField] instanceof ISOBitMapPackager) {
if (// some bits are set in the high part (3rd bitmap)
bmap12.length() - 1 > 128) {
// new bitmap, with the high 3rd bitmap (use 128 as dummy bit0)
bmap3 = bmap12.get(128, 193);
// don't really need to clear dummy bit0 I guess...
bmap3.clear(0);
// indicate presence of field that will hold the 3rd bitmap
bmap12.set(thirdBitmapField);
// clear high part, so that the field's pack() method will not use it
bmap12.clear(129, 193);
// Now create add-hoc ISOBitMap in position thirdBitmapField to hold 3rd bitmap
ISOBitMap bmField = new ISOBitMap(thirdBitmapField);
bmField.setValue(bmap3);
m.set(bmField);
// fields is a clone of m's inner map, so we store it here as well
fields.put(thirdBitmapField, bmField);
// bit65 should only be set if there's a data-containing DE-65 (which should't happen!)
bmap12.set(65, fields.get(65) == null ? false : true);
} else {
// else: No bits/fields above 128 in this message.
// In case there's an old (residual/garbage) field `thirdBitmapField` in the message
// we need to clear the bit and the data
// remove from ISOMsg
m.unset(thirdBitmapField);
// remove from inner bitmap
bmap12.clear(thirdBitmapField);
// remove from fields clone
fields.remove(thirdBitmapField);
}
}
// now will emit the 1st and 2nd bitmaps, and the loop below will take care of 3rd
// when emitting field `thirdBitmapField`
b = getBitMapfieldPackager().pack(c);
len += b.length;
v.add(b);
}
// if Field 1 is a BitMap then we are packing an
// ISO-8583 message so next field is fld#2.
// else we are packing an ANSI X9.2 message, first field is 1
int tmpMaxField = Math.min(m.getMaxField(), (bmap3 != null || fld.length > 129) ? 192 : 128);
for (int i = first; i <= tmpMaxField; i++) {
if ((c = (ISOComponent) fields.get(i)) != null) {
try {
ISOFieldPackager fp = fld[i];
if (fp == null)
throw new ISOException("null field " + i + " packager");
b = fp.pack(c);
len += b.length;
v.add(b);
} catch (ISOException e) {
if (evt != null) {
evt.addMessage("error packing field " + i);
evt.addMessage(c);
evt.addMessage(e);
}
throw new ISOException("error packing field " + i, e);
}
}
}
// BBB This part not needed
// if(m.getMaxField()>128 && fld.length > 128) {
// for (int i=1; i<=64; i++) {
// if ((c = (ISOComponent)fields.get (i + 128)) != null)
// {
// try {
// b = fld[i+128].pack(c);
// len += b.length;
// v.add (b);
// } catch (ISOException e) {
// if (evt != null) {
// evt.addMessage ("error packing field "+(i+128));
// evt.addMessage (c);
// evt.addMessage (e);
// }
// throw e;
// }
// }
// }
// }
int k = 0;
byte[] d = new byte[len];
// if ISOMsg insert header (we pre-read it at the beginning)
if (hdr != null) {
System.arraycopy(hdr, 0, d, k, hdr.length);
k += hdr.length;
}
for (byte[] bb : v) {
System.arraycopy(bb, 0, d, k, bb.length);
k += bb.length;
}
if (// save a few CPU cycle if no logger available
evt != null)
evt.addMessage(ISOUtil.hexString(d));
return d;
} catch (ISOException e) {
if (evt != null)
evt.addMessage(e);
throw e;
} finally {
if (evt != null)
Logger.log(evt);
}
}
use of org.jpos.util.LogEvent in project jPOS by jpos.
the class ISOServer method shutdownChannels.
private void shutdownChannels() {
Iterator iter = channels.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
WeakReference ref = (WeakReference) entry.getValue();
ISOChannel c = (ISOChannel) ref.get();
if (c != null) {
try {
c.disconnect();
fireEvent(new ISOServerClientDisconnectEvent(this));
} catch (IOException e) {
Logger.log(new LogEvent(this, "shutdown", e));
}
}
}
}
use of org.jpos.util.LogEvent in project jPOS by jpos.
the class AmexChannel method getMessageLength.
protected int getMessageLength() throws IOException, ISOException {
int l = 0;
byte[] b = new byte[2];
// ignore polls (0 message length)
while (l == 0) {
serverIn.readFully(b, 0, 2);
l = ((int) b[0] & 0xFF) << 8 | (int) b[1] & 0xFF;
if (l == 0) {
serverOut.write(b);
serverOut.flush();
Logger.log(new LogEvent(this, "poll"));
}
}
// Message length includes length itself, so adjust the message total down by 2
l = l - 2;
return l;
}
use of org.jpos.util.LogEvent in project jPOS by jpos.
the class ChannelPool method connect.
public synchronized void connect() throws IOException {
current = null;
LogEvent evt = new LogEvent(this, "connect");
evt.addMessage("pool-size=" + Integer.toString(pool.size()));
for (int i = 0; i < pool.size(); i++) {
try {
evt.addMessage("pool-" + Integer.toString(i));
ISOChannel c = (ISOChannel) pool.get(i);
c.connect();
if (c.isConnected()) {
current = c;
usable = true;
break;
}
} catch (IOException e) {
evt.addMessage(e);
}
}
if (current == null)
evt.addMessage("connect failed");
Logger.log(evt);
if (current == null) {
throw new IOException("unable to connect");
}
}
use of org.jpos.util.LogEvent in project jPOS by jpos.
the class ChannelPool method disconnect.
public synchronized void disconnect() throws IOException {
current = null;
LogEvent evt = new LogEvent(this, "disconnect");
for (Object aPool : pool) {
try {
ISOChannel c = (ISOChannel) aPool;
c.disconnect();
} catch (IOException e) {
evt.addMessage(e);
}
}
Logger.log(evt);
}
Aggregations