com.solab.iso8583
Class MessageFactory

java.lang.Object
  extended by com.solab.iso8583.MessageFactory

public class MessageFactory
extends Object

This class is used to create messages, either from scratch or from an existing String or byte buffer. It can be configured to put default values on newly created messages, and also to know what to expect when reading messages from an InputStream.

The factory can be configured to know what values to set for newly created messages, both from a template (useful for fields that must be set with the same value for EVERY message created) and individually (for trace [field 11] and message date [field 7]).

It can also be configured to know what fields to expect in incoming messages (all possible values must be stated, indicating the date type for each). This way the messages can be parsed from a byte buffer.

Author:
Enrique Zamudio

Field Summary
protected  Logger log
           
 
Constructor Summary
MessageFactory()
           
 
Method Summary
 void addMessageTemplate(IsoMessage templ)
          Adds a message template to the factory.
 IsoMessage createResponse(IsoMessage request)
          Creates a message to respond to a request.
 boolean getAssignDate()
          Returns true if the factory is assigning the current date to newly created messages (field 7).
 CustomField<?> getCustomField(int index)
          Returns a custom field encoder/decoder for the specified field number, if one is available.
 CustomField<?> getCustomField(Integer index)
          Returns a custom field encoder/decoder for the specified field number, if one is available.
 int getEtx()
           
 String getIsoHeader(int type)
          Returns the ISO header used for the specified type.
 IsoMessage getMessageTemplate(int type)
          Returns the template for the specified message type.
 TraceNumberGenerator getTraceNumberGenerator()
          Returns the generator used to assign trace numbers to new messages.
 boolean getUseBinaryMessages()
          Returns true is the factory is set to create and parse binary messages, false if it uses ASCII messages.
 IsoMessage newMessage(int type)
          Creates a new message of the specified type, with optional trace and date values as well as any other values specified in a message template.
 IsoMessage parseMessage(byte[] buf, int isoHeaderLength)
          Creates a new message instance from the buffer, which must contain a valid ISO8583 message.
 void removeMessageTemplate(int type)
          Removes the message template for the specified type.
 void setAssignDate(boolean flag)
          Sets whether the factory should set the current date on newly created messages, in field 7.
 void setConfigPath(String path)
          Tells the receiver to read the configuration at the specified path.
 void setCustomField(int index, CustomField<?> value)
          Sets the CustomField encoder for the specified field number.
 void setCustomFields(Map<Integer,CustomField<?>> value)
          Specifies a map for custom field encoder/decoders.
 void setEtx(int value)
          Sets the ETX character to be sent at the end of the message.
 void setIsoHeader(int type, String value)
          Sets the ISO header for a specific message type.
 void setIsoHeaders(Map<Integer,String> value)
          Sets the ISO header to be used in each message type.
 void setParseMap(int type, Map<Integer,FieldParseInfo> map)
          Sets a map with the fields that are to be expected when parsing a certain type of message.
 void setTraceNumberGenerator(TraceNumberGenerator value)
          Sets the generator that this factory will get new trace numbers from.
 void setUseBinaryMessages(boolean flag)
          Tells the receiver to create and parse binary messages if the flag is true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final Logger log
Constructor Detail

MessageFactory

public MessageFactory()
Method Detail

setCustomFields

public void setCustomFields(Map<Integer,CustomField<?>> value)
Specifies a map for custom field encoder/decoders. The keys are the field numbers.


setCustomField

public void setCustomField(int index,
                           CustomField<?> value)
Sets the CustomField encoder for the specified field number.


getCustomField

public CustomField<?> getCustomField(int index)
Returns a custom field encoder/decoder for the specified field number, if one is available.


getCustomField

public CustomField<?> getCustomField(Integer index)
Returns a custom field encoder/decoder for the specified field number, if one is available.


setConfigPath

public void setConfigPath(String path)
                   throws IOException
Tells the receiver to read the configuration at the specified path. This just calls ConfigParser.configureFromClasspathConfig() with itself and the specified path at arguments, but is really convenient in case the MessageFactory is being configured from within, say, Spring.

Throws:
IOException

setUseBinaryMessages

public void setUseBinaryMessages(boolean flag)
Tells the receiver to create and parse binary messages if the flag is true. Default is false, that is, create and parse ASCII messages.


getUseBinaryMessages

public boolean getUseBinaryMessages()
Returns true is the factory is set to create and parse binary messages, false if it uses ASCII messages. Default is false.


setEtx

public void setEtx(int value)
Sets the ETX character to be sent at the end of the message. This is optional and the default is -1, which means nothing should be sent as terminator.

Parameters:
value - The ASCII value of the ETX character or -1 to indicate no terminator should be used.

getEtx

public int getEtx()

newMessage

public IsoMessage newMessage(int type)
Creates a new message of the specified type, with optional trace and date values as well as any other values specified in a message template. If the factory is set to use binary messages, then the returned message will be written using binary coding.

Parameters:
type - The message type, for example 0x200, 0x400, etc.

createResponse

public IsoMessage createResponse(IsoMessage request)
Creates a message to respond to a request. Increments the message type by 16, sets all fields from the template if there is one, and copies all values from the request, overwriting fields from the template if they overlap.

Parameters:
request - An ISO8583 message with a request type (ending in 00).

parseMessage

public IsoMessage parseMessage(byte[] buf,
                               int isoHeaderLength)
                        throws ParseException
Creates a new message instance from the buffer, which must contain a valid ISO8583 message. If the factory is set to use binary messages then it will try to parse a binary message.

Parameters:
buf - The byte buffer containing the message. Must not include the length header.
isoHeaderLength - The expected length of the ISO header, after which the message type and the rest of the message must come.
Throws:
ParseException

setAssignDate

public void setAssignDate(boolean flag)
Sets whether the factory should set the current date on newly created messages, in field 7. Default is false.


getAssignDate

public boolean getAssignDate()
Returns true if the factory is assigning the current date to newly created messages (field 7). Default is false.


setTraceNumberGenerator

public void setTraceNumberGenerator(TraceNumberGenerator value)
Sets the generator that this factory will get new trace numbers from. There is no default generator.


getTraceNumberGenerator

public TraceNumberGenerator getTraceNumberGenerator()
Returns the generator used to assign trace numbers to new messages.


setIsoHeaders

public void setIsoHeaders(Map<Integer,String> value)
Sets the ISO header to be used in each message type.

Parameters:
value - A map where the keys are the message types and the values are the ISO headers.

setIsoHeader

public void setIsoHeader(int type,
                         String value)
Sets the ISO header for a specific message type.

Parameters:
type - The message type, for example 0x200.
value - The ISO header, or NULL to remove any headers for this message type.

getIsoHeader

public String getIsoHeader(int type)
Returns the ISO header used for the specified type.


addMessageTemplate

public void addMessageTemplate(IsoMessage templ)
Adds a message template to the factory. If there was a template for the same message type as the new one, it is overwritten.


removeMessageTemplate

public void removeMessageTemplate(int type)
Removes the message template for the specified type.


getMessageTemplate

public IsoMessage getMessageTemplate(int type)
Returns the template for the specified message type. This allows templates to be modified programatically.


setParseMap

public void setParseMap(int type,
                        Map<Integer,FieldParseInfo> map)
Sets a map with the fields that are to be expected when parsing a certain type of message.

Parameters:
type - The message type.
map - A map of FieldParseInfo instances, each of which define what type and length of field to expect. The keys will be the field numbers.