View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt.expression;
17  
18  import org.apache.commons.beanutils.DynaBean;
19  import org.apache.commons.beanutils.DynaProperty;
20  
21  /***
22   * Updates <code>DynaBean</code>'s.
23   * @since 0.7
24   * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
25   */
26  public class DynaBeanUpdater extends TypedUpdater {
27  
28      /*** The name of the dyna property to be updated */
29      private final String propertyName;
30      
31      /***
32       * Constructs a <code>DynaBeanUpdater</code>
33       * for given <code>DynaProperty</code>.
34       * @param dynaProperty <code>DyanProperty</code>, not null
35       */
36      public DynaBeanUpdater(DynaProperty dynaProperty) {
37          this(dynaProperty.getName(), dynaProperty.getType());
38      }
39      
40      /***
41       * Constructs a <code>DynaBeanUpdater</code>
42       * for the given type and property name.
43       * @param propertyName name of the dyan property
44       * @param type type of the dyna property
45       */
46      public DynaBeanUpdater(String propertyName, Class type) {
47          this.propertyName = propertyName;
48          setValueType(type);
49      }
50      
51      
52      /***
53       * Executes the update on the given code>DynaBean</code>
54       * @see org.apache.commons.betwixt.expression.TypedUpdater#executeUpdate(Context, java.lang.Object, java.lang.Object)
55       */
56      protected void executeUpdate(Context context, Object bean, Object value) throws Exception {
57          if (bean instanceof DynaBean)
58          {
59              DynaBean dynaBean = (DynaBean) bean;
60              dynaBean.set(propertyName, value);
61          }
62          else
63          {
64              handleException(context, new IllegalArgumentException("DynaBean required."));
65          }
66      }
67  
68      /***
69       * Outputs something suitable for logging.
70       */
71      public String toString() {
72          return "DynaBeanUpdater [property=" + propertyName + "]";
73      }
74      
75  }