KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > wire > TransformerPairWireFormat


1 /*
2  * $Id: TransformerPairWireFormat.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.transformers.wire;
12
13 import org.mule.umo.UMOException;
14 import org.mule.umo.transformer.UMOTransformer;
15 import org.mule.umo.transformer.TransformerException;
16 import org.mule.config.i18n.Message;
17 import org.mule.config.i18n.Messages;
18 import org.mule.util.IOUtils;
19 import org.mule.MuleException;
20 import org.mule.MuleManager;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * TODO
31  */

32 public class TransformerPairWireFormat implements WireFormat
33 {
34
35     /**
36      * logger used by this class
37      */

38     protected transient Log logger = LogFactory.getLog(getClass());
39
40     protected UMOTransformer inboundTransformer;
41     protected UMOTransformer outboundTransformer;
42
43     public Object JavaDoc read(InputStream JavaDoc in) throws UMOException
44     {
45         if (inboundTransformer == null)
46         {
47             throw new NullPointerException JavaDoc(new Message(Messages.X_IS_NULL, "inboundTransformer").getMessage());
48         }
49         if (inboundTransformer.isSourceTypeSupported(InputStream JavaDoc.class))
50         {
51             return inboundTransformer.transform(in);
52         }
53         else
54         {
55             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
56             try
57             {
58                 IOUtils.copy(in, baos);
59                 return inboundTransformer.transform(baos.toByteArray());
60             }
61             catch (IOException JavaDoc e)
62             {
63                 throw new MuleException(new Message(Messages.FAILED_TO_READ_PAYLOAD, e));
64             }
65         }
66     }
67
68     public void write(OutputStream JavaDoc out, Object JavaDoc o) throws UMOException
69     {
70         if (outboundTransformer == null)
71         {
72             throw new NullPointerException JavaDoc(
73                 new Message(Messages.X_IS_NULL, "outboundTransformer").getMessage());
74         }
75         try
76         {
77             Class JavaDoc returnClass = outboundTransformer.getReturnClass();
78             if (returnClass == null)
79             {
80                 logger.warn("No return class was set on transformer: " + outboundTransformer + ". Attempting to work out" +
81                         "how to treat the result transformation");
82                 Object JavaDoc result = outboundTransformer.transform(o);
83                 byte[] bytes;
84                 if(result instanceof byte[])
85                 {
86                     bytes = (byte[])result;
87                 }
88                 else
89                 {
90                     bytes = result.toString().getBytes(MuleManager.getConfiguration().getEncoding());
91                 }
92
93                 out.write(bytes);
94             }
95             else if (returnClass.equals(byte[].class))
96             {
97                 byte[] b = (byte[])outboundTransformer.transform(o);
98                 out.write(b);
99             }
100             else
101             {
102                 if (returnClass != null && returnClass.equals(String JavaDoc.class))
103                 {
104                     String JavaDoc s = (String JavaDoc)outboundTransformer.transform(o);
105                     out.write(s.getBytes(MuleManager.getConfiguration().getEncoding()));
106                 }
107                 else
108                 {
109                     throw new TransformerException(
110                         new Message(Messages.TRANSFORM_FAILED_FROM_X, o.getClass()));
111                 }
112             }
113         }
114         catch (IOException JavaDoc e)
115         {
116             throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X, o.getClass(), e));
117         }
118     }
119
120     public UMOTransformer getInboundTransformer()
121     {
122         return inboundTransformer;
123     }
124
125     public void setInboundTransformer(UMOTransformer inboundTransformer)
126     {
127         this.inboundTransformer = inboundTransformer;
128     }
129
130     public UMOTransformer getOutboundTransformer()
131     {
132         return outboundTransformer;
133     }
134
135     public void setOutboundTransformer(UMOTransformer outboundTransformer)
136     {
137         this.outboundTransformer = outboundTransformer;
138     }
139 }
140
Popular Tags