View Javadoc

1   /*
2    * $Id: TransformerUtils.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformer;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleException;
16  import org.mule.api.endpoint.ImmutableEndpoint;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.transformer.Transformer;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.transformer.types.DataTypeFactory;
21  import org.mule.transport.service.TransportFactoryException;
22  import org.mule.transport.service.TransportServiceDescriptor;
23  
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.StringTokenizer;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  public class TransformerUtils
33  {
34  
35      public static final String COMMA = ",";
36  
37      private static Log logger = LogFactory.getLog(AbstractTransformer.class);
38  
39      public static void initialiseAllTransformers(List<Transformer> transformers) throws InitialisationException
40      {
41          if (transformers != null)
42          {
43              Iterator<Transformer> transformer = transformers.iterator();
44              while (transformer.hasNext())
45              {
46                  (transformer.next()).initialise();
47              }
48          }
49      }
50  
51      public static String toString(List<Transformer> transformers)
52      {
53          StringBuffer buffer = new StringBuffer();
54          Iterator<Transformer> transformer = transformers.iterator();
55          while (transformer.hasNext())
56          {
57              buffer.append(transformer.next().toString());
58              if (transformer.hasNext())
59              {
60                  buffer.append(" -> ");
61              }
62          }
63          return buffer.toString();
64      }
65  
66      public static Transformer firstOrNull(List<Transformer> transformers)
67      {
68          if (transformers != null && 0 != transformers.size())
69          {
70              return transformers.get(0);
71          }
72          else
73          {
74              return null;
75          }
76      }
77  
78      public static boolean isSourceTypeSupportedByFirst(List<Transformer> transformers, Class clazz)
79      {
80          Transformer transformer = firstOrNull(transformers);
81          return null != transformer && transformer.isSourceDataTypeSupported(new DataTypeFactory().create(clazz));
82      }
83  
84      protected static interface TransformerSource
85      {
86          public List<Transformer> getTransformers() throws TransportFactoryException;
87      }
88  
89      protected static List<Transformer> getTransformersFromSource(TransformerSource source)
90      {
91          try
92          {
93              List<Transformer> transformers = source.getTransformers();
94              TransformerUtils.initialiseAllTransformers(transformers);
95              return transformers;
96          }
97          catch (MuleException e)
98          {
99              logger.debug(e.getMessage(), e);
100             return null;
101         }
102     }
103 
104     public static List<Transformer> getDefaultInboundTransformers(final TransportServiceDescriptor serviceDescriptor, final ImmutableEndpoint endpoint)
105     {
106         return getTransformersFromSource(new TransformerSource()
107         {
108             public List<Transformer> getTransformers() throws TransportFactoryException
109             {
110                 return serviceDescriptor.createInboundTransformers(endpoint);
111             }
112         });
113     }
114 
115     public static List<Transformer> getDefaultResponseTransformers(final TransportServiceDescriptor serviceDescriptor, final ImmutableEndpoint endpoint)
116     {
117         return getTransformersFromSource(new TransformerSource()
118         {
119             public List<Transformer> getTransformers() throws TransportFactoryException
120             {
121                 return serviceDescriptor.createResponseTransformers(endpoint);
122             }
123         });
124     }
125 
126     public static List<Transformer> getDefaultOutboundTransformers(final TransportServiceDescriptor serviceDescriptor, final ImmutableEndpoint endpoint)
127     {
128         return getTransformersFromSource(new TransformerSource()
129         {
130             public List<Transformer> getTransformers() throws TransportFactoryException
131             {
132                 return serviceDescriptor.createOutboundTransformers(endpoint);
133             }
134         });
135     }
136 
137     /**
138      * Builds a list of Transformers.
139      *
140      * @param names - a list of transformers separated by commans
141      * @param muleContext the current muleContext. This is used to look up transformers in the registry
142      * @return a list (possibly empty) of transformers or
143      * @throws org.mule.api.DefaultMuleException if any of the transformers cannot be found
144      */
145     public static List<Transformer> getTransformers(String names, MuleContext muleContext) throws DefaultMuleException
146     {
147         if (null != names)
148         {
149             List<Transformer> transformers = new LinkedList<Transformer>();
150             StringTokenizer st = new StringTokenizer(names, COMMA);
151             while (st.hasMoreTokens())
152             {
153                 String key = st.nextToken().trim();
154                 Transformer transformer = muleContext.getRegistry().lookupTransformer(key);
155 
156                 if (transformer == null)
157                 {
158                     throw new DefaultMuleException(CoreMessages.objectNotRegistered("Transformer", key));
159                 }
160                 transformers.add(transformer);
161             }
162             return transformers;
163         }
164         else
165         {
166             return null;
167         }
168     }
169 
170 }