KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jbi > components > TransformerComponent


1 /*
2  * $Id: TransformerComponent.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.providers.jbi.components;
12
13 import org.mule.umo.transformer.TransformerException;
14 import org.mule.umo.transformer.UMOTransformer;
15
16 import javax.jbi.messaging.InOnly;
17 import javax.jbi.messaging.InOut;
18 import javax.jbi.messaging.MessageExchange;
19 import javax.jbi.messaging.MessagingException;
20 import javax.jbi.messaging.NormalizedMessage;
21 import javax.xml.transform.Source JavaDoc;
22
23 /**
24  * Mule transformers can be reused in side a Jbi container
25  *
26  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
27  * @version $Revision: 3798 $
28  */

29 public class TransformerComponent extends AbstractJbiComponent
30 {
31
32     protected UMOTransformer transformer = null;
33
34     public void onMessage(MessageExchange messageExchange, NormalizedMessage message)
35         throws MessagingException
36     {
37         if (messageExchange.getRole() == MessageExchange.Role.PROVIDER)
38         {
39             return;
40         }
41         NormalizedMessage in = messageExchange.getMessage("in");
42
43         try
44         {
45             NormalizedMessage out = messageExchange.createMessage();
46             transform(messageExchange, in, out);
47
48             if (messageExchange instanceof InOut)
49             {
50                 messageExchange.setMessage(out, OUT);
51             }
52             else
53             {
54                 InOnly outExchange = exchangeFactory.createInOnlyExchange();
55                 outExchange.setInMessage(out);
56                 deliveryChannel.sendSync(outExchange);
57             }
58             done(messageExchange);
59         }
60         catch (Exception JavaDoc e)
61         {
62             error(messageExchange, e);
63         }
64     }
65
66     protected void transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
67         throws TransformerException, MessagingException
68     {
69         Object JavaDoc result = transformer.transform(in.getContent());
70         if (result instanceof Source JavaDoc)
71         {
72             out.setContent((Source JavaDoc)result);
73         }
74         else
75         {
76             throw new UnsupportedOperationException JavaDoc(
77                 "Support for Source transformation is not yet implemented");
78         }
79     }
80 }
81
Popular Tags