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.RuntimeLogger;
25 import org.apache.velocity.runtime.log.Log;
26 import org.apache.velocity.runtime.log.RuntimeLoggerLog;
27 import org.apache.velocity.util.introspection.Introspector;
28
29
30 /**
31 * Executor that simply tries to execute a get(key)
32 * operation. This will try to find a get(key) method
33 * for any type of object, not just objects that
34 * implement the Map interface as was previously
35 * the case.
36 *
37 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
38 * @version $Id: GetExecutor.java 687177 2008-08-19 22:00:32Z nbubna $
39 */
40 public class GetExecutor extends AbstractExecutor
41 {
42 private final Introspector introspector;
43
44 // This is still threadsafe because this object is only read except in the C'tor.
45 private Object [] params = {};
46
47 /**
48 * @param log
49 * @param introspector
50 * @param clazz
51 * @param property
52 * @since 1.5
53 */
54 public GetExecutor(final Log log, final Introspector introspector,
55 final Class clazz, final String property)
56 {
57 this.log = log;
58 this.introspector = introspector;
59
60 // If you passed in null as property, we don't use the value
61 // for parameter lookup. Instead we just look for get() without
62 // any parameters.
63 //
64 // In any other case, the following condition will set up an array
65 // for looking up get(String) on the class.
66
67 if (property != null)
68 {
69 this.params = new Object[] { property };
70 }
71 discover(clazz);
72 }
73
74 /**
75 * @param rlog
76 * @param introspector
77 * @param clazz
78 * @param property
79 * @deprecated RuntimeLogger is deprecated. Use the other constructor.
80 */
81 public GetExecutor(final RuntimeLogger rlog, final Introspector introspector,
82 final Class clazz, final String property)
83 {
84 this(new RuntimeLoggerLog(rlog), introspector, clazz, property);
85 }
86
87 /**
88 * @since 1.5
89 */
90 protected void discover(final Class clazz)
91 {
92 try
93 {
94 setMethod(introspector.getMethod(clazz, "get", params));
95 }
96 /**
97 * pass through application level runtime exceptions
98 */
99 catch( RuntimeException e )
100 {
101 throw e;
102 }
103 catch(Exception e)
104 {
105 String msg = "Exception while looking for get('" + params[0] + "') method";
106 log.error(msg, e);
107 throw new VelocityException(msg, e);
108 }
109 }
110
111 /**
112 * @see org.apache.velocity.runtime.parser.node.AbstractExecutor#execute(java.lang.Object)
113 */
114 public Object execute(final Object o)
115 throws IllegalAccessException, InvocationTargetException
116 {
117 return isAlive() ? getMethod().invoke(o, params) : null;
118 }
119 }