KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > MessageChunkingRouter


1 /*
2  * $Id: MessageChunkingRouter.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.outbound;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.impl.MuleMessage;
16 import org.mule.umo.UMOMessage;
17 import org.mule.umo.UMOSession;
18 import org.mule.umo.routing.RoutingException;
19
20 /**
21  * A router that breaks up the current message onto smaller parts and sends them to
22  * the same destination. The Destination component needs to have a
23  * MessageChunkingAggregator inbound router in order to rebuild the message at the
24  * other end.
25  *
26  * @see org.mule.routing.inbound.MessageChunkingAggregator
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30 public class MessageChunkingRouter extends FilteringOutboundRouter
31 {
32     private int chunkSize = 0;
33     private int numberOfMessages = 1;
34
35     public int getChunkSize()
36     {
37         return chunkSize;
38     }
39
40     public void setChunkSize(int chunkSize)
41     {
42         this.chunkSize = chunkSize;
43     }
44
45     public int getNumberOfMessages()
46     {
47         return numberOfMessages;
48     }
49
50     public void setNumberOfMessages(int numberOfMessages)
51     {
52         this.numberOfMessages = numberOfMessages;
53     }
54
55     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
56         throws RoutingException
57     {
58         if (chunkSize == 0 && numberOfMessages < 2)
59         {
60             return super.route(message, session, synchronous);
61         }
62         else if (chunkSize > 0)
63         {
64             byte[] data = new byte[0];
65             try
66             {
67                 data = message.getPayloadAsBytes();
68             }
69             catch (Exception JavaDoc e)
70             {
71                 throw new RoutingException(new Message(Messages.FAILED_TO_READ_PAYLOAD), message,
72                     getEndpoint(0, message), e);
73             }
74
75             int parts = data.length / chunkSize;
76             if ((parts * chunkSize) < data.length)
77             {
78                 parts++;
79             }
80             int len = chunkSize;
81             UMOMessage part = null;
82             int count = 0;
83             int pos = 0;
84             byte[] buffer = null;
85             try
86             {
87                 for (; count < parts; count++)
88                 {
89                     if ((pos + len) > data.length)
90                     {
91                         len = data.length - pos;
92                     }
93                     buffer = new byte[len];
94                     System.arraycopy(data, pos, buffer, 0, buffer.length);
95                     pos += len;
96                     part = new MuleMessage(buffer, message);
97                     part.setCorrelationId(message.getUniqueId());
98                     part.setCorrelationGroupSize(parts);
99                     part.setCorrelationSequence(count);
100                     super.route(part, session, synchronous);
101                 }
102
103             }
104             catch (RoutingException e)
105             {
106                 // we'll want to send the whole message to the Exception handler
107
e = new RoutingException(e.getI18nMessage(), e.getUmoMessage(), e.getEndpoint(), e.getCause());
108                 // e.addInfo("chunking", "true");
109
// buffer = new byte[data.length - len];
110
// System.arraycopy(data, len, buffer, 0, buffer.length);
111
// e.addInfo("remaining data", buffer);
112
throw e;
113             }
114         }
115         return message;
116     }
117 }
118
Popular Tags