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: AssignPropertiesCommand.java 473102 2006-11-09 22:26:21Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import java.util.Iterator;
24  
25  import org.apache.commons.chain.Catalog;
26  import org.apache.commons.chain.Command;
27  import org.apache.commons.chain.Context;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.shale.clay.config.Globals;
31  import org.apache.shale.clay.config.beans.AttributeBean;
32  import org.apache.shale.clay.config.beans.ComponentBean;
33  
34  /***
35   * <p>
36   * Sets the <code>UIComponent</code> properties using the attributes collection of
37   * {@link org.apache.shale.clay.config.beans.AttributeBean} by invoking the
38   * <code>Globals.SET_ATTRIBUTE_COMMAND_NAME</code> <code>Chain</code>.
39   * </p>
40   */
41  public class AssignPropertiesCommand extends AbstractCommand {
42  
43      /***
44       * <p>
45       * Common logger utility.
46       * </p>
47       */
48      private static Log log;
49      static {
50          log = LogFactory.getLog(AssignPropertiesCommand.class);
51      }
52  
53      /***
54       * <p>
55       * Invokes a chain that fires several Commands.
56       * <dl>
57       * <dt>Associated Property Commands
58       * <dd>{@link org.apache.shale.clay.component.chain.PropertyActionCommand}
59       * <dd>{@link org.apache.shale.clay.component.chain.PropertyActionListenerCommand}
60       * <dd>{@link org.apache.shale.clay.component.chain.PropertyValidatorCommand}
61       * <dd>{@link org.apache.shale.clay.component.chain.PropertyValueChangeListenerCommand}
62       * <dd>{@link org.apache.shale.clay.component.chain.PropertyValueCommand}
63       * </dl>
64       * </p>
65       *
66       * @param context common chains
67       * @return <code>true</code> if the chain is complete
68       * @exception Exception propagated up to the top of the chain
69       */
70      public boolean execute(Context context) throws Exception {
71  
72          boolean isFinal = false;
73  
74          ClayContext clayContext = (ClayContext) context;
75          if (clayContext == null) {
76              throw new NullPointerException(getMessages()
77                      .getMessage("clay.null.clayContext"));
78          }
79  
80          ComponentBean displayElement = clayContext.getDisplayElement();
81          if (displayElement == null) {
82              throw new NullPointerException(getMessages()
83                      .getMessage("clay.null.componentBean"));
84          }
85  
86          Iterator ai = displayElement.getAttributeIterator();
87          Catalog defaultCatalog = getCatalog();
88          Catalog customizationCatalog = getCustomizationCatalog();
89  
90          Command defaultCommand = defaultCatalog.getCommand(Globals.SET_ATTRIBUTE_COMMAND_NAME);
91          next: while (ai.hasNext()) {
92              AttributeBean a = (AttributeBean) ai.next();
93              if (a.getValue() == null) {
94                 continue next;
95              }
96  
97              Command command = null;
98              //look for a command override in the customization catalog first
99              if (customizationCatalog != null) {
100                 command = customizationCatalog.getCommand(a.getName());
101             }
102             //look for a command override in the defaut catalog
103             if (command == null) {
104                command = defaultCatalog.getCommand(a.getName());
105             }
106             //use the default command
107             if (command == null) {
108                command = defaultCommand;
109             }
110 
111             clayContext.setAttribute(a);
112 
113             try {
114                 command.execute(clayContext);
115             } catch (Exception e) {
116                 log.error(getMessages().getMessage("assign.property.error",
117                         new Object[] { a }), e);
118                         throw e;
119 
120             }
121 
122         }
123         ai = null;
124 
125         return isFinal;
126     }
127 
128 }