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
20 import java.lang.reflect.Method;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /** <p><code>MethodUpdater</code> updates the current bean context
26 * by calling a WriteMethod with the String value from the XML attribute
27 * or element.</p>
28 *
29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30 * @version $Revision: 438373 $
31 */
32 public class MethodUpdater extends TypedUpdater {
33
34 /** Logger */
35 private static Log log = LogFactory.getLog( MethodUpdater.class );
36
37 /**
38 * Programmatically set log
39 * @param aLog the implementation to which this class should log
40 */
41 public static void setLog( Log aLog ) {
42 log = aLog;
43 }
44
45 /** The method to call on the bean */
46 private Method method;
47 /** Base constructor */
48 public MethodUpdater() {
49 }
50
51 /**
52 * Convenience constructor sets method property
53 * @param method the Method to be invoked on the context's bean in the update
54 */
55 public MethodUpdater(Method method) {
56 setMethod( method );
57 }
58
59 /**
60 * Gets the method which will be invoked by the update
61 *
62 * @return the Method to be invoked by the update
63 */
64 public Method getMethod() {
65 return method;
66 }
67
68 /**
69 * Sets the constant value of this expression
70 * @param method the Method to be invoked by the update
71 */
72 public void setMethod(Method method) {
73 this.method = method;
74 Class[] types = method.getParameterTypes();
75 if ( types == null || types.length <= 0 ) {
76 throw new IllegalArgumentException( "The Method must have at least one parameter" );
77 }
78 setValueType(types[0]);
79 }
80
81 // Implementation methods
82 //-------------------------------------------------------------------------
83
84
85
86 /**
87 * Returns something useful for logging.
88 * @return something useful for logging
89 */
90 public String toString() {
91 return "MethodUpdater [method=" + method + "]";
92 }
93
94 /**
95 * Updates the bean by method invocation.
96 * @since 0.7
97 */
98 protected void executeUpdate(Context context, Object bean, Object newValue) throws Exception {
99 if ( log.isDebugEnabled() ) {
100 log.debug(
101 "Calling setter method: " + method.getName() + " on bean: " + bean
102 + " with new value: " + newValue
103 );
104 }
105 Object[] arguments = { newValue };
106 try
107 {
108 method.invoke( bean, arguments );
109 }
110 catch (IllegalAccessException e)
111 {
112 method.setAccessible(true);
113 method.invoke( bean, arguments );
114 }
115 }
116 }