KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractEventResequencer.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.apache.commons.collections.IteratorUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.umo.MessagingException;
17 import org.mule.umo.UMOEvent;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * <code>AbstractEventResequencer</code> is used to receive a set of events,
27  * resequence them and forward them on to their destination
28  *
29  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30  * @version $Revision: 3798 $
31  */

32
33 public abstract class AbstractEventResequencer extends SelectiveConsumer
34 {
35     protected static final String JavaDoc NO_CORRELATION_ID = "no-id";
36
37     /**
38      * logger used by this class
39      */

40     protected static Log logger = LogFactory.getLog(AbstractEventResequencer.class);
41
42     private Comparator JavaDoc comparator;
43     private Map JavaDoc eventGroups = new HashMap JavaDoc();
44
45     public UMOEvent[] process(UMOEvent event) throws MessagingException
46     {
47         if (isMatch(event))
48         {
49             EventGroup eg = addEvent(event);
50             if (shouldResequence(eg))
51             {
52                 List JavaDoc resequencedEvents = resequenceEvents(eg);
53                 removeGroup(eg.getGroupId());
54                 UMOEvent[] returnEvents = new UMOEvent[resequencedEvents.size()];
55                 resequencedEvents.toArray(returnEvents);
56                 return returnEvents;
57             }
58         }
59         return null;
60     }
61
62     protected EventGroup addEvent(UMOEvent event)
63     {
64         String JavaDoc cId = event.getMessage().getCorrelationId();
65         if (cId == null)
66         {
67             cId = NO_CORRELATION_ID;
68         }
69         EventGroup eg = (EventGroup)eventGroups.get(cId);
70         if (eg == null)
71         {
72             eg = new EventGroup(cId);
73             eg.addEvent(event);
74             eventGroups.put(eg.getGroupId(), eg);
75         }
76         else
77         {
78             eg.addEvent(event);
79         }
80         return eg;
81     }
82
83     protected void removeGroup(Object JavaDoc id)
84     {
85         eventGroups.remove(id);
86     }
87
88     protected List JavaDoc resequenceEvents(EventGroup events)
89     {
90         List JavaDoc result = IteratorUtils.toList(events.iterator(), events.size());
91         if (comparator != null)
92         {
93             Collections.sort(result, comparator);
94         }
95         else
96         {
97             logger.warn("Event comparator is null, events were not reordered");
98         }
99         return result;
100     }
101
102     public Comparator JavaDoc getComparator()
103     {
104         return comparator;
105     }
106
107     public void setComparator(Comparator JavaDoc comparator)
108     {
109         this.comparator = comparator;
110     }
111
112     protected abstract boolean shouldResequence(EventGroup events);
113 }
114
Popular Tags