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
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 463298 2006-10-12 16:10:32Z henning $
38 */
39 public class PutExecutor extends SetExecutor
40 {
41 private final Introspector introspector;
42 private final String property;
43
44 /**
45 * @param log
46 * @param introspector
47 * @param clazz
48 * @param arg
49 * @param property
50 */
51 public PutExecutor(final Log log, final Introspector introspector,
52 final Class clazz, final Object arg, final String property)
53 {
54 this.log = log;
55 this.introspector = introspector;
56 this.property = property;
57
58 discover(clazz, arg);
59 }
60
61 /**
62 * @param clazz
63 * @param arg
64 */
65 protected void discover(final Class clazz, final Object arg)
66 {
67 Object [] params;
68
69 // If you passed in null as property, we don't use the value
70 // for parameter lookup. Instead we just look for put(Object) without
71 // any parameters.
72 //
73 // In any other case, the following condition will set up an array
74 // for looking up put(String, Object) on the class.
75
76 if (property == null)
77 {
78 // The passed in arg object is used by the Cache to look up the method.
79 params = new Object[] { arg };
80 }
81 else
82 {
83 params = new Object[] { property, arg };
84 }
85
86 try
87 {
88 setMethod(introspector.getMethod(clazz, "put", params));
89 }
90 /**
91 * pass through application level runtime exceptions
92 */
93 catch( RuntimeException e )
94 {
95 throw e;
96 }
97 catch(Exception e)
98 {
99 log.error("While looking for put('" + params[0] + "') method:", e);
100 }
101 }
102
103 /**
104 * @see org.apache.velocity.runtime.parser.node.SetExecutor#execute(java.lang.Object, java.lang.Object)
105 */
106 public Object execute(final Object o, final Object value)
107 throws IllegalAccessException, InvocationTargetException
108 {
109 Object [] params;
110
111 if (isAlive())
112 {
113 // If property != null, pass in the name for put(key, value). Else just put(value).
114 if (property == null)
115 {
116 params = new Object [] { value };
117 }
118 else
119 {
120 params = new Object [] { property, value };
121 }
122
123 return getMethod().invoke(o, params);
124 }
125
126 return null;
127 }
128 }