001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.core;
017:
018: import java.lang.reflect.InvocationTargetException;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.beanutils.MethodUtils;
023: import org.apache.commons.jelly.JellyTagException;
024: import org.apache.commons.jelly.MissingAttributeException;
025: import org.apache.commons.jelly.TagSupport;
026: import org.apache.commons.jelly.XMLOutput;
027:
028: /**
029: * A tag which calls a method in an object instantied by core:new
030: *
031: *
032: * @author Rodney Waldhoff
033: * @version $Revision: 155420 $
034: */
035: public class InvokeTag extends TagSupport implements ArgTagParent {
036:
037: /** the variable exported */
038: private String var;
039:
040: /** the variable where the method's exception is exported */
041: private String exceptionVar;
042:
043: /** the method to invoke */
044: private String methodName;
045:
046: /** the object to invoke the method on */
047: private Object onInstance;
048:
049: private List paramTypes = new ArrayList();
050: private List paramValues = new ArrayList();
051:
052: public InvokeTag() {
053: }
054:
055: /** Sets the name of the variable exported by this tag */
056: public void setVar(String var) {
057: this .var = var;
058: }
059:
060: /** Sets the name of a variable that exports the exception thrown by
061: * the method's invocation (if any)
062: */
063: public void setExceptionVar(String var) {
064: this .exceptionVar = var;
065: }
066:
067: public void setMethod(String method) {
068: this .methodName = method;
069: }
070:
071: public void setOn(Object instance) {
072: this .onInstance = instance;
073: }
074:
075: public void addArgument(Class type, Object value) {
076: paramTypes.add(type);
077: paramValues.add(value);
078: }
079:
080: // Tag interface
081: //-------------------------------------------------------------------------
082: public void doTag(XMLOutput output)
083: throws MissingAttributeException, JellyTagException {
084: if (null == methodName) {
085: throw new MissingAttributeException("method");
086: }
087: if (null == onInstance) {
088: throw new MissingAttributeException("on");
089: }
090:
091: invokeBody(output);
092:
093: Object[] values = paramValues.toArray();
094: Class[] types = (Class[]) (paramTypes
095: .toArray(new Class[paramTypes.size()]));
096:
097: Object result = null;
098: try {
099: result = MethodUtils.invokeMethod(onInstance, methodName,
100: values, types);
101: } catch (NoSuchMethodException e) {
102: throw new JellyTagException(e);
103: } catch (IllegalAccessException e) {
104: throw new JellyTagException(e);
105: } catch (InvocationTargetException e) {
106: if (null != exceptionVar) {
107: context.setVariable(exceptionVar, e
108: .getTargetException());
109: } else {
110: throw new JellyTagException("method " + methodName
111: + " threw exception: "
112: + e.getTargetException().getMessage(), e
113: .getTargetException());
114: }
115: } finally {
116: paramTypes.clear();
117: paramValues.clear();
118: }
119:
120: ArgTag parentArg = (ArgTag) (findAncestorWithClass(ArgTag.class));
121: if (null != parentArg) {
122: parentArg.setValue(result);
123: }
124: if (null != var) {
125: context.setVariable(var, result);
126: }
127: }
128: }
|