View Javadoc

1   /*
2    * $Id: AbstractStreamingTransformer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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 CPAL v1.0
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.streaming;
12  
13  import org.mule.MuleManager;
14  import org.mule.umo.endpoint.UMOImmutableEndpoint;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.transformer.TransformerException;
17  import org.mule.umo.transformer.UMOStreamingTransformer;
18  import org.mule.util.ClassUtils;
19  
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import org.apache.commons.beanutils.BeanUtils;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * TODO
29   */
30  public abstract class AbstractStreamingTransformer implements UMOStreamingTransformer
31  {
32      /**
33       * logger used by this class
34       */
35      protected final Log logger = LogFactory.getLog(getClass());
36  
37      /**
38       * The name that identifies this transformer. If none is set the class name of
39       * the transformer is used
40       */
41      protected String name = null;
42  
43      /**
44       * The endpoint that this transformer instance is configured on
45       */
46      protected UMOImmutableEndpoint endpoint = null;
47  
48      /**
49       * This is the following transformer in the chain of transformers.
50       */
51      protected UMOStreamingTransformer nextTransformer;
52  
53      /**
54       * Determines whether the transformer will throw an exception if the message
55       * passed is is not supported or the return tye is incorrect
56       */
57      private boolean ignoreBadInput = false;
58  
59      /**
60       * default constructor required for discovery
61       */
62      public AbstractStreamingTransformer()
63      {
64          name = generateTransformerName();
65      }
66  
67      /**
68       * @return transformer name
69       */
70      public String getName()
71      {
72          if (name == null)
73          {
74              setName(ClassUtils.getSimpleName(getClass()));
75          }
76          return name;
77      }
78  
79      /**
80       * @param string
81       */
82      public void setName(String string)
83      {
84          logger.debug("Setting transformer name to: " + name);
85          name = string;
86      }
87  
88      /**
89       * Transforms the object.
90       * 
91       * @param src The source object to transform.
92       * @return The transformed object
93       */
94      public final Object transform(InputStream src, OutputStream dest, String encoding)
95          throws TransformerException
96      {
97          if (encoding == null && endpoint != null)
98          {
99              encoding = endpoint.getEncoding();
100         }
101 
102         // last resort
103         if (encoding == null)
104         {
105             encoding = MuleManager.getConfiguration().getEncoding();
106         }
107 
108         if (logger.isDebugEnabled())
109         {
110             logger.debug("Applying transformer " + getName() + " (" + getClass().getName() + ")");
111             logger.debug("Object before transform");
112         }
113 
114         // TODO we should have a pipeline of transformers that we just 'execute'
115         // Object result = doTransform(src, dest, encoding);
116         // if (result == null)
117         // {
118         // result = NullPayload.getInstance();
119         // }
120         //
121         // if (logger.isDebugEnabled())
122         // {
123         // logger.debug("Object after transform");
124         // }
125         //
126         // if (nextTransformer != null)
127         // {
128         // logger.debug("Following transformer in the chain is " +
129         // nextTransformer.getName() + " ("
130         // + nextTransformer.getClass().getName() + ")");
131         // result = nextTransformer.transform(result);
132         // }
133         //
134         // return result;
135         return null;
136     }
137 
138     public UMOImmutableEndpoint getEndpoint()
139     {
140         return endpoint;
141     }
142 
143     /*
144      * (non-Javadoc)
145      * 
146      * @see org.mule.umo.transformer.UMOTransformer#setConnector(org.mule.umo.provider.UMOConnector)
147      */
148     public void setEndpoint(UMOImmutableEndpoint endpoint)
149     {
150         this.endpoint = endpoint;
151         UMOStreamingTransformer trans = nextTransformer;
152         while (trans != null && endpoint != null)
153         {
154             trans.setEndpoint(endpoint);
155             trans = trans.getNextTransformer();
156         }
157     }
158 
159     protected abstract Object doTransform(InputStream src, OutputStream dest, String encoding)
160         throws TransformerException;
161 
162     /*
163      * (non-Javadoc)
164      * 
165      * @see org.mule.umo.transformer.UMOTransformer#getNextTransformer()
166      */
167     public UMOStreamingTransformer getNextTransformer()
168     {
169         return nextTransformer;
170     }
171 
172     /*
173      * (non-Javadoc)
174      * 
175      * @see org.mule.umo.transformer.UMOTransformer#setNextTransformer(org.mule.umo.transformer.UMOTransformer)
176      */
177     public void setNextTransformer(UMOStreamingTransformer nextTransformer)
178     {
179         this.nextTransformer = nextTransformer;
180     }
181 
182     /*
183      * (non-Javadoc)
184      * 
185      * @see java.lang.Object#clone()
186      */
187     public Object clone() throws CloneNotSupportedException
188     {
189         try
190         {
191             // TODO see AbstractTransformer.clone() for why this is not enough
192             return BeanUtils.cloneBean(this);
193         }
194         catch (Exception e)
195         {
196             throw (CloneNotSupportedException) new CloneNotSupportedException("Failed to clone transformer: "
197                             + e.getMessage()).initCause(e);
198         }
199     }
200 
201     /**
202      * Template method were deriving classes can do any initialisation after the
203      * properties have been set on this transformer
204      * 
205      * @throws org.mule.umo.lifecycle.InitialisationException
206      */
207     public void initialise() throws InitialisationException
208     {
209         // nothing to do
210     }
211 
212     protected String generateTransformerName()
213     {
214         String name = getClass().getName();
215         int i = name.lastIndexOf(".");
216         if (i > -1)
217         {
218             name = name.substring(i + 1);
219         }
220         return name;
221     }
222 
223     public boolean isIgnoreBadInput()
224     {
225         return ignoreBadInput;
226     }
227 
228     public void setIgnoreBadInput(boolean ignoreBadInput)
229     {
230         this.ignoreBadInput = ignoreBadInput;
231     }
232 
233     public String toString()
234     {
235         return "StreamingTransformer{" + "name='" + name + "'" + ", ignoreBadInput=" + ignoreBadInput + "}";
236     }
237 
238     public boolean isAcceptNull()
239     {
240         return false;
241     }
242 
243 }