01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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://jakarta.apache.org/commons'>Jakarta 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: * 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,
57: Object value) throws Exception {
58: if (bean instanceof DynaBean) {
59: DynaBean dynaBean = (DynaBean) bean;
60: dynaBean.set(propertyName, value);
61: } else {
62: handleException(context, new IllegalArgumentException(
63: "DynaBean required."));
64: }
65: }
66:
67: /**
68: * Outputs something suitable for logging.
69: */
70: public String toString() {
71: return "DynaBeanUpdater [property=" + propertyName + "]";
72: }
73:
74: }
|