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: CreateValueChangeListenerCommand.java 471910 2006-11-06 22:44:56Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import javax.faces.component.EditableValueHolder;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.ValueBinding;
27  import javax.faces.event.ValueChangeListener;
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   * Creates a {@link ValueChangeListener} and assign it to the
38   * <code>parent</code> property in the {@link ClayContext}.
39   */
40  public class CreateValueChangeListenerCommand extends AbstractCommand {
41  
42      /***
43       * <p>
44       * Common Logging utility.
45       * </p>
46       */
47      private static Log log;
48      static {
49          log = LogFactory.getLog(CreateValueChangeListenerCommand.class);
50      }
51  
52      /***
53       * <p>
54       * Creates a <code>ValueChangeListener</code> and assigns it to the
55       * <code>parent</code> attribute.
56       * </p>
57       *
58       * @param context common chains
59       * @return <code>true</code> if the chain is complete
60       * @exception Exception propagated up to the top of the chain
61       */
62      public boolean execute(Context context) throws Exception {
63  
64          boolean isFinal = false;
65  
66          ClayContext clayContext = (ClayContext) context;
67          if (clayContext == null) {
68              throw new NullPointerException(getMessages()
69                      .getMessage("clay.null.clayContext"));
70          }
71  
72          UIComponent child = (UIComponent) clayContext.getChild();
73          if (child == null) {
74              throw new NullPointerException(getMessages()
75                      .getMessage("clay.null.childComponent"));
76          }
77  
78          ComponentBean displayElement = clayContext.getDisplayElement();
79          if (displayElement == null) {
80              throw new NullPointerException(getMessages()
81                      .getMessage("clay.null.componentBean"));
82          }
83  
84          EditableValueHolder parent = (EditableValueHolder) clayContext
85                  .getParent();
86          if (parent == null) {
87              throw new NullPointerException(getMessages()
88                      .getMessage("clay.null.parentComponent"));
89          }
90  
91          FacesContext facesContext = clayContext.getFacesContext();
92          if (facesContext == null) {
93              throw new NullPointerException(getMessages()
94                      .getMessage("clay.null.facesContext"));
95          }
96  
97          ValueChangeListener listener = null;
98          try {
99  
100             AttributeBean attr = displayElement.getAttribute("binding");
101             if (attr != null && isValueReference(attr.getValue())) {
102                 clayContext.setAttribute(attr);
103                 String expr = replaceMnemonic(clayContext);
104                 ValueBinding vb = facesContext.getApplication().createValueBinding(expr);
105                 listener = (ValueChangeListener) vb.getValue(facesContext);
106 
107             } else {
108 
109                 ClassLoader loader = Thread.currentThread().getContextClassLoader();
110                 if (loader == null) {
111                     loader = getClass().getClassLoader();
112                 }
113 
114                 listener = (ValueChangeListener) loader.loadClass(
115                         displayElement.getComponentType()).newInstance();
116 
117                 loader = null;
118             }
119         } catch (Exception e) {
120             log.error(getMessages().getMessage("create.valueChangeListener"), e);
121             throw e;
122         }
123         parent.addValueChangeListener(listener);
124         // reassign the child to the valueChangeListener for the
125         // AssignPropertiesCommand
126         clayContext.setChild(listener);
127 
128         return isFinal;
129     }
130 
131 }