View Javadoc

1   /*
2    * $Id: StaticComponent.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.components.simple;
12  
13  import org.mule.umo.UMOEventContext;
14  import org.mule.umo.lifecycle.Callable;
15  import org.mule.umo.lifecycle.Initialisable;
16  import org.mule.umo.lifecycle.InitialisationException;
17  import org.mule.util.IOUtils;
18  
19  import java.io.IOException;
20  
21  /**
22   * A component that will return a static data object as a result. This is useful for
23   * testing with expected results. The data returned can be read from a file or set as
24   * a property on this component.
25   */
26  public class StaticComponent implements Callable, Initialisable
27  {
28  
29      private Object data;
30      private String dataFile;
31      private String prefix;
32      private String postfix;
33  
34      public void initialise() throws InitialisationException
35      {
36          if (dataFile != null)
37          {
38              try
39              {
40                  data = IOUtils.getResourceAsString(dataFile, getClass());
41              }
42              catch (IOException e)
43              {
44                  throw new InitialisationException(e, this);
45              }
46          }
47      }
48  
49      public Object getData()
50      {
51          return data;
52      }
53  
54      public void setData(Object data)
55      {
56          this.data = data;
57      }
58  
59      public String getDataFile()
60      {
61          return dataFile;
62      }
63  
64      public void setDataFile(String dataFile)
65      {
66          this.dataFile = dataFile;
67      }
68  
69      public String getPrefix()
70      {
71          return prefix;
72      }
73  
74      public void setPrefix(String prefix)
75      {
76          this.prefix = prefix;
77      }
78  
79      public String getPostfix()
80      {
81          return postfix;
82      }
83  
84      public void setPostfix(String postfix)
85      {
86          this.postfix = postfix;
87      }
88  
89      public Object onCall(UMOEventContext eventContext) throws Exception
90      {
91          if (data != null)
92          {
93              return data;
94          }
95  
96          String eventData = eventContext.getTransformedMessageAsString();
97  
98          if (prefix != null)
99          {
100             eventData = prefix + eventData;
101         }
102 
103         if (postfix != null)
104         {
105             eventData += postfix;
106         }
107 
108         return eventData;
109     }
110 }