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  package org.apache.commons.betwixt.expression;
18  
19  import org.apache.commons.beanutils.DynaBean;
20  import org.apache.commons.beanutils.DynaProperty;
21  
22  /**
23   * Updates <code>DynaBean</code>'s.
24   * @since 0.7
25   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
26   */
27  public class DynaBeanUpdater extends TypedUpdater {
28  
29      /** The name of the dyna property to be updated */
30      private final String propertyName;
31      
32      /**
33       * Constructs a <code>DynaBeanUpdater</code>
34       * for given <code>DynaProperty</code>.
35       * @param dynaProperty <code>DyanProperty</code>, not null
36       */
37      public DynaBeanUpdater(DynaProperty dynaProperty) {
38          this(dynaProperty.getName(), dynaProperty.getType());
39      }
40      
41      /**
42       * Constructs a <code>DynaBeanUpdater</code>
43       * for the given type and property name.
44       * @param propertyName name of the dyan property
45       * @param type type of the dyna property
46       */
47      public DynaBeanUpdater(String propertyName, Class type) {
48          this.propertyName = propertyName;
49          setValueType(type);
50      }
51      
52      
53      /**
54       * Executes the update on the given code>DynaBean</code>
55       * @see org.apache.commons.betwixt.expression.TypedUpdater#executeUpdate(Context, java.lang.Object, java.lang.Object)
56       */
57      protected void executeUpdate(Context context, Object bean, Object value) throws Exception {
58          if (bean instanceof DynaBean)
59          {
60              DynaBean dynaBean = (DynaBean) bean;
61              dynaBean.set(propertyName, value);
62          }
63          else
64          {
65              handleException(context, new IllegalArgumentException("DynaBean required."));
66          }
67      }
68  
69      /**
70       * Outputs something suitable for logging.
71       */
72      public String toString() {
73          return "DynaBeanUpdater [property=" + propertyName + "]";
74      }
75      
76  }