001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.InvocationTargetException;
023:
024: import org.apache.velocity.runtime.RuntimeLogger;
025: import org.apache.velocity.runtime.log.Log;
026: import org.apache.velocity.runtime.log.RuntimeLoggerLog;
027: import org.apache.velocity.util.introspection.Introspector;
028:
029: /**
030: * Executor that simply tries to execute a get(key)
031: * operation. This will try to find a get(key) method
032: * for any type of object, not just objects that
033: * implement the Map interface as was previously
034: * the case.
035: *
036: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
037: * @version $Id: GetExecutor.java 463298 2006-10-12 16:10:32Z henning $
038: */
039: public class GetExecutor extends AbstractExecutor {
040: private final Introspector introspector;
041:
042: // This is still threadsafe because this object is only read except in the C'tor.
043: private Object[] params = {};
044:
045: /**
046: * @param log
047: * @param introspector
048: * @param clazz
049: * @param property
050: */
051: public GetExecutor(final Log log, final Introspector introspector,
052: final Class clazz, final String property) {
053: this .log = log;
054: this .introspector = introspector;
055:
056: // If you passed in null as property, we don't use the value
057: // for parameter lookup. Instead we just look for get() without
058: // any parameters.
059: //
060: // In any other case, the following condition will set up an array
061: // for looking up get(String) on the class.
062:
063: if (property != null) {
064: this .params = new Object[] { property };
065: }
066: discover(clazz);
067: }
068:
069: /**
070: * @param rlog
071: * @param introspector
072: * @param clazz
073: * @param property
074: * @deprecated RuntimeLogger is deprecated. Use the other constructor.
075: */
076: public GetExecutor(final RuntimeLogger rlog,
077: final Introspector introspector, final Class clazz,
078: final String property) {
079: this (new RuntimeLoggerLog(rlog), introspector, clazz, property);
080: }
081:
082: protected void discover(final Class clazz) {
083: try {
084: setMethod(introspector.getMethod(clazz, "get", params));
085: }
086: /**
087: * pass through application level runtime exceptions
088: */
089: catch (RuntimeException e) {
090: throw e;
091: } catch (Exception e) {
092: log.error("While looking for get('" + params[0]
093: + "') method:", e);
094: }
095: }
096:
097: /**
098: * @see org.apache.velocity.runtime.parser.node.AbstractExecutor#execute(java.lang.Object)
099: */
100: public Object execute(final Object o)
101: throws IllegalAccessException, InvocationTargetException {
102: return isAlive() ? getMethod().invoke(o, params) : null;
103: }
104: }
|