1 /*
2 * $Id: NoArgsCallWrapper.java 7976 2007-08-21 14:26:13Z 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.config.i18n.CoreMessages;
14 import org.mule.impl.NoSatisfiableMethodsException;
15 import org.mule.impl.VoidResult;
16 import org.mule.umo.UMOEventContext;
17 import org.mule.umo.lifecycle.Callable;
18 import org.mule.umo.lifecycle.Initialisable;
19 import org.mule.umo.lifecycle.InitialisationException;
20 import org.mule.util.ClassUtils;
21 import org.mule.util.StringUtils;
22
23 import java.lang.reflect.Method;
24
25 public class NoArgsCallWrapper implements Callable, Initialisable
26 {
27 /**
28 * To allow injecting the delegate instead of instanciating it.
29 */
30 private Object delegateInstance;
31 private String delegateMethod;
32 private String delegateClass;
33
34 /**
35 * Method used to perform any initialisation work. If a fatal error occurs during initialisation an
36 * <code>InitialisationException</code> should be thrown, causing the Mule instance to shutdown. If the error is
37 * recoverable, say by retrying to connect, a <code>RecoverableException</code> should be thrown. There is no
38 * guarantee that by throwing a Recoverable exception that the Mule instance will not shut down.
39 *
40 * @throws org.mule.umo.lifecycle.InitialisationException
41 * if a fatal error occurs causing the Mule instance to shutdown
42 * @throws org.mule.umo.lifecycle.RecoverableException
43 * if an error occurs that can be recovered from
44 */
45 public void initialise() throws InitialisationException
46 {
47 if (delegateInstance == null)
48 {
49 // delegateInstance null -> both class and method required
50 if (StringUtils.isBlank(delegateClass) || StringUtils.isBlank(delegateMethod))
51 {
52 throw new InitialisationException(CoreMessages.noDelegateClassAndMethodProvidedForNoArgsWrapper(), this);
53 }
54 }
55 else
56 {
57 // delegateInstance provided -> no delegate class configured
58 if (StringUtils.isNotBlank(delegateClass))
59 {
60 throw new InitialisationException(CoreMessages.noDelegateClassIfDelegateInstanceSpecified(), this);
61 }
62 }
63
64 if (StringUtils.isBlank(delegateMethod))
65 {
66 throw new InitialisationException(CoreMessages.objectIsNull("delegateMethod"), this);
67 }
68 }
69
70 public Object onCall(UMOEventContext context) throws Exception
71 {
72 Class clazz = delegateInstance == null ? ClassUtils.loadClass(delegateClass, getClass())
73 : delegateInstance.getClass();
74
75 Method method = ClassUtils.getMethod(clazz, delegateMethod, null);
76 if (method == null)
77 {
78 throw new NoSatisfiableMethodsException(clazz, delegateMethod);
79 }
80 if (delegateInstance == null)
81 {
82 delegateInstance = clazz.newInstance();
83 }
84 Object result = method.invoke(delegateInstance, null);
85 if (Void.TYPE.equals(method.getReturnType()))
86 {
87 result = VoidResult.getInstance();
88 }
89 return result;
90 }
91
92 /**
93 * Getter for property 'delegateInstance'.
94 *
95 * @return Value for property 'delegateInstance'.
96 */
97 public Object getDelegateInstance()
98 {
99 return delegateInstance;
100 }
101
102 /**
103 * Setter for property 'delegateInstance'.
104 *
105 * @param delegateInstance Value to set for property 'delegateInstance'.
106 */
107 public void setDelegateInstance(final Object delegateInstance)
108 {
109 this.delegateInstance = delegateInstance;
110 }
111
112 /**
113 * Getter for property 'delegateMethod'.
114 *
115 * @return Value for property 'delegateMethod'.
116 */
117 public String getDelegateMethod()
118 {
119 return delegateMethod;
120 }
121
122 /**
123 * Setter for property 'delegateMethod'.
124 *
125 * @param delegateMethod Value to set for property 'delegateMethod'.
126 */
127 public void setDelegateMethod(final String delegateMethod)
128 {
129 this.delegateMethod = delegateMethod;
130 }
131
132 /**
133 * Getter for property 'delegateClass'.
134 *
135 * @return Value for property 'delegateClass'.
136 */
137 public String getDelegateClass()
138 {
139 return delegateClass;
140 }
141
142 /**
143 * Setter for property 'delegateClass'.
144 *
145 * @param delegateClass Value to set for property 'delegateClass'.
146 */
147 public void setDelegateClass(final String delegateClass)
148 {
149 this.delegateClass = delegateClass;
150 }
151 }