Class BaseChannel
java.lang.Object
java.util.Observable
org.jpos.iso.BaseChannel
- All Implemented Interfaces:
Cloneable, Configurable, ExceptionHandlerAware, BaseChannelMBean, ClientChannel, FactoryChannel, FilteredChannel, ISOChannel, ISOSource, ServerChannel, ISOMsgMetrics.Source, LogSource
- Direct Known Subclasses:
AmexChannel, ASCIIChannel, BASE24Channel, BASE24TCPChannel, BCDChannel, CMFChannel, CSChannel, GICCChannel, GZIPChannel, HEXChannel, LogChannel, NACChannel, NCCChannel, PADChannel, PostChannel, RawChannel, RBPChannel, TelnetXMLChannel, VAPChannel, X25Channel, XMLChannel
public abstract class BaseChannel
extends Observable
implements FilteredChannel, ClientChannel, ServerChannel, FactoryChannel, ISOMsgMetrics.Source, LogSource, Configurable, BaseChannelMBean, Cloneable, ExceptionHandlerAware
ISOChannel is an abstract class that provides functionality that
allows the transmission and reception of ISO 8583 Messages
over a TCP/IP session.
This class is not thread-safe.
ISOChannel is Observable in order to support GUI components
such as ISOChannelPanel.
It now support the new Logger architecture so we will
probably setup ISOChannelPanel to be a LogListener instead
of being an Observer in future releases.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]protected byte[]protected Loggerprotected Stringprotected booleanprotected ISOPackagerprotected Stringprotected DataInputStreamprotected Lockprotected DataOutputStreamprotected Lockprotected ServerSocketprotected ISOClientSocketFactoryprotected booleanFields inherited from interface ISOChannel
CONNECT, RX, SIZEOF_CNT, TX -
Constructor Summary
ConstructorsConstructorDescriptionconstructor shared by server and client ISOChannels (which have different signatures)BaseChannel(String host, int port, ISOPackager p) constructs a client ISOChannelconstructs a server ISOChannelBaseChannel(ISOPackager p, ServerSocket serverSocket) constructs a server ISOChannel associated with a Server Socket -
Method Summary
Modifier and TypeMethodDescriptionvoidAccepts connectionvoidvoidvoidaddIncomingFilter(ISOFilter filter) voidaddOutgoingFilter(ISOFilter filter) protected ISOMsgapplyIncomingFilters(ISOMsg m, byte[] header, byte[] image, LogEvent evt) protected ISOMsgapplyIncomingFilters(ISOMsg m, LogEvent evt) protected ISOMsgapplyOutgoingFilters(ISOMsg m, LogEvent evt) protected voidsets timeout, and also keep aliveclone()Expose channel clonning interfaceprotected voidvoidconnect()Connects client ISOChannel to serverprotected voidsetup I/O Streams from socketprotected ISOMsgprotected ISOMsgsupport old factory method name for backward compatibilityvoiddisconnects the TCP/IP session.intgetBytes(byte[] b) Low level receivestatic ISOChannelgetChannel(String name) int[]protected ISOHeadergetDynamicHeader(byte[] image) Allow subclasses to override the Default header on incoming messages.protected ISOPackagergetDynamicPackager(byte[] image) allow subclasses to override default packager on incoming messagesprotected ISOPackagergetDynamicPackager(byte[] header, byte[] image) allow subclasses to override default packager on incoming messagesprotected ISOPackagerallow subclasses to override default packager on outgoing messagesMap<Class<? extends Exception>, List<ExceptionHandler>> byte[]protected intprotected intgetHeaderLength(byte[] b) protected intgetHost()intprotected voidgetMessage(byte[] b, int offset, int len) protected intprotected voidRead some trailer data from this channel and optionally store it in the incoming ISOMsg.protected voidDeprecated.use getMessageTrailer(ISOMsg m) instead.getName()intgetPort()getRealm()Gets the ISOClientSocketFactory (may be null)intintprotected voidprotected voidbooleanbooleanbooleanprotected booleanisRejected(byte[] b) booleanprotected Socketprotected Socketfactory method pattern (as suggested by Vincent.Greene@amo.com)protected byte[]protected voidprotected byte[]readHeader(int hLen) Reads in a message header.receive()Waits and receive an ISOMsg over the TCP/IP sessionvoidIssues a disconnect followed by a connectvoidremoveFilter(ISOFilter filter) voidremoveFilter(ISOFilter filter, int direction) voidremoveIncomingFilter(ISOFilter filter) voidremoveOutgoingFilter(ISOFilter filter) voidreset stat infovoidsend(byte[] b) sends a byte[] over the TCP/IP sessionvoidSends the specifiedISOMsgover this ISOChannel.voidSends a high-level keep-alive message (zero length)protected voidsendMessage(byte[] b, int offset, int len) protected voidsendMessageHeader(ISOMsg m, int len) protected voidsendMessageLength(int len) protected voidsendMessageTrailer(ISOMsg m, byte[] b) Send a Message trailer.protected voidsendMessageTrailler(ISOMsg m, byte[] b) Deprecated.use sendMessageTrailer(ISOMsg m, byte[] b instead.protected voidsendMessageTrailler(ISOMsg m, int len) Deprecated.use sendMessageTrailer(ISOMsg m, byte[] b) instead.voidImplements 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)voidsetHeader(byte[] header) voidvoidvoidinitialize an ISOChannelvoidsetIncomingFilters(Collection filters) voidsetISOMsgMetrics(ISOMsgMetrics metrics) voidsetLocalAddress(String iface, int port) initialize an ISOChannelvoidvoidsetMaxPacketLength(int maxPacketLength) voidassociates this ISOChannel with a name using NameRegistrarvoidsetOutgoingFilters(Collection filters) voidsetOverrideHeader(boolean overrideHeader) voidset Packager for channelvoidsetPort(int port) voidsetServerSocket(ServerSocket sock) Associates this ISOChannel with a server socketvoidsetSocketFactory(ISOClientSocketFactory socketFactory) Sets the specified Socket Factory to create socketsvoidsetSoLinger(boolean on, int linger) Socket SO_LINGER option to use when closing the socket.voidsetTimeout(int timeout) sets socket timeout (as suggested by Leonard Thomas invalid input: '<'leonard@rhinosystemsinc.com>)voidsetUsable(boolean b) protected booleanshouldIgnore(byte[] b) protected byte[]protected voidMethods inherited from class Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChangedMethods inherited from class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ExceptionHandlerAware
addHandler, addHandler, handle, removeHandler, removeHandler, removeHandlers
-
Field Details
-
usable
-
overrideHeader
-
serverIn
-
serverOut
-
serverInLock
-
serverOutLock
-
packager
-
serverSocket
-
incomingFilters
-
outgoingFilters
-
socketFactory
-
cnt
-
logger
-
realm
-
originalRealm
-
header
-
-
Constructor Details
-
BaseChannel
public BaseChannel()constructor shared by server and client ISOChannels (which have different signatures) -
BaseChannel
constructs a client ISOChannel- Parameters:
host- server TCP Addressport- server port numberp- an ISOPackager- See Also:
-
BaseChannel
constructs a server ISOChannel- Parameters:
p- an ISOPackager- Throws:
IOException- on error- See Also:
-
BaseChannel
constructs a server ISOChannel associated with a Server Socket- Parameters:
p- an ISOPackagerserverSocket- where to accept a connection- Throws:
IOException- on error- See Also:
-
-
Method Details
-
setHost
initialize an ISOChannel- Specified by:
setHostin interfaceClientChannel- Parameters:
host- server TCP Addressport- server port number
-
setLocalAddress
initialize an ISOChannel- Parameters:
iface- server TCP Addressport- server port number
-
setHost
- Specified by:
setHostin interfaceBaseChannelMBean- Parameters:
host- to connect (client ISOChannel)
-
setPort
- Specified by:
setPortin interfaceBaseChannelMBean- Parameters:
port- to connect (client ISOChannel)
-
getHost
- Specified by:
getHostin interfaceBaseChannelMBean- Specified by:
getHostin interfaceClientChannel- Returns:
- hostname (may be null)
-
getPort
- Specified by:
getPortin interfaceBaseChannelMBean- Specified by:
getPortin interfaceClientChannel- Returns:
- port number
-
setPackager
set Packager for channel- Specified by:
setPackagerin interfaceISOChannel- Parameters:
p- an ISOPackager- See Also:
-
getPackager
- Specified by:
getPackagerin interfaceISOChannel- Returns:
- current packager
-
setServerSocket
Associates this ISOChannel with a server socket- Parameters:
sock- where to accept a connection
-
getExceptionHandlers
- Specified by:
getExceptionHandlersin interfaceExceptionHandlerAware- Returns:
- A map of exception classes to exception handlers. These handlers only execute if the exception matches.
-
resetCounters
reset stat info -
getCounters
- Returns:
- counters
-
isConnected
- Specified by:
isConnectedin interfaceBaseChannelMBean- Specified by:
isConnectedin interfaceISOChannel- Specified by:
isConnectedin interfaceISOSource- Returns:
- the connection state
-
setISOMsgMetrics
- Specified by:
setISOMsgMetricsin interfaceISOMsgMetrics.Source
-
getISOMsgMetrics
- Specified by:
getISOMsgMetricsin interfaceISOMsgMetrics.Source
-
connect
setup I/O Streams from socket- Parameters:
socket- a Socket (client or server)- Throws:
IOException- on error
-
postConnectHook
- Throws:
IOException
-
newSocket
factory method pattern (as suggested by Vincent.Greene@amo.com)- Parameters:
host- remote hostport- 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:
-
newSocket
- Throws:
IOException
-
getSocket
-
getServerSocket
- Returns:
- current serverSocket
-
setTimeout
sets socket timeout (as suggested by Leonard Thomas invalid input: '<'leonard@rhinosystemsinc.com>)- Parameters:
timeout- in milliseconds- Throws:
SocketException- on error
-
getTimeout
-
applyTimeout
sets timeout, and also keep alive- Throws:
SocketException
-
setSoLinger
Socket SO_LINGER option to use when closing the socket.- See Also:
-
isSoLingerOn
-
getSoLingerSeconds
-
connect
Connects client ISOChannel to server- Specified by:
connectin interfaceBaseChannelMBean- Specified by:
connectin interfaceISOChannel- Throws:
IOException
-
accept
Accepts connection- Specified by:
acceptin interfaceServerChannel- Throws:
IOException
-
setUsable
- Specified by:
setUsablein interfaceISOChannel- Parameters:
b- - new Usable state (used by ISOMUX internals to flag as unusable in order to force a reconnection)
-
getDynamicPackager
allow subclasses to override default packager on outgoing messages- Parameters:
m- outgoing ISOMsg- Returns:
- ISOPackager
-
getDynamicPackager
allow subclasses to override default packager on incoming messages- Parameters:
image- incoming message image- Returns:
- ISOPackager
-
getDynamicPackager
allow subclasses to override default packager on incoming messages- Parameters:
header- message headerimage- incoming message image- Returns:
- ISOPackager
-
getDynamicHeader
Allow subclasses to override the Default header on incoming messages.- Parameters:
image- message image- Returns:
- ISOHeader instance
-
sendMessageLength
- Throws:
IOException
-
sendMessageHeader
- Throws:
IOException
-
sendMessageTrailler
Deprecated.use sendMessageTrailer(ISOMsg m, byte[] b) instead.- Parameters:
m- a reference to the ISOMsglen- the packed image length- Throws:
IOException- on error
-
sendMessageTrailler
Deprecated.use sendMessageTrailer(ISOMsg m, byte[] b instead.- Throws:
IOException
-
sendMessageTrailer
Send a Message trailer.- Parameters:
m- The unpacked ISOMsg.b- The packed ISOMsg image.- Throws:
IOException
-
getMessageTrailler
Deprecated.use getMessageTrailer(ISOMsg m) instead.- Throws:
IOException
-
getMessageTrailer
Read some trailer data from this channel and optionally store it in the incoming ISOMsg.- Parameters:
m- The ISOMessage to store the trailer data.- Throws:
IOException
-
getMessage
- Throws:
IOExceptionISOException
-
getMessageLength
- Throws:
IOExceptionISOException
-
getHeaderLength
-
getHeaderLength
-
getHeaderLength
-
streamReceive
- Throws:
IOException
-
sendMessage
- Throws:
IOException
-
send
Sends the specifiedISOMsgover this ISOChannel.This method performs the following steps:
- Verifies the channel is connected.
- Sets the message direction to
ISOMsg.OUTGOING. - Retrieves and sets a dynamic packager for the message.
- Applies all registered outgoing filters, allowing them to modify or veto the message.
- Packs the message and writes its length, header, body, and trailer to the underlying stream, protected by a locking mechanism to ensure thread safety.
- Flushes the output stream and increments message counters.
- Notifies observers of the sent message.
- Logs both the message and the send operation through
ChannelEventandLogEvent.
ISOFilter.VetoExceptionis thrown by a filter, the message is not sent, and the exception is logged.- Specified by:
sendin interfaceISOChannel- Specified by:
sendin interfaceISOSource- Parameters:
m- the ISO message to be sent. The message will be modified in-place: its direction and packager will be updated, and filters may alter its content.- Throws:
IOException- if the channel is not connected, if the output stream fails, if locking times out, or if an unexpected I/O error occurs.ISOException- if packing the message fails or other ISO-specific issues occur.ISOFilter.VetoException- if an outgoing filter vetoes the message.- See Also:
-
send
sends a byte[] over the TCP/IP session- Specified by:
sendin interfaceISOChannel- Parameters:
b- the byte array to be sent- Throws:
IOExceptionISOException
-
sendKeepAlive
Sends a high-level keep-alive message (zero length)- Throws:
IOException- on exception
-
isExpectKeepAlive
-
isRejected
-
shouldIgnore
-
createMsg
-
createISOMsg
-
readHeader
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
Waits and receive an ISOMsg over the TCP/IP session- Specified by:
receivein interfaceISOChannel- Returns:
- the Message received
- Throws:
IOExceptionISOException
-
getBytes
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
disconnects the TCP/IP session. The instance is ready for a reconnection. There is no need to create a new ISOChannel- Specified by:
disconnectin interfaceBaseChannelMBean- Specified by:
disconnectin interfaceISOChannel- Throws:
IOException
-
reconnect
Issues a disconnect followed by a connect- Specified by:
reconnectin interfaceBaseChannelMBean- Specified by:
reconnectin interfaceISOChannel- Throws:
IOException
-
setLogger
-
getRealm
-
getLogger
-
getOriginalRealm
-
setName
associates this ISOChannel with a name using NameRegistrar- Specified by:
setNamein interfaceISOChannel- Parameters:
name- name to register- See Also:
-
getName
- Specified by:
getNamein interfaceISOChannel- Returns:
- this ISOChannel's name ("" if no name was set)
-
addFilter
-
addIncomingFilter
- Specified by:
addIncomingFilterin interfaceFilteredChannel- Parameters:
filter- incoming filter to add
-
addOutgoingFilter
- Specified by:
addOutgoingFilterin interfaceFilteredChannel- Parameters:
filter- outgoing filter to add
-
addFilter
- Specified by:
addFilterin interfaceFilteredChannel- Parameters:
filter- filter to add (both directions, incoming/outgoing)
-
removeFilter
- Parameters:
filter- filter to removedirection- ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both
-
removeFilter
- Specified by:
removeFilterin interfaceFilteredChannel- Parameters:
filter- filter to remove (both directions)
-
removeIncomingFilter
- Specified by:
removeIncomingFilterin interfaceFilteredChannel- Parameters:
filter- incoming filter to remove
-
removeOutgoingFilter
- Specified by:
removeOutgoingFilterin interfaceFilteredChannel- Parameters:
filter- outgoing filter to remove
-
applyOutgoingFilters
- Throws:
ISOFilter.VetoException
-
applyIncomingFilters
- Throws:
ISOFilter.VetoException
-
applyIncomingFilters
protected ISOMsg applyIncomingFilters(ISOMsg m, byte[] header, byte[] image, LogEvent evt) throws ISOFilter.VetoException - Throws:
ISOFilter.VetoException
-
unpack
- Throws:
ISOException
-
pack
- Throws:
ISOException
-
setConfiguration
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)
- Specified by:
setConfigurationin interfaceConfigurable- Parameters:
cfg- Configuration- Throws:
ConfigurationException
-
getConfiguration
-
getIncomingFilters
- Specified by:
getIncomingFiltersin interfaceFilteredChannel- Returns:
- Collection containing all incoming filters
-
getOutgoingFilters
- Specified by:
getOutgoingFiltersin interfaceFilteredChannel- Returns:
- Collection containing all outgoing filters
-
setIncomingFilters
- Specified by:
setIncomingFiltersin interfaceFilteredChannel- Parameters:
filters- incoming filter set
-
setOutgoingFilters
- Specified by:
setOutgoingFiltersin interfaceFilteredChannel- Parameters:
filters- outgoing filter set
-
setHeader
-
setHeader
-
getHeader
-
setOverrideHeader
-
isOverrideHeader
-
getChannel
- Parameters:
name- the Channel's name (without the "channel." prefix)- Returns:
- ISOChannel instance with given name.
- Throws:
NameRegistrar.NotFoundException- See Also:
-
getSocketFactory
Gets the ISOClientSocketFactory (may be null)- Returns:
- ISOClientSocketFactory
- Since:
- 1.3.3 \
- See Also:
-
setSocketFactory
Sets the specified Socket Factory to create sockets- Specified by:
setSocketFactoryin interfaceFactoryChannel- Parameters:
socketFactory- the ISOClientSocketFactory- Since:
- 1.3.3
- See Also:
-
getMaxPacketLength
-
setMaxPacketLength
-
closeSocket
- Throws:
IOException
-
clone
Description copied from interface:ISOChannelExpose channel clonning interface- Specified by:
clonein interfaceISOChannel- Overrides:
clonein classObject
-
incrementMsgInCounter
- Throws:
ISOException
-
incrementMsgOutCounter
- Throws:
ISOException
-