Source code: org/mule/transformers/CompressionTransformer.java
1 /*
2 * $Header: /cvsroot/mule/mule/src/java/org/mule/transformers/CompressionTransformer.java,v 1.6 2003/10/20 21:44:38 rossmason Exp $
3 * $Revision: 1.6 $
4 * $Date: 2003/10/20 21:44:38 $
5 * ------------------------------------------------------------------------------------------------------
6 *
7 * Copyright (c) Cubis Limited. All rights reserved.
8 * http://www.cubis.co.uk
9 *
10 * The software in this package is published under the terms of the BSD
11 * style license a copy of which has been included with this distribution in
12 * the LICENSE.txt file.
13 *
14 */
15 package org.mule.transformers;
16
17 import java.io.IOException;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.mule.umo.transformer.TransformerException;
22 import org.mule.util.Utility;
23 import org.mule.util.compression.CompressionHelper;
24
25 /**
26 * <p><code>CompressionTransformer</code> Is a base class for all transformers.
27 * Transformations transform one object into another. This base class provides facilities for
28 * compressing and uncompressing messages.
29 *
30 * @author Ross Mason
31 * @version $Revision: 1.6 $
32 */
33
34 public abstract class CompressionTransformer extends AbstractTransformer
35 {
36 public static final String PROPERTY_DO_COMPRESSION = "org.mule.doCompression";
37
38 /** logger used by this class */
39 private static transient Log log = LogFactory.getLog(CompressionTransformer.class);
40
41 private boolean doCompression = false;
42
43 /** default constructor required for discovery */
44 public CompressionTransformer()
45 {
46 }
47
48 public void processProperties() throws TransformerException
49 {
50 if (props != null)
51 {
52 String temp = (String) props.get(PROPERTY_DO_COMPRESSION);
53 if (temp != null)
54 {
55 doCompression = Boolean.valueOf(temp).booleanValue();
56 }
57 }
58
59 }
60
61 /**
62 * Transforms the object.
63 *
64 * @param src The source object to transform.
65 * @param doCompression determines weather the data should be compressed or not
66 * @return The transformed object
67 */
68 public Object transform(Object src, boolean doCompression) throws TransformerException, TransformerException
69 {
70 setDoCompression(doCompression);
71 return transform(src);
72 }
73
74 /**
75 *
76 * @param src the source data to compress
77 * @return a compressed Message as a byte[]
78 * @throws TransformerException
79 */
80 protected byte[] compressMessage(Object src) throws TransformerException
81 {
82 try
83 {
84 if (getDoCompression())
85 {
86 byte[] buffer;
87 if (src instanceof String)
88 {
89 buffer = ((String) src).getBytes();
90 }
91 else
92 {
93 buffer = Utility.objectToByteArray(src);
94 }
95 byte[] cmp = CompressionHelper.compressByteArray(buffer);
96 if (log.isDebugEnabled())
97 log.debug("Compressed message in transformation");
98 return cmp;
99
100 }
101 else
102 {
103 return Utility.objectToByteArray(src);
104 }
105 }
106 catch (IOException e)
107 {
108 throw new TransformerException("Failed to compress message", e);
109 }
110
111 }
112
113 /**
114 * Uncompresses an Object into a byte[].
115 * @param src The Message to uncompress
116 * @return
117 * @throws TransformerException
118 */
119 protected byte[] uncompressMessage(Object src) throws TransformerException
120 {
121 byte[] buffer = null;
122 try
123 {
124 if (src instanceof String)
125 {
126 buffer = CompressionHelper.uncompressByteArray(((String) src).getBytes());
127 }
128 else
129 {
130 buffer = CompressionHelper.uncompressByteArray(Utility.objectToByteArray(src));
131 }
132 }
133 catch (IOException e)
134 {
135 log.error("Failed to uncompress message: " + e, e);
136 }
137 return buffer;
138 }
139
140 /**
141 *
142 * @return True if compression will be uesed otherwise false
143 */
144 public boolean getDoCompression()
145 {
146 return doCompression;
147 }
148
149 /**
150 * @param doCompression determines whether compression is used
151 */
152 public void setDoCompression(boolean doCompression)
153 {
154 this.doCompression = doCompression;
155 }
156 }