01: /*
02: * $Id: AbstractMessageAwareTransformer.java 11343 2008-03-13 10:58:26Z tcarlson $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transformer;
12:
13: import org.mule.DefaultMuleMessage;
14: import org.mule.MuleServer;
15: import org.mule.RequestContext;
16: import org.mule.api.MuleEventContext;
17: import org.mule.api.MuleMessage;
18: import org.mule.api.transformer.TransformerException;
19: import org.mule.config.i18n.CoreMessages;
20:
21: /**
22: * <code>AbstractMessageAwareTransformer</code> is a transformer that has a reference
23: * to the current message. This message can be used obtains properties associated
24: * with the current message useful to the transform. Note that when part of a
25: * transform chain, the Message payload reflects the pre-transform message state,
26: * unless there is no current event for this thread, then the message will be a new
27: * DefaultMuleMessage with the src as its payload. Transformers should always work on the
28: * src object not the message payload.
29: *
30: * @see org.mule.api.MuleMessage
31: * @see org.mule.DefaultMuleMessage
32: */
33:
34: public abstract class AbstractMessageAwareTransformer extends
35: AbstractTransformer {
36:
37: public boolean isSourceTypeSupported(Class aClass,
38: boolean exactMatch) {
39: //TODO RM* This is a bit of hack since we could just register MuleMessage as a supportedType, but this has some
40: //funny behaviour in certain ObjectToXml transformers
41: return (super .isSourceTypeSupported(aClass, exactMatch) || MuleMessage.class
42: .isAssignableFrom(aClass));
43: }
44:
45: public final Object doTransform(Object src, String encoding)
46: throws TransformerException {
47: MuleMessage message;
48: if (src instanceof MuleMessage) {
49: message = (MuleMessage) src;
50: } else if (MuleServer.getMuleContext().getConfiguration()
51: .isAutoWrapMessageAwareTransform()) {
52: message = new DefaultMuleMessage(src);
53: } else {
54: MuleEventContext event = RequestContext.getEventContext();
55: if (event == null) {
56: throw new TransformerException(CoreMessages
57: .noCurrentEventForTransformer(), this );
58: }
59: message = event.getMessage();
60: if (!message.getPayload().equals(src)) {
61: throw new IllegalStateException(
62: "Transform payload does not match current MuleEventContext payload");
63: }
64: }
65: return transform(message, encoding);
66: }
67:
68: public abstract Object transform(MuleMessage message,
69: String outputEncoding) throws TransformerException;
70:
71: }
|