2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.shale.clay.component.chain;
22
23 import javax.faces.component.ActionSource;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27 import javax.faces.event.ActionListener;
28
29 import org.apache.commons.chain.Context;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.shale.clay.config.beans.AttributeBean;
33 import org.apache.shale.clay.config.beans.ComponentBean;
34
35 /***
36 * <p>
37 * This <code>Command</code> will create a <code>ActonListener</code> and assign it to the
38 * <code>parent</code>. The construction of the <code>ActionListener</code>
39 * is based on the current <code>displayElement</code> in the
40 * {@link org.apache.shale.clay.component.chain.ClayContext}
41 * </p>
42 */
43 public class CreateActionListenerCommand extends AbstractCommand {
44
45 /***
46 * <p>
47 * Common Logger utility.
48 * </p>
49 */
50 private static Log log;
51 static {
52 log = LogFactory.getLog(CreateActionListenerCommand.class);
53 }
54
55 /***
56 * <p>
57 * Uses the {@link ClayContext} to build a <code>ActionListener</code> and
58 * assign it to the context <code>parent</code> using the context
59 * <code>displayElement</code> that is derived from type
60 * {@link ComponentBean}.
61 * </p>
62 *
63 * @param context common chains
64 * @return <code>true</code> if the chain is complete
65 * @exception Exception propagated up to the top of the chain
66 */
67 public boolean execute(Context context) throws Exception {
68
69 boolean isFinal = false;
70
71 ClayContext clayContext = (ClayContext) context;
72 if (clayContext == null) {
73 throw new NullPointerException(getMessages()
74 .getMessage("clay.null.clayContext"));
75 }
76
77 UIComponent child = (UIComponent) clayContext.getChild();
78 if (child == null) {
79 throw new NullPointerException(getMessages()
80 .getMessage("clay.null.childComponent"));
81 }
82
83 ComponentBean displayElement = clayContext.getDisplayElement();
84 if (displayElement == null) {
85 throw new NullPointerException(getMessages()
86 .getMessage("clay.null.componentBean"));
87 }
88
89 ActionSource parent = (ActionSource) clayContext.getParent();
90 if (parent == null) {
91 throw new NullPointerException(getMessages()
92 .getMessage("clay.null.parentComponent"));
93 }
94
95 FacesContext facesContext = clayContext.getFacesContext();
96 if (facesContext == null) {
97 throw new NullPointerException(getMessages()
98 .getMessage("clay.null.facesContext"));
99 }
100
101 ActionListener listener = null;
102 try {
103
104 AttributeBean attr = displayElement.getAttribute("binding");
105 if (attr != null && isValueReference(attr.getValue())) {
106 clayContext.setAttribute(attr);
107 String expr = replaceMnemonic(clayContext);
108 ValueBinding vb = facesContext.getApplication().createValueBinding(expr);
109 listener = (ActionListener) vb.getValue(facesContext);
110 } else {
111
112 ClassLoader loader = Thread.currentThread().getContextClassLoader();
113 if (loader == null) {
114 loader = getClass().getClassLoader();
115 }
116
117 listener = (ActionListener) loader.loadClass(
118 displayElement.getComponentType()).newInstance();
119
120 loader = null;
121 }
122 } catch (Exception e) {
123 log.error(getMessages().getMessage("create.actionListener.error",
124 new Object[] { displayElement }), e);
125 throw e;
126 }
127
128 parent.addActionListener(listener);
129
130
131 clayContext.setChild(listener);
132
133 return isFinal;
134 }
135
136 }