1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.commons.jelly.tags.core; |
17 |
|
|
18 |
|
import java.lang.reflect.InvocationTargetException; |
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.List; |
21 |
|
|
22 |
|
import org.apache.commons.beanutils.MethodUtils; |
23 |
|
import org.apache.commons.jelly.JellyTagException; |
24 |
|
import org.apache.commons.jelly.MissingAttributeException; |
25 |
|
import org.apache.commons.jelly.TagSupport; |
26 |
|
import org.apache.commons.jelly.XMLOutput; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class InvokeTag extends TagSupport implements ArgTagParent { |
36 |
|
|
37 |
|
|
38 |
|
private String var; |
39 |
|
|
40 |
|
|
41 |
|
private String exceptionVar; |
42 |
|
|
43 |
|
|
44 |
|
private String methodName; |
45 |
|
|
46 |
|
|
47 |
|
private Object onInstance; |
48 |
|
|
49 |
221 |
private List paramTypes = new ArrayList(); |
50 |
221 |
private List paramValues = new ArrayList(); |
51 |
|
|
52 |
221 |
public InvokeTag() { |
53 |
221 |
} |
54 |
|
|
55 |
|
|
56 |
|
public void setVar(String var) { |
57 |
26 |
this.var = class="keyword">var; |
58 |
26 |
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public void setExceptionVar(String var) { |
64 |
13 |
this.exceptionVar = var; |
65 |
13 |
} |
66 |
|
|
67 |
|
public void setMethod(String method) { |
68 |
221 |
this.methodName = method; |
69 |
221 |
} |
70 |
|
|
71 |
|
public void setOn(Object instance) { |
72 |
221 |
this.onInstance = instance; |
73 |
221 |
} |
74 |
|
|
75 |
|
public void addArgument(Class type, Object value) { |
76 |
208 |
paramTypes.add(type); |
77 |
208 |
paramValues.add(value); |
78 |
208 |
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
83 |
221 |
if ( null == methodName) { |
84 |
0 |
throw new MissingAttributeException( "method" ); |
85 |
|
} |
86 |
221 |
if ( null == onInstance ) { |
87 |
0 |
throw new MissingAttributeException( "on" ); |
88 |
|
} |
89 |
|
|
90 |
221 |
invokeBody(output); |
91 |
|
|
92 |
221 |
Object[] values = paramValues.toArray(); |
93 |
221 |
Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()])); |
94 |
|
|
95 |
221 |
Object result = null; |
96 |
|
try { |
97 |
221 |
result = MethodUtils.invokeMethod(onInstance,methodName,values,types); |
98 |
|
} |
99 |
0 |
catch (NoSuchMethodException e) { |
100 |
0 |
throw new JellyTagException(e); |
101 |
|
} |
102 |
0 |
catch (IllegalAccessException e) { |
103 |
0 |
throw new JellyTagException(e); |
104 |
|
} |
105 |
26 |
catch (InvocationTargetException e) { |
106 |
26 |
if(null != exceptionVar) { |
107 |
13 |
context.setVariable(exceptionVar,e.getTargetException()); |
108 |
13 |
} else { |
109 |
13 |
throw new JellyTagException("method " + methodName + |
110 |
|
" threw exception: "+ e.getTargetException().getMessage(), |
111 |
|
e.getTargetException() ); |
112 |
|
} |
113 |
|
} |
114 |
|
finally { |
115 |
221 |
paramTypes.clear(); |
116 |
221 |
paramValues.clear(); |
117 |
221 |
} |
118 |
|
|
119 |
221 |
ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class)); |
120 |
208 |
if(null != parentArg) { |
121 |
52 |
parentArg.setValue(result); |
122 |
|
} |
123 |
208 |
if(null != var) { |
124 |
26 |
context.setVariable(var, result); |
125 |
|
} |
126 |
208 |
} |
127 |
|
} |