KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > IdempotentSecureHashReceiver


1 /*
2  * $Id: IdempotentSecureHashReceiver.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.inbound;
12
13 import org.mule.transformers.simple.ByteArrayToHexString;
14 import org.mule.transformers.simple.SerializableToByteArray;
15 import org.mule.umo.MessagingException;
16 import org.mule.umo.UMOEvent;
17 import org.mule.umo.routing.RoutingException;
18 import org.mule.umo.transformer.TransformerException;
19
20 import java.security.MessageDigest JavaDoc;
21 import java.security.NoSuchAlgorithmException JavaDoc;
22
23 /**
24  * <code>IdempotentSecureHashReceiver</code> ensures that only unique messages are
25  * received by a component. It does this by calculating the SHA-256 hash of the
26  * message itself. This provides a value with an infinitesimally small chance of a
27  * collision. This can be used to filter message duplicates. Please keep in mind that
28  * the hash is calculated over the entire byte array representing the message, so any
29  * leading or trailing spaces or extraneous bytes (like padding) can produce
30  * different hash values for the same semantic message content. Care should be taken
31  * to ensure that messages do not contain extraneous bytes. This class is useful when
32  * the message does not support unique identifiers. This implementation provides for
33  * a persistent store of message hash values via the underlying file system and is
34  * suitable in failover environments.
35  *
36  * @author <a HREF="mailto:rlucente@xecu.net">Rich Lucente</a>
37  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
38  * @version $Revision: 3798 $
39  */

40
41 public class IdempotentSecureHashReceiver extends IdempotentReceiver
42 {
43     private static final String JavaDoc messageDigestAlgorithm = "SHA-256";
44
45     private SerializableToByteArray objectToByteArray = new SerializableToByteArray();
46     private ByteArrayToHexString byteArrayToHexString = new ByteArrayToHexString();
47
48     protected String JavaDoc getIdForEvent(UMOEvent event) throws MessagingException
49     {
50         try
51         {
52             MessageDigest JavaDoc md = MessageDigest.getInstance(messageDigestAlgorithm);
53             return (String JavaDoc)byteArrayToHexString.transform(md.digest((byte[])objectToByteArray.transform(event.getMessage()
54                 .getPayload())));
55         }
56         catch (NoSuchAlgorithmException JavaDoc nsa)
57         {
58             throw new RoutingException(event.getMessage(), event.getEndpoint(), nsa);
59         }
60         catch (TransformerException te)
61         {
62             throw new RoutingException(event.getMessage(), event.getEndpoint(), te);
63         }
64     }
65 }
66
Popular Tags