KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > builder > AbstractMessageBuilder


1 /*
2  * $Id: AbstractMessageBuilder.java 4259 2006-12-14 03:12:07Z 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.components.builder;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.MuleProperties;
16 import org.mule.impl.MuleMessage;
17 import org.mule.impl.UMODescriptorAware;
18 import org.mule.umo.UMODescriptor;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.UMOMessage;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.lifecycle.Callable;
23 import org.mule.umo.routing.UMOOutboundRouter;
24 import org.mule.util.StringMessageUtils;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * A component that will invoke all outbound endpoints configured on the component
32  * allow the result of each endpoint invocation to be aggregated to a single message.
33  *
34  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
35  * @version $Revision: 4259 $
36  */

37 public abstract class AbstractMessageBuilder implements UMODescriptorAware, Callable, MessageBuilder
38 {
39
40     /**
41      * logger used by this class
42      */

43     protected transient Log logger = LogFactory.getLog(getClass());
44
45     protected UMODescriptor descriptor;
46
47     public void setDescriptor(UMODescriptor descriptor)
48     {
49         this.descriptor = descriptor;
50     }
51
52     public Object JavaDoc onCall(UMOEventContext eventContext) throws Exception JavaDoc
53     {
54
55         UMOMessage requestMessage = new MuleMessage(eventContext.getTransformedMessage(),
56             eventContext.getMessage());
57
58         UMOMessage responseMessage = requestMessage;
59         Object JavaDoc builtMessage;
60
61         if (descriptor.getOutboundRouter().hasEndpoints())
62         {
63             List JavaDoc endpoints = new ArrayList JavaDoc();
64             for (Iterator JavaDoc iterator = descriptor.getOutboundRouter().getRouters().iterator(); iterator.hasNext();)
65             {
66                 UMOOutboundRouter router = (UMOOutboundRouter)iterator.next();
67                 endpoints.addAll(router.getEndpoints());
68             }
69             for (Iterator JavaDoc iterator = endpoints.iterator(); iterator.hasNext();)
70             {
71                 UMOEndpoint endpoint = (UMOEndpoint)iterator.next();
72                 boolean rsync = eventContext.getMessage().getBooleanProperty(
73                     MuleProperties.MULE_REMOTE_SYNC_PROPERTY, endpoint.isRemoteSync());
74                 if (!rsync)
75                 {
76                     logger.info("Endpoint: " + endpoint
77                                 + " is not remoteSync enabled. Message builder finishing");
78                     if (eventContext.isSynchronous())
79                     {
80                         responseMessage = eventContext.sendEvent(requestMessage, endpoint);
81                     }
82                     else
83                     {
84                         eventContext.dispatchEvent(requestMessage, endpoint);
85                         responseMessage = null;
86                     }
87                     break;
88                 }
89                 else
90                 {
91                     responseMessage = eventContext.sendEvent(requestMessage, endpoint);
92                     if (logger.isDebugEnabled())
93                     {
94                         logger.debug("Response Message Received from: " + endpoint.getEndpointURI());
95                     }
96                     if (logger.isTraceEnabled())
97                     {
98                         try
99                         {
100                             logger.trace("Message Payload: \n"
101                                          + StringMessageUtils.truncate(
102                                              StringMessageUtils.toString(responseMessage.getPayload()), 200,
103                                              false));
104                         }
105                         catch (Exception JavaDoc e)
106                         {
107                             // ignore
108
}
109                     }
110                     builtMessage = buildMessage(requestMessage, responseMessage);
111                     responseMessage = new MuleMessage(builtMessage, responseMessage);
112                     requestMessage = responseMessage;
113                 }
114             }
115         }
116         else
117         {
118             logger.info("There are currently no endpoints configured on component: " + descriptor.getName());
119         }
120         eventContext.setStopFurtherProcessing(true);
121         return responseMessage;
122     }
123 }
124
Popular Tags