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.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 463298 2006-10-12 16:10:32Z henning $
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 */
53 public GetExecutor(final Log log, final Introspector introspector,
54 final Class clazz, final String property)
55 {
56 this.log = log;
57 this.introspector = introspector;
58
59 // If you passed in null as property, we don't use the value
60 // for parameter lookup. Instead we just look for get() without
61 // any parameters.
62 //
63 // In any other case, the following condition will set up an array
64 // for looking up get(String) on the class.
65
66 if (property != null)
67 {
68 this.params = new Object[] { property };
69 }
70 discover(clazz);
71 }
72
73 /**
74 * @param rlog
75 * @param introspector
76 * @param clazz
77 * @param property
78 * @deprecated RuntimeLogger is deprecated. Use the other constructor.
79 */
80 public GetExecutor(final RuntimeLogger rlog, final Introspector introspector,
81 final Class clazz, final String property)
82 {
83 this(new RuntimeLoggerLog(rlog), introspector, clazz, property);
84 }
85
86 protected void discover(final Class clazz)
87 {
88 try
89 {
90 setMethod(introspector.getMethod(clazz, "get", params));
91 }
92 /**
93 * pass through application level runtime exceptions
94 */
95 catch( RuntimeException e )
96 {
97 throw e;
98 }
99 catch(Exception e)
100 {
101 log.error("While looking for get('" + params[0] + "') method:", e);
102 }
103 }
104
105 /**
106 * @see org.apache.velocity.runtime.parser.node.AbstractExecutor#execute(java.lang.Object)
107 */
108 public Object execute(final Object o)
109 throws IllegalAccessException, InvocationTargetException
110 {
111 return isAlive() ? getMethod().invoke(o, params) : null;
112 }
113 }