Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NoArgsCallWrapper |
|
| 2.375;2.375 |
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 | 0 | 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 | 0 | if (delegateInstance == null) |
48 | { | |
49 | // delegateInstance null -> both class and method required | |
50 | 0 | if (StringUtils.isBlank(delegateClass) || StringUtils.isBlank(delegateMethod)) |
51 | { | |
52 | 0 | throw new InitialisationException(CoreMessages.noDelegateClassAndMethodProvidedForNoArgsWrapper(), this); |
53 | } | |
54 | } | |
55 | else | |
56 | { | |
57 | // delegateInstance provided -> no delegate class configured | |
58 | 0 | if (StringUtils.isNotBlank(delegateClass)) |
59 | { | |
60 | 0 | throw new InitialisationException(CoreMessages.noDelegateClassIfDelegateInstanceSpecified(), this); |
61 | } | |
62 | } | |
63 | ||
64 | 0 | if (StringUtils.isBlank(delegateMethod)) |
65 | { | |
66 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("delegateMethod"), this); |
67 | } | |
68 | 0 | } |
69 | ||
70 | public Object onCall(UMOEventContext context) throws Exception | |
71 | { | |
72 | 0 | Class clazz = delegateInstance == null ? ClassUtils.loadClass(delegateClass, getClass()) |
73 | : delegateInstance.getClass(); | |
74 | ||
75 | 0 | Method method = ClassUtils.getMethod(clazz, delegateMethod, null); |
76 | 0 | if (method == null) |
77 | { | |
78 | 0 | throw new NoSatisfiableMethodsException(clazz, delegateMethod); |
79 | } | |
80 | 0 | if (delegateInstance == null) |
81 | { | |
82 | 0 | delegateInstance = clazz.newInstance(); |
83 | } | |
84 | 0 | Object result = method.invoke(delegateInstance, null); |
85 | 0 | if (Void.TYPE.equals(method.getReturnType())) |
86 | { | |
87 | 0 | result = VoidResult.getInstance(); |
88 | } | |
89 | 0 | return result; |
90 | } | |
91 | ||
92 | /** | |
93 | * Getter for property 'delegateInstance'. | |
94 | * | |
95 | * @return Value for property 'delegateInstance'. | |
96 | */ | |
97 | public Object getDelegateInstance() | |
98 | { | |
99 | 0 | 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 | 0 | this.delegateInstance = delegateInstance; |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * Getter for property 'delegateMethod'. | |
114 | * | |
115 | * @return Value for property 'delegateMethod'. | |
116 | */ | |
117 | public String getDelegateMethod() | |
118 | { | |
119 | 0 | 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 | 0 | this.delegateMethod = delegateMethod; |
130 | 0 | } |
131 | ||
132 | /** | |
133 | * Getter for property 'delegateClass'. | |
134 | * | |
135 | * @return Value for property 'delegateClass'. | |
136 | */ | |
137 | public String getDelegateClass() | |
138 | { | |
139 | 0 | 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 | 0 | this.delegateClass = delegateClass; |
150 | 0 | } |
151 | } |