org.jpos.iso
Class BaseChannel

java.lang.Object
  extended by java.util.Observable
      extended by org.jpos.iso.BaseChannel
All Implemented Interfaces:
Cloneable, Configurable, BaseChannelMBean, ClientChannel, FactoryChannel, FilteredChannel, ISOChannel, ISOSource, ServerChannel, LogSource
Direct Known Subclasses:
AmexChannel, ASCIIChannel, BASE24Channel, BASE24TCPChannel, BCDChannel, CSChannel, GZIPChannel, HEXChannel, LogChannel, NACChannel, NCCChannel, PADChannel, PostChannel, RawChannel, RBPChannel, TelnetXMLChannel, VAPChannel, X25Channel, XMLChannel

public abstract class BaseChannel
extends Observable
implements FilteredChannel, ClientChannel, ServerChannel, FactoryChannel, LogSource, Configurable, BaseChannelMBean, Cloneable

ISOChannel is an abstract class that provides functionality that allows the transmision and reception of ISO 8583 Messages over a TCP/IP session.

This class is not thread-safe.

ISOChannel is Observable in order to suport GUI components such as ISOChannelPanel.
It now support the new Logger architecture so we will probably setup ISOChannelPanel to be a LogListener insteado of being an Observer in future releases.

Version:
$Revision$ $Date$
Author:
Alejandro P. Revilla, Bharavi Gade
See Also:
ISOMsg, MUX, ISOException, CSChannel, Logger

Field Summary
protected  int[] cnt
           
protected  byte[] header
           
protected  Vector incomingFilters
           
protected  Logger logger
           
protected  String originalRealm
           
protected  Vector outgoingFilters
           
protected  boolean overrideHeader
           
protected  ISOPackager packager
           
protected  String realm
           
protected  DataInputStream serverIn
           
protected  Object serverInLock
           
protected  DataOutputStream serverOut
           
protected  Object serverOutLock
           
protected  ServerSocket serverSocket
           
protected  ISOClientSocketFactory socketFactory
           
protected  boolean usable
           
 
Fields inherited from interface org.jpos.iso.ISOChannel
CONNECT, RX, SIZEOF_CNT, TX
 
Constructor Summary
BaseChannel()
          constructor shared by server and client ISOChannels (which have different signatures)
BaseChannel(ISOPackager p)
          constructs a server ISOChannel
BaseChannel(ISOPackager p, ServerSocket serverSocket)
          constructs a server ISOChannel associated with a Server Socket
BaseChannel(String host, int port, ISOPackager p)
          constructs a client ISOChannel
 
Method Summary
 void accept(ServerSocket s)
          Accepts connection
 void addFilter(ISOFilter filter)
           
 void addFilter(ISOFilter filter, int direction)
           
 void addIncomingFilter(ISOFilter filter)
           
 void addOutgoingFilter(ISOFilter filter)
           
protected  ISOMsg applyIncomingFilters(ISOMsg m, byte[] header, byte[] image, LogEvent evt)
           
protected  ISOMsg applyIncomingFilters(ISOMsg m, LogEvent evt)
           
protected  ISOMsg applyOutgoingFilters(ISOMsg m, LogEvent evt)
           
protected  void applyTimeout()
           
 Object clone()
          Expose channel clonning interface
 void connect()
          Connects client ISOChannel to server
protected  void connect(Socket socket)
          setup I/O Streams from socket
protected  ISOMsg createISOMsg()
           
protected  ISOMsg createMsg()
          support old factory method name for backward compatibility
 void disconnect()
          disconnects the TCP/IP session.
 int getBytes(byte[] b)
          Low level receive
static ISOChannel getChannel(String name)
           
 Configuration getConfiguration()
           
 int[] getCounters()
           
protected  ISOHeader getDynamicHeader(byte[] image)
          Allow subclasses to override the Default header on incoming messages.
protected  ISOPackager getDynamicPackager(byte[] image)
          allow subclasses to override default packager on outgoing messages
protected  ISOPackager getDynamicPackager(byte[] header, byte[] image)
          allow subclasses to override default packager on outgoing messages
protected  ISOPackager getDynamicPackager(ISOMsg m)
          allow subclasses to override default packager on outgoing messages
 byte[] getHeader()
           
protected  int getHeaderLength()
           
protected  int getHeaderLength(byte[] b)
           
protected  int getHeaderLength(ISOMsg m)
           
 String getHost()
           
 Collection getIncomingFilters()
           
 Logger getLogger()
           
 int getMaxPacketLength()
           
protected  void getMessage(byte[] b, int offset, int len)
           
protected  int getMessageLength()
           
protected  void getMessageTrailler()
           
 String getName()
           
 String getOriginalRealm()
           
 Collection getOutgoingFilters()
           
 ISOPackager getPackager()
           
 int getPort()
           
 String getRealm()
           
 ServerSocket getServerSocket()
           
 Socket getSocket()
           
 ISOClientSocketFactory getSocketFactory()
          Gets the ISOClientSocketFactory (may be null)
 int getTimeout()
           
 boolean isConnected()
           
 boolean isOverrideHeader()
           
protected  boolean isRejected(byte[] b)
           
protected  Socket newSocket(String[] hosts, int[] ports, LogEvent evt)
           
protected  Socket newSocket(String host, int port)
          factory method pattern (as suggested by Vincent.Greene@amo.com)
protected  void postConnectHook()
           
protected  byte[] readHeader(int hLen)
          Reads in a message header.
 ISOMsg receive()
          Waits and receive an ISOMsg over the TCP/IP session
 void reconnect()
          Issues a disconnect followed by a connect
 void removeFilter(ISOFilter filter)
           
 void removeFilter(ISOFilter filter, int direction)
           
 void removeIncomingFilter(ISOFilter filter)
           
 void removeOutgoingFilter(ISOFilter filter)
           
 void resetCounters()
          reset stat info
 void send(byte[] b)
          sends a byte[] over the TCP/IP session
 void send(ISOMsg m)
          sends an ISOMsg over the TCP/IP session
 void sendKeepAlive()
          Sends a high-level keep-alive message (zero length)
protected  void sendMessage(byte[] b, int offset, int len)
           
protected  void sendMessageHeader(ISOMsg m, int len)
           
protected  void sendMessageLength(int len)
           
protected  void sendMessageTrailler(ISOMsg m, byte[] b)
           
protected  void sendMessageTrailler(ISOMsg m, int len)
          Deprecated. use sendMessageTrailler(ISOMsg m, byte[] b) instead.
 void setConfiguration(Configuration cfg)
          Implements Configurable
Properties:
host - destination host (if ClientChannel) port - port number (if ClientChannel) local-iface - local interfase to use (if ClientChannel) local-port - local port to bind (if ClientChannel) (host not present indicates a ServerChannel)
 void setHeader(byte[] header)
           
 void setHeader(String header)
           
 void setHost(String host)
           
 void setHost(String host, int port)
          initialize an ISOChannel
 void setIncomingFilters(Collection filters)
           
 void setLocalAddress(String iface, int port)
          initialize an ISOChannel
 void setLogger(Logger logger, String realm)
           
 void setMaxPacketLength(int maxPacketLength)
           
 void setName(String name)
          associates this ISOChannel with a name using NameRegistrar
 void setOutgoingFilters(Collection filters)
           
 void setOverrideHeader(boolean overrideHeader)
           
 void setPackager(ISOPackager p)
          set Packager for channel
 void setPort(int port)
           
 void setServerSocket(ServerSocket sock)
          Associates this ISOChannel with a server socket
 void setSocketFactory(ISOClientSocketFactory socketFactory)
          Sets the specified Socket Factory to create sockets
 void setTimeout(int timeout)
          sets socket timeout (as suggested by Leonard Thomas )
 void setUsable(boolean b)
           
protected  boolean shouldIgnore(byte[] b)
           
protected  byte[] streamReceive()
           
protected  void unpack(ISOMsg m, byte[] b)
           
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

usable

protected boolean usable

overrideHeader

protected boolean overrideHeader

serverIn

protected DataInputStream serverIn

serverOut

protected DataOutputStream serverOut

serverInLock

protected Object serverInLock

serverOutLock

protected Object serverOutLock

packager

protected ISOPackager packager

serverSocket

protected ServerSocket serverSocket

incomingFilters

protected Vector incomingFilters

outgoingFilters

protected Vector outgoingFilters

socketFactory

protected ISOClientSocketFactory socketFactory

cnt

protected int[] cnt

logger

protected Logger logger

realm

protected String realm

originalRealm

protected String originalRealm

header

protected byte[] header
Constructor Detail

BaseChannel

public BaseChannel()
constructor shared by server and client ISOChannels (which have different signatures)


BaseChannel

public BaseChannel(String host,
                   int port,
                   ISOPackager p)
constructs a client ISOChannel

Parameters:
host - server TCP Address
port - server port number
p - an ISOPackager
See Also:
ISOPackager

BaseChannel

public BaseChannel(ISOPackager p)
            throws IOException
constructs a server ISOChannel

Parameters:
p - an ISOPackager
Throws:
IOException - on error
See Also:
ISOPackager

BaseChannel

public BaseChannel(ISOPackager p,
                   ServerSocket serverSocket)
            throws IOException
constructs a server ISOChannel associated with a Server Socket

Parameters:
p - an ISOPackager
serverSocket - where to accept a connection
Throws:
IOException - on error
See Also:
ISOPackager
Method Detail

setHost

public void setHost(String host,
                    int port)
initialize an ISOChannel

Specified by:
setHost in interface ClientChannel
Parameters:
host - server TCP Address
port - server port number

setLocalAddress

public void setLocalAddress(String iface,
                            int port)
initialize an ISOChannel

Parameters:
iface - server TCP Address
port - server port number

setHost

public void setHost(String host)
Specified by:
setHost in interface BaseChannelMBean
Parameters:
host - to connect (client ISOChannel)

setPort

public void setPort(int port)
Specified by:
setPort in interface BaseChannelMBean
Parameters:
port - to connect (client ISOChannel)

getHost

public String getHost()
Specified by:
getHost in interface BaseChannelMBean
Specified by:
getHost in interface ClientChannel
Returns:
hostname (may be null)

getPort

public int getPort()
Specified by:
getPort in interface BaseChannelMBean
Specified by:
getPort in interface ClientChannel
Returns:
port number

setPackager

public void setPackager(ISOPackager p)
set Packager for channel

Specified by:
setPackager in interface ISOChannel
Parameters:
p - an ISOPackager
See Also:
ISOPackager

getPackager

public ISOPackager getPackager()
Specified by:
getPackager in interface ISOChannel
Returns:
current packager

setServerSocket

public void setServerSocket(ServerSocket sock)
Associates this ISOChannel with a server socket

Parameters:
sock - where to accept a connection

resetCounters

public void resetCounters()
reset stat info


getCounters

public int[] getCounters()
Returns:
counters

isConnected

public boolean isConnected()
Specified by:
isConnected in interface BaseChannelMBean
Specified by:
isConnected in interface ISOChannel
Specified by:
isConnected in interface ISOSource
Returns:
the connection state

connect

protected void connect(Socket socket)
                throws IOException
setup I/O Streams from socket

Parameters:
socket - a Socket (client or server)
Throws:
IOException - on error

postConnectHook

protected void postConnectHook()
                        throws IOException
Throws:
IOException

newSocket

protected Socket newSocket(String host,
                           int port)
                    throws IOException
factory method pattern (as suggested by Vincent.Greene@amo.com)

Parameters:
host - remote host
port - remote port
Returns:
newly created socket
Throws:
IOException - on error Use Socket factory if exists. If it is missing create a normal socket
See Also:
ISOClientSocketFactory

newSocket

protected Socket newSocket(String[] hosts,
                           int[] ports,
                           LogEvent evt)
                    throws IOException
Throws:
IOException

getSocket

public Socket getSocket()
Returns:
current socket

getServerSocket

public ServerSocket getServerSocket()
Returns:
current serverSocket

setTimeout

public void setTimeout(int timeout)
                throws SocketException
sets socket timeout (as suggested by Leonard Thomas )

Parameters:
timeout - in milliseconds
Throws:
SocketException - on error

getTimeout

public int getTimeout()

applyTimeout

protected void applyTimeout()
                     throws SocketException
Throws:
SocketException

connect

public void connect()
             throws IOException
Connects client ISOChannel to server

Specified by:
connect in interface BaseChannelMBean
Specified by:
connect in interface ISOChannel
Throws:
IOException

accept

public void accept(ServerSocket s)
            throws IOException
Accepts connection

Specified by:
accept in interface ServerChannel
Throws:
IOException

setUsable

public void setUsable(boolean b)
Specified by:
setUsable in interface ISOChannel
Parameters:
b - - new Usable state (used by ISOMUX internals to flag as unusable in order to force a reconnection)

getDynamicPackager

protected ISOPackager getDynamicPackager(ISOMsg m)
allow subclasses to override default packager on outgoing messages

Parameters:
m - outgoing ISOMsg
Returns:
ISOPackager

getDynamicPackager

protected ISOPackager getDynamicPackager(byte[] image)
allow subclasses to override default packager on outgoing messages

Parameters:
image - incoming message image
Returns:
ISOPackager

getDynamicPackager

protected ISOPackager getDynamicPackager(byte[] header,
                                         byte[] image)
allow subclasses to override default packager on outgoing messages

Parameters:
header - message header
image - incoming message image
Returns:
ISOPackager

getDynamicHeader

protected ISOHeader getDynamicHeader(byte[] image)
Allow subclasses to override the Default header on incoming messages.

Parameters:
image - message image
Returns:
ISOHeader instance

sendMessageLength

protected void sendMessageLength(int len)
                          throws IOException
Throws:
IOException

sendMessageHeader

protected void sendMessageHeader(ISOMsg m,
                                 int len)
                          throws IOException
Throws:
IOException

sendMessageTrailler

protected void sendMessageTrailler(ISOMsg m,
                                   int len)
                            throws IOException
Deprecated. use sendMessageTrailler(ISOMsg m, byte[] b) instead.

Parameters:
m - a reference to the ISOMsg
len - the packed image length
Throws:
IOException - on error

sendMessageTrailler

protected void sendMessageTrailler(ISOMsg m,
                                   byte[] b)
                            throws IOException
Throws:
IOException

getMessageTrailler

protected void getMessageTrailler()
                           throws IOException
Throws:
IOException

getMessage

protected void getMessage(byte[] b,
                          int offset,
                          int len)
                   throws IOException,
                          ISOException
Throws:
IOException
ISOException

getMessageLength

protected int getMessageLength()
                        throws IOException,
                               ISOException
Throws:
IOException
ISOException

getHeaderLength

protected int getHeaderLength()

getHeaderLength

protected int getHeaderLength(byte[] b)

getHeaderLength

protected int getHeaderLength(ISOMsg m)

streamReceive

protected byte[] streamReceive()
                        throws IOException
Throws:
IOException

sendMessage

protected void sendMessage(byte[] b,
                           int offset,
                           int len)
                    throws IOException
Throws:
IOException

send

public void send(ISOMsg m)
          throws IOException,
                 ISOException
sends an ISOMsg over the TCP/IP session

Specified by:
send in interface ISOChannel
Specified by:
send in interface ISOSource
Parameters:
m - the Message to be sent
Throws:
IOException
ISOException
ISOFilter.VetoException;

send

public void send(byte[] b)
          throws IOException,
                 ISOException
sends a byte[] over the TCP/IP session

Specified by:
send in interface ISOChannel
Parameters:
b - the byte array to be sent
Throws:
IOException
ISOException
ISOFilter.VetoException;

sendKeepAlive

public void sendKeepAlive()
                   throws IOException
Sends a high-level keep-alive message (zero length)

Throws:
IOException - on exception

isRejected

protected boolean isRejected(byte[] b)

shouldIgnore

protected boolean shouldIgnore(byte[] b)

createMsg

protected ISOMsg createMsg()
support old factory method name for backward compatibility

Returns:
newly created ISOMsg

createISOMsg

protected ISOMsg createISOMsg()

readHeader

protected byte[] readHeader(int hLen)
                     throws IOException
Reads in a message header.

Parameters:
hLen - The Length og the reader to read
Returns:
The header bytes that were read in
Throws:
IOException - on error

receive

public ISOMsg receive()
               throws IOException,
                      ISOException
Waits and receive an ISOMsg over the TCP/IP session

Specified by:
receive in interface ISOChannel
Returns:
the Message received
Throws:
IOException
ISOException

getBytes

public int getBytes(byte[] b)
             throws IOException
Low level receive

Parameters:
b - byte array
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - on error

disconnect

public void disconnect()
                throws IOException
disconnects the TCP/IP session. The instance is ready for a reconnection. There is no need to create a new ISOChannel

Specified by:
disconnect in interface BaseChannelMBean
Specified by:
disconnect in interface ISOChannel
Throws:
IOException

reconnect

public void reconnect()
               throws IOException
Issues a disconnect followed by a connect

Specified by:
reconnect in interface BaseChannelMBean
Specified by:
reconnect in interface ISOChannel
Throws:
IOException

setLogger

public void setLogger(Logger logger,
                      String realm)
Specified by:
setLogger in interface LogSource

getRealm

public String getRealm()
Specified by:
getRealm in interface LogSource

getLogger

public Logger getLogger()
Specified by:
getLogger in interface LogSource

getOriginalRealm

public String getOriginalRealm()

setName

public void setName(String name)
associates this ISOChannel with a name using NameRegistrar

Specified by:
setName in interface ISOChannel
Parameters:
name - name to register
See Also:
NameRegistrar

getName

public String getName()
Specified by:
getName in interface ISOChannel
Returns:
this ISOChannel's name ("" if no name was set)

addFilter

public void addFilter(ISOFilter filter,
                      int direction)
Parameters:
filter - filter to add
direction - ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both

addIncomingFilter

public void addIncomingFilter(ISOFilter filter)
Specified by:
addIncomingFilter in interface FilteredChannel
Parameters:
filter - incoming filter to add

addOutgoingFilter

public void addOutgoingFilter(ISOFilter filter)
Specified by:
addOutgoingFilter in interface FilteredChannel
Parameters:
filter - outgoing filter to add

addFilter

public void addFilter(ISOFilter filter)
Specified by:
addFilter in interface FilteredChannel
Parameters:
filter - filter to add (both directions, incoming/outgoing)

removeFilter

public void removeFilter(ISOFilter filter,
                         int direction)
Parameters:
filter - filter to remove
direction - ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both

removeFilter

public void removeFilter(ISOFilter filter)
Specified by:
removeFilter in interface FilteredChannel
Parameters:
filter - filter to remove (both directions)

removeIncomingFilter

public void removeIncomingFilter(ISOFilter filter)
Specified by:
removeIncomingFilter in interface FilteredChannel
Parameters:
filter - incoming filter to remove

removeOutgoingFilter

public void removeOutgoingFilter(ISOFilter filter)
Specified by:
removeOutgoingFilter in interface FilteredChannel
Parameters:
filter - outgoing filter to remove

applyOutgoingFilters

protected ISOMsg applyOutgoingFilters(ISOMsg m,
                                      LogEvent evt)
                               throws ISOFilter.VetoException
Throws:
ISOFilter.VetoException

applyIncomingFilters

protected ISOMsg applyIncomingFilters(ISOMsg m,
                                      LogEvent evt)
                               throws ISOFilter.VetoException
Throws:
ISOFilter.VetoException

applyIncomingFilters

protected ISOMsg applyIncomingFilters(ISOMsg m,
                                      byte[] header,
                                      byte[] image,
                                      LogEvent evt)
                               throws ISOFilter.VetoException
Throws:
ISOFilter.VetoException

unpack

protected void unpack(ISOMsg m,
                      byte[] b)
               throws ISOException
Throws:
ISOException

setConfiguration

public void setConfiguration(Configuration cfg)
                      throws ConfigurationException
Implements Configurable
Properties:
(host not present indicates a ServerChannel)

Specified by:
setConfiguration in interface Configurable
Parameters:
cfg - Configuration
Throws:
ConfigurationException

getConfiguration

public Configuration getConfiguration()

getIncomingFilters

public Collection getIncomingFilters()
Specified by:
getIncomingFilters in interface FilteredChannel
Returns:
Collection containing all incoming filters

getOutgoingFilters

public Collection getOutgoingFilters()
Specified by:
getOutgoingFilters in interface FilteredChannel
Returns:
Collection containing all outgoing filters

setIncomingFilters

public void setIncomingFilters(Collection filters)
Specified by:
setIncomingFilters in interface FilteredChannel
Parameters:
filters - incoming filter set

setOutgoingFilters

public void setOutgoingFilters(Collection filters)
Specified by:
setOutgoingFilters in interface FilteredChannel
Parameters:
filters - outgoing filter set

setHeader

public void setHeader(byte[] header)

setHeader

public void setHeader(String header)

getHeader

public byte[] getHeader()

setOverrideHeader

public void setOverrideHeader(boolean overrideHeader)

isOverrideHeader

public boolean isOverrideHeader()

getChannel

public static ISOChannel getChannel(String name)
                             throws NameRegistrar.NotFoundException
Parameters:
name - the Channel's name (without the "channel." prefix)
Returns:
ISOChannel instance with given name.
Throws:
NameRegistrar.NotFoundException;
NameRegistrar.NotFoundException
See Also:
NameRegistrar

getSocketFactory

public ISOClientSocketFactory getSocketFactory()
Gets the ISOClientSocketFactory (may be null)

Returns:
ISOClientSocketFactory
Since:
1.3.3 \
See Also:
ISOClientSocketFactory

setSocketFactory

public void setSocketFactory(ISOClientSocketFactory socketFactory)
Sets the specified Socket Factory to create sockets

Specified by:
setSocketFactory in interface FactoryChannel
Parameters:
socketFactory - the ISOClientSocketFactory
Since:
1.3.3
See Also:
ISOClientSocketFactory

getMaxPacketLength

public int getMaxPacketLength()

setMaxPacketLength

public void setMaxPacketLength(int maxPacketLength)

clone

public Object clone()
Description copied from interface: ISOChannel
Expose channel clonning interface

Specified by:
clone in interface ISOChannel
Overrides:
clone in class Object


Copyright © 1998-2012 jPOS.org. All Rights Reserved.