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