01: /*
02: * $Id: StaticComponent.java 10808 2008-02-14 20:36:57Z acooke $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.component.simple;
12:
13: import org.mule.api.MuleEventContext;
14: import org.mule.api.lifecycle.Callable;
15: import org.mule.api.lifecycle.Initialisable;
16: import org.mule.api.lifecycle.InitialisationException;
17: import org.mule.api.lifecycle.LifecycleTransitionResult;
18: import org.mule.util.IOUtils;
19:
20: import java.io.IOException;
21:
22: /**
23: * A service that will return a static data object as a result. This is useful for
24: * testing with expected results. The data returned can be read from a file or set as
25: * a property on this service.
26: */
27: public class StaticComponent implements Callable, Initialisable {
28:
29: private Object data;
30: private String dataFile;
31: private String prefix;
32: private String postfix;
33:
34: public LifecycleTransitionResult initialise()
35: throws InitialisationException {
36: if (dataFile != null) {
37: try {
38: data = IOUtils
39: .getResourceAsString(dataFile, getClass());
40: } catch (IOException e) {
41: throw new InitialisationException(e, this );
42: }
43: }
44: return LifecycleTransitionResult.OK;
45: }
46:
47: public Object getData() {
48: return data;
49: }
50:
51: public void setData(Object data) {
52: this .data = data;
53: }
54:
55: public String getDataFile() {
56: return dataFile;
57: }
58:
59: public void setDataFile(String dataFile) {
60: this .dataFile = dataFile;
61: }
62:
63: public String getPrefix() {
64: return prefix;
65: }
66:
67: public void setPrefix(String prefix) {
68: this .prefix = prefix;
69: }
70:
71: public String getPostfix() {
72: return postfix;
73: }
74:
75: public void setPostfix(String postfix) {
76: this .postfix = postfix;
77: }
78:
79: public Object onCall(MuleEventContext eventContext)
80: throws Exception {
81: if (data != null) {
82: return data;
83: }
84:
85: String eventData = eventContext.transformMessageToString();
86:
87: if (prefix != null) {
88: eventData = prefix + eventData;
89: }
90:
91: if (postfix != null) {
92: eventData += postfix;
93: }
94:
95: return eventData;
96: }
97: }
|