From 3feb4a7704652b49a68a245d47f1fb5c611548e4 Mon Sep 17 00:00:00 2001 From: seanjburns Date: Tue, 27 Nov 2012 23:04:59 +0200 Subject: [PATCH] Very simple change to allow the factory to create other implementations of IsoMessage --- .../java/com/solab/iso8583/IsoMessage.java | 2 +- .../com/solab/iso8583/MessageFactory.java | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/solab/iso8583/IsoMessage.java b/src/main/java/com/solab/iso8583/IsoMessage.java index e610853..b4480a2 100644 --- a/src/main/java/com/solab/iso8583/IsoMessage.java +++ b/src/main/java/com/solab/iso8583/IsoMessage.java @@ -57,7 +57,7 @@ public class IsoMessage { } /** Creates a new message with the specified ISO header. This will be prepended to the message. */ - IsoMessage(String header) { + protected IsoMessage(String header) { isoHeader = header; } diff --git a/src/main/java/com/solab/iso8583/MessageFactory.java b/src/main/java/com/solab/iso8583/MessageFactory.java index 18b2e0d..e312d49 100644 --- a/src/main/java/com/solab/iso8583/MessageFactory.java +++ b/src/main/java/com/solab/iso8583/MessageFactory.java @@ -49,12 +49,12 @@ import com.solab.iso8583.parse.FieldParseInfo; * * @author Enrique Zamudio */ -public class MessageFactory { +public class MessageFactory { protected final Logger log = LoggerFactory.getLogger(getClass()); /** This map stores the message template for each message type. */ - private Map typeTemplates = new HashMap(); + private Map typeTemplates = new HashMap(); /** Stores the information needed to parse messages sorted by type. */ private Map> parseMap = new HashMap>(); /** Stores the field numbers to be parsed, in order of appearance. */ @@ -169,8 +169,8 @@ public class MessageFactory { * 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. * @param type The message type, for example 0x200, 0x400, etc. */ - public IsoMessage newMessage(int type) { - IsoMessage m = new IsoMessage(isoHeaders.get(type)); + public T newMessage(int type) { + T m = createIsoMessage(isoHeaders.get(type)); m.setType(type); m.setEtx(etx); m.setBinary(useBinary); @@ -201,8 +201,8 @@ public class MessageFactory { * 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. * @param request An ISO8583 message with a request type (ending in 00). */ - public IsoMessage createResponse(IsoMessage request) { - IsoMessage resp = new IsoMessage(isoHeaders.get(request.getType() + 16)); + public T createResponse(T request) { + T resp = createIsoMessage(isoHeaders.get(request.getType() + 16)); resp.setCharacterEncoding(request.getCharacterEncoding()); resp.setBinary(request.isBinary()); resp.setType(request.getType() + 16); @@ -234,13 +234,13 @@ public class MessageFactory { * @param buf The byte buffer containing the message. Must not include the length header. * @param isoHeaderLength The expected length of the ISO header, after which the message type * and the rest of the message must come. */ - public IsoMessage parseMessage(byte[] buf, int isoHeaderLength) + public T parseMessage(byte[] buf, int isoHeaderLength) throws ParseException, UnsupportedEncodingException { final int minlength = isoHeaderLength+(useBinary ? 10 : 20); if (buf.length < minlength) { throw new ParseException("Insufficient buffer length, needs to be at least " + minlength, 0); } - final IsoMessage m = new IsoMessage(isoHeaderLength > 0 ? new String(buf, 0, isoHeaderLength) : null); + final T m = createIsoMessage(isoHeaderLength > 0 ? new String(buf, 0, isoHeaderLength) : null); m.setCharacterEncoding(encoding); int type = 0; if (useBinary) { @@ -405,7 +405,16 @@ public class MessageFactory { return m; } - /** Sets whether the factory should set the current date on newly created messages, + /** Creates a Iso message, override this method in the subclass to provide your + * own implementations of IsoMessage. + * @param header + * @return IsoMessage + */ + protected T createIsoMessage(String header) { + return (T) new IsoMessage(header); + } + + /** Sets whether the factory should set the current date on newly created messages, * in field 7. Default is false. */ public void setAssignDate(boolean flag) { setDate = flag; @@ -452,7 +461,7 @@ public class MessageFactory { /** Adds a message template to the factory. If there was a template for the same * message type as the new one, it is overwritten. */ - public void addMessageTemplate(IsoMessage templ) { + public void addMessageTemplate(T templ) { if (templ != null) { typeTemplates.put(templ.getType(), templ); } @@ -465,7 +474,7 @@ public class MessageFactory { /** Returns the template for the specified message type. This allows templates to be modified * programatically. */ - public IsoMessage getMessageTemplate(int type) { + public T getMessageTemplate(int type) { return typeTemplates.get(type); }