001: /*
002: * $Id: AbstractResponseRouter.java 11343 2008-03-13 10:58:26Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.routing.response;
012:
013: import org.mule.api.MuleMessage;
014: import org.mule.api.config.MuleProperties;
015: import org.mule.api.lifecycle.InitialisationException;
016: import org.mule.api.lifecycle.LifecycleTransitionResult;
017: import org.mule.api.routing.ResponseRouter;
018: import org.mule.routing.AbstractRouter;
019: import org.mule.routing.CorrelationPropertiesExpressionEvaluator;
020: import org.mule.util.ClassUtils;
021: import org.mule.util.expression.ExpressionEvaluator;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * <code>AbstractResponseRouter</code> is a base class for all Response Routers
028: */
029:
030: public abstract class AbstractResponseRouter extends AbstractRouter
031: implements ResponseRouter {
032: protected final Log logger = LogFactory.getLog(getClass());
033:
034: private int timeout = -1; // undefined
035:
036: private boolean failOnTimeout = true;
037:
038: protected ExpressionEvaluator propertyExtractor = new CorrelationPropertiesExpressionEvaluator();
039:
040: //@Override
041: public LifecycleTransitionResult initialise()
042: throws InitialisationException {
043: if (timeout == -1) // undefined
044: {
045: setTimeout(muleContext.getConfiguration()
046: .getDefaultSynchronousEventTimeout());
047: }
048: return super .initialise();
049: }
050:
051: public ExpressionEvaluator getPropertyExtractor() {
052: return propertyExtractor;
053: }
054:
055: public void setPropertyExtractor(
056: ExpressionEvaluator propertyExtractor) {
057: this .propertyExtractor = propertyExtractor;
058: }
059:
060: /**
061: * A digester callback to configure a custom correlation extractor.
062: *
063: * @param className correlation extractor fully qualified class name
064: */
065: public void setPropertyExtractorAsString(String className) {
066: try {
067: this .propertyExtractor = (ExpressionEvaluator) ClassUtils
068: .instanciateClass(className, null, getClass());
069: } catch (Exception ex) {
070: throw (IllegalArgumentException) new IllegalArgumentException(
071: "Couldn't instanciate property extractor class "
072: + className).initCause(ex);
073: }
074: }
075:
076: public int getTimeout() {
077: return timeout;
078: }
079:
080: public void setTimeout(int timeout) {
081: this .timeout = timeout;
082: }
083:
084: /**
085: * Extracts a 'Correlation Id' from a reply message. The correlation Id does not
086: * have to be the Message Correlation Id. It can be extracted from the message
087: * payload if desired.
088: *
089: * @param message a received reply message
090: * @return the correlation Id for this message
091: */
092: protected Object getReplyAggregateIdentifier(MuleMessage message) {
093: return propertyExtractor.evaluate(
094: MuleProperties.MULE_CORRELATION_ID_PROPERTY, message);
095: }
096:
097: /**
098: * Extracts a Group identifier from the current event. When an event is received
099: * with a group identifier not registered with this router, a new group is
100: * created. The id returned here can be a correlationId or some custom
101: * aggregation Id. This implementation uses the Unique Message Id of the
102: * MuleMessage being returned a
103: *
104: * @param message A response messages received on the response router endpoint
105: * @return an aggregation Id for this event
106: */
107: protected Object getCallResponseAggregateIdentifier(
108: MuleMessage message) {
109: return propertyExtractor.evaluate(
110: MuleProperties.MULE_MESSAGE_ID_PROPERTY, message);
111: }
112:
113: public boolean isFailOnTimeout() {
114: return failOnTimeout;
115: }
116:
117: public void setFailOnTimeout(boolean failOnTimeout) {
118: this.failOnTimeout = failOnTimeout;
119: }
120: }
|