001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.betwixt.expression;
018
019 import org.apache.commons.beanutils.DynaBean;
020 import org.apache.commons.beanutils.DynaProperty;
021
022 /**
023 * Updates <code>DynaBean</code>'s.
024 * @since 0.7
025 * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
026 */
027 public class DynaBeanUpdater extends TypedUpdater {
028
029 /** The name of the dyna property to be updated */
030 private final String propertyName;
031
032 /**
033 * Constructs a <code>DynaBeanUpdater</code>
034 * for given <code>DynaProperty</code>.
035 * @param dynaProperty <code>DyanProperty</code>, not null
036 */
037 public DynaBeanUpdater(DynaProperty dynaProperty) {
038 this(dynaProperty.getName(), dynaProperty.getType());
039 }
040
041 /**
042 * Constructs a <code>DynaBeanUpdater</code>
043 * for the given type and property name.
044 * @param propertyName name of the dyan property
045 * @param type type of the dyna property
046 */
047 public DynaBeanUpdater(String propertyName, Class type) {
048 this.propertyName = propertyName;
049 setValueType(type);
050 }
051
052
053 /**
054 * Executes the update on the given code>DynaBean</code>
055 * @see org.apache.commons.betwixt.expression.TypedUpdater#executeUpdate(Context, java.lang.Object, java.lang.Object)
056 */
057 protected void executeUpdate(Context context, Object bean, Object value) throws Exception {
058 if (bean instanceof DynaBean)
059 {
060 DynaBean dynaBean = (DynaBean) bean;
061 dynaBean.set(propertyName, value);
062 }
063 else
064 {
065 handleException(context, new IllegalArgumentException("DynaBean required."));
066 }
067 }
068
069 /**
070 * Outputs something suitable for logging.
071 */
072 public String toString() {
073 return "DynaBeanUpdater [property=" + propertyName + "]";
074 }
075
076 }