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 java.lang.reflect.Method;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /** <p><code>MethodUpdater</code> updates the current bean context
025: * by calling a WriteMethod with the String value from the XML attribute
026: * or element.</p>
027: *
028: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
029: * @version $Revision: 438373 $
030: */
031: public class MethodUpdater extends TypedUpdater {
032:
033: /** Logger */
034: private static Log log = LogFactory.getLog(MethodUpdater.class);
035:
036: /**
037: * Programmatically set log
038: * @param aLog the implementation to which this class should log
039: */
040: public static void setLog(Log aLog) {
041: log = aLog;
042: }
043:
044: /** The method to call on the bean */
045: private Method method;
046:
047: /** Base constructor */
048: public MethodUpdater() {
049: }
050:
051: /**
052: * Convenience constructor sets method property
053: * @param method the Method to be invoked on the context's bean in the update
054: */
055: public MethodUpdater(Method method) {
056: setMethod(method);
057: }
058:
059: /**
060: * Gets the method which will be invoked by the update
061: *
062: * @return the Method to be invoked by the update
063: */
064: public Method getMethod() {
065: return method;
066: }
067:
068: /**
069: * Sets the constant value of this expression
070: * @param method the Method to be invoked by the update
071: */
072: public void setMethod(Method method) {
073: this .method = method;
074: Class[] types = method.getParameterTypes();
075: if (types == null || types.length <= 0) {
076: throw new IllegalArgumentException(
077: "The Method must have at least one parameter");
078: }
079: setValueType(types[0]);
080: }
081:
082: // Implementation methods
083: //-------------------------------------------------------------------------
084:
085: /**
086: * Returns something useful for logging.
087: * @return something useful for logging
088: */
089: public String toString() {
090: return "MethodUpdater [method=" + method + "]";
091: }
092:
093: /**
094: * Updates the bean by method invocation.
095: * @since 0.7
096: */
097: protected void executeUpdate(Context context, Object bean,
098: Object newValue) throws Exception {
099: if (log.isDebugEnabled()) {
100: log.debug("Calling setter method: " + method.getName()
101: + " on bean: " + bean + " with new value: "
102: + newValue);
103: }
104: Object[] arguments = { newValue };
105: try {
106: method.invoke(bean, arguments);
107: } catch (IllegalAccessException e) {
108: method.setAccessible(true);
109: method.invoke(bean, arguments);
110: }
111: }
112: }
|