1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.component.remotecommand;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26
27 import javax.el.MethodExpression;
28 import javax.faces.view.facelets.*;
29
30 import org.primefaces.extensions.component.parameters.MethodParameter;
31 import org.primefaces.extensions.component.parameters.MethodSignatureTagHandler;
32
33
34
35
36
37
38
39
40 public class RemoteCommandHandler extends ComponentHandler {
41
42
43
44
45
46
47
48
49 private static final class RemoteCommandMetaRule extends MetaRule {
50
51 private final Class<?>[] parameters;
52
53 public RemoteCommandMetaRule(final Class<?>[] parameters) {
54 this.parameters = parameters;
55 }
56
57 @Override
58 public Metadata applyRule(final String name, final TagAttribute attribute, final MetadataTarget meta) {
59 if (meta.isTargetInstanceOf(RemoteCommand.class) && RemoteCommand.PropertyKeys.actionListener.toString().equals(name)) {
60 final Method method = meta.getWriteMethod("actionListenerMethodExpression");
61
62 return new ActionListenerMetadata(attribute, method, parameters);
63 }
64
65 return null;
66 }
67 }
68
69
70
71
72
73
74
75
76 private static final class ActionListenerMetadata extends Metadata {
77
78 private final TagAttribute attribute;
79 private final Method method;
80 private final Class<?>[] parameters;
81
82 public ActionListenerMetadata(final TagAttribute attribute, final Method method, final Class<?>[] parameters) {
83 this.attribute = attribute;
84 this.method = method;
85 this.parameters = parameters;
86 }
87
88 @Override
89 public void applyMetadata(final FaceletContext context, final Object instance) {
90 final MethodExpression expression = attribute.getMethodExpression(context, null, parameters);
91
92
93 try {
94 method.invoke(instance, expression);
95 }
96 catch (final InvocationTargetException e) {
97 throw new TagAttributeException(attribute, e.getCause());
98 }
99 catch (final Exception e) {
100 throw new TagAttributeException(attribute, e);
101 }
102 }
103 }
104
105 public RemoteCommandHandler(final ComponentConfig config) {
106 super(config);
107 }
108
109 @Override
110 protected MetaRuleset createMetaRuleset(final Class type) {
111 final MetaRuleset metaRuleset = super.createMetaRuleset(type);
112 metaRuleset.addRule(new RemoteCommandMetaRule(getParameterTypes()));
113
114 return metaRuleset;
115 }
116
117 private Class<?>[] getParameterTypes() {
118
119 MethodSignatureTagHandler signatureTagHandler = null;
120
121 if (nextHandler instanceof CompositeFaceletHandler) {
122 final CompositeFaceletHandler handler = (CompositeFaceletHandler) nextHandler;
123 if (handler.getHandlers().length > 0 && handler.getHandlers()[0] instanceof MethodSignatureTagHandler) {
124 signatureTagHandler = (MethodSignatureTagHandler) handler.getHandlers()[0];
125 }
126 }
127
128 if (signatureTagHandler == null) {
129 return new Class<?>[] {};
130 }
131
132 return signatureTagHandler.getParameterTypes();
133 }
134 }