2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: CreateActionListenerCommand.java 471910 2006-11-06 22:44:56Z gvanmatre $
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         // reassign the child to the ActionListener for the
130         // AssignPropertiesCommand
131         clayContext.setChild(listener);
132 
133         return isFinal;
134     }
135 
136 }