1 package org.apache.velocity.runtime.parser.node; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.lang.reflect.InvocationTargetException; 23 import org.apache.velocity.exception.VelocityException; 24 import org.apache.velocity.runtime.log.Log; 25 import org.apache.velocity.util.introspection.Introspector; 26 27 28 /** 29 * Executor that simply tries to execute a put(key, value) 30 * operation. This will try to find a put(key) method 31 * for any type of object, not just objects that 32 * implement the Map interface as was previously 33 * the case. 34 * 35 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 36 * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a> 37 * @version $Id: PutExecutor.java 687177 2008-08-19 22:00:32Z nbubna $ 38 * @since 1.5 39 */ 40 public class PutExecutor extends SetExecutor 41 { 42 private final Introspector introspector; 43 private final String property; 44 45 /** 46 * @param log 47 * @param introspector 48 * @param clazz 49 * @param arg 50 * @param property 51 */ 52 public PutExecutor(final Log log, final Introspector introspector, 53 final Class clazz, final Object arg, final String property) 54 { 55 this.log = log; 56 this.introspector = introspector; 57 this.property = property; 58 59 discover(clazz, arg); 60 } 61 62 /** 63 * @param clazz 64 * @param arg 65 */ 66 protected void discover(final Class clazz, final Object arg) 67 { 68 Object [] params; 69 70 // If you passed in null as property, we don't use the value 71 // for parameter lookup. Instead we just look for put(Object) without 72 // any parameters. 73 // 74 // In any other case, the following condition will set up an array 75 // for looking up put(String, Object) on the class. 76 77 if (property == null) 78 { 79 // The passed in arg object is used by the Cache to look up the method. 80 params = new Object[] { arg }; 81 } 82 else 83 { 84 params = new Object[] { property, arg }; 85 } 86 87 try 88 { 89 setMethod(introspector.getMethod(clazz, "put", params)); 90 } 91 /** 92 * pass through application level runtime exceptions 93 */ 94 catch( RuntimeException e ) 95 { 96 throw e; 97 } 98 catch(Exception e) 99 { 100 String msg = "Exception while looking for put('" + params[0] + "') method"; 101 log.error(msg, e); 102 throw new VelocityException(msg, e); 103 } 104 } 105 106 /** 107 * @see org.apache.velocity.runtime.parser.node.SetExecutor#execute(java.lang.Object, java.lang.Object) 108 */ 109 public Object execute(final Object o, final Object value) 110 throws IllegalAccessException, InvocationTargetException 111 { 112 Object [] params; 113 114 if (isAlive()) 115 { 116 // If property != null, pass in the name for put(key, value). Else just put(value). 117 if (property == null) 118 { 119 params = new Object [] { value }; 120 } 121 else 122 { 123 params = new Object [] { property, value }; 124 } 125 126 return getMethod().invoke(o, params); 127 } 128 129 return null; 130 } 131 }