Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mule/transformers/AbstractTransformer.java


1   /* 
2    * $Header: /cvsroot/mule/mule/src/java/org/mule/transformers/AbstractTransformer.java,v 1.8 2003/10/20 21:44:38 rossmason Exp $
3    * $Revision: 1.8 $
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.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.mule.MuleManager;
25  import org.mule.umo.provider.UMOConnector;
26  import org.mule.umo.transformer.TransformerException;
27  import org.mule.umo.transformer.UMOTransformer;
28  
29  /**
30   *  <p><code>CompressionTransformer</code> Is a base class for all transformers.
31   * Transformations transform one object into another.  This base class provides facilities for
32   * compressing and uncompressing messages.
33   *
34   * @author Ross Mason
35   * @version $Revision: 1.8 $
36   */
37  
38  public abstract class AbstractTransformer implements UMOTransformer
39  {
40      /**
41       * The fully qualified class name of the fallback <code>Transformer</code>
42       * implementation class to use, if no other can be found.
43       */
44      public static final String TRANSFORMER_DEFAULT = "org.mule.transformers.DefaultTransformer";
45  
46      /**
47      * Specifies that the transformer properties should be obtained from the Mule
48      * Manager properties
49      */
50      public static final String USE_MANAGER_PROPERTIES = "org.mule.useManagerProperties";
51  
52      /** logger used by this class */
53      private static transient Log log = LogFactory.getLog(AbstractTransformer.class);
54  
55      protected Class returnClass = null;
56  
57      protected HashMap props = null;
58  
59      protected String name = null;
60  
61      protected UMOConnector connector = null;
62  
63      protected SingleTransformerSession transformerSession = null;
64  
65      private List sourceTypes = new ArrayList();
66  
67      /** default constructor required for discovery */
68      public AbstractTransformer()
69      {
70      }
71  
72      protected Object checkReturnClass(Object object) throws TransformerException
73      {
74          if (returnClass != null)
75          {
76              if (!returnClass.isInstance(object))
77              {
78                  throw new TransformerException(
79                      "The object transformed is of type: "
80                          + object.getClass().getName()
81                          + ", but the expected return type is: "
82                          + returnClass.getName());
83              }
84          }
85          log.debug("The transformed object is of expected type. Type is: " + object.getClass().getName());
86          return object;
87      }
88  
89      protected void registerSourceType(Class aClass)
90      {
91          sourceTypes.add(aClass);
92      }
93  
94      protected void unregisterSourceType(Class aClass)
95      {
96          sourceTypes.remove(aClass);
97      }
98  
99      protected Iterator getSourceTypeClassesIterator()
100     {
101         return sourceTypes.iterator();
102     }
103 
104     ///////////// Transformer Implementation /////////////////
105     /////////////////// PROPERTIES /////////////////////
106     public HashMap getProperties()
107     {
108         return props;
109     }
110 
111     public void setProperties(HashMap props) throws TransformerException
112     {
113         Object temp = props.get(USE_MANAGER_PROPERTIES);
114         if (temp != null)
115         {
116             if (Boolean.valueOf(temp.toString()).booleanValue())
117             {
118                 Object key = null;
119                 this.props = new HashMap();
120                 MuleManager manager = MuleManager.getInstance();
121                 for (Iterator i = manager.getPropertyNames(); i.hasNext();)
122                 {
123                     key = i.next();
124                     this.props.put(key, manager.getProperty(key));
125                 }
126                 this.props.putAll(props);
127             }
128             else
129             {
130                 this.props = props;
131             }
132         }
133         else
134         {
135             this.props = props;
136         }
137         processProperties();
138     }
139 
140     /**
141      * @return
142      */
143     public String getName()
144     {
145         if (name == null)
146         {
147             name = getClass().getName();
148             name = name.substring(name.lastIndexOf(".") + 1);
149         }
150         log.debug("Setting transformer name to: " + name);
151         return name;
152     }
153 
154     /**
155      * @param string
156      */
157     public void setName(String string)
158     {
159         name = string;
160     }
161 
162     /* (non-Javadoc)
163      * @see org.mule.transformers.Transformer#getReturnClass()
164      */
165     public Class getReturnClass()
166     {
167         return returnClass;
168     }
169 
170     /* (non-Javadoc)
171      * @see org.mule.transformers.Transformer#setReturnClass(java.lang.String)
172      */
173     public void setReturnClass(Class newClass)
174     {
175         returnClass = newClass;
176     }
177 
178     public boolean isSourceTypeSupported(Class aClass)
179     {
180         if (sourceTypes.isEmpty())
181         {
182             return true;
183         }
184         Class anotherClass = null;
185         for (Iterator i = getSourceTypeClassesIterator(); i.hasNext();)
186         {
187             anotherClass = (Class) i.next();
188             if (anotherClass.isAssignableFrom(aClass))
189             {
190                 return true;
191             }
192         }
193         return false;
194     }
195 
196     public Object sessionTransform(Object src) throws TransformerException
197     {
198         if (transformerSession == null)
199         {
200             startSession();
201         }
202         else if (!transformerSession.isInSession())
203         {
204             startSession();
205         }
206 
207         return transformerSession.getTransformer().transform(src);
208     }
209 
210     public void startSession() throws TransformerException
211     {
212         if (transformerSession == null)
213         {
214             transformerSession = new SingleTransformerSession(this);
215         }
216         else if (transformerSession.isDestroyed())
217         {
218             transformerSession = new SingleTransformerSession(this);
219         }
220         transformerSession.begin();
221     }
222 
223     public Object rollbackSession() throws TransformerException
224     {
225         if (transformerSession != null)
226         {
227             transformerSession.rollback();
228             return transformerSession.getData();
229         }
230         else
231         {
232             return null;
233         }
234     }
235 
236     public Object commitSession() throws TransformerException
237     {
238         if (transformerSession != null)
239         {
240             transformerSession.commit();
241             return transformerSession.getData();
242         }
243         else
244         {
245             return null;
246         }
247     }
248 
249     public boolean isInSession()
250     {
251         return (transformerSession != null && transformerSession.isInSession());
252     }
253     ////////////////// Abstract MEthods ////////////////////////
254 
255     /**
256      * A template method to allow deriving classes to do something with their
257      * properties once the setProperties method is invoked
258      */
259     public abstract void processProperties() throws TransformerException;
260 
261     /**
262     * Transforms the object.
263     *
264     * @param src The source object to transform.
265     * @return The transformed object
266     */
267     public Object transform(Object src) throws TransformerException
268     {
269         if (!isSourceTypeSupported(src.getClass()))
270         {
271             throw new TransformerException(
272                 "This transformer: " + getName() + " does not support this source type: " + src.getClass().getName());
273         }
274         return checkReturnClass(doTransform(src));
275     }
276 
277     /* (non-Javadoc)
278      * @see org.mule.umo.transformer.UMOTransformer#getConnector()
279      */
280     public UMOConnector getConnector()
281     {
282 
283         return connector;
284     }
285 
286     /* (non-Javadoc)
287      * @see org.mule.umo.transformer.UMOTransformer#setConnector(org.mule.umo.provider.UMOConnector)
288      */
289     public void setConnector(UMOConnector connector)
290     {
291         this.connector = connector;
292 
293     }
294 
295     public abstract Object doTransform(Object src) throws TransformerException;
296 
297 }