KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > simple > StaticComponent


1 /*
2  * $Id: StaticComponent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
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  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
27  * @version $Revision: 3798 $
28  */

29 public class StaticComponent implements Callable, Initialisable
30 {
31
32     private Object JavaDoc data;
33     private String JavaDoc dataFile;
34     private String JavaDoc prefix;
35     private String JavaDoc postfix;
36
37     public void initialise() throws InitialisationException
38     {
39         if (dataFile != null)
40         {
41             try
42             {
43                 data = IOUtils.getResourceAsString(dataFile, getClass());
44             }
45             catch (IOException JavaDoc e)
46             {
47                 throw new InitialisationException(e, this);
48             }
49         }
50     }
51
52     public Object JavaDoc getData()
53     {
54         return data;
55     }
56
57     public void setData(Object JavaDoc data)
58     {
59         this.data = data;
60     }
61
62     public String JavaDoc getDataFile()
63     {
64         return dataFile;
65     }
66
67     public void setDataFile(String JavaDoc dataFile)
68     {
69         this.dataFile = dataFile;
70     }
71
72     public String JavaDoc getPrefix()
73     {
74         return prefix;
75     }
76
77     public void setPrefix(String JavaDoc prefix)
78     {
79         this.prefix = prefix;
80     }
81
82     public String JavaDoc getPostfix()
83     {
84         return postfix;
85     }
86
87     public void setPostfix(String JavaDoc postfix)
88     {
89         this.postfix = postfix;
90     }
91
92     public Object JavaDoc onCall(UMOEventContext eventContext) throws Exception JavaDoc
93     {
94
95         if (data != null)
96         {
97             return data;
98         }
99
100         String JavaDoc eventData = eventContext.getTransformedMessageAsString();
101
102         if (prefix != null)
103         {
104             eventData = prefix + eventData;
105         }
106
107         if (postfix != null)
108         {
109             eventData += postfix;
110         }
111         return eventData;
112     }
113 }
114
Popular Tags