View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.core;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.beanutils.MethodUtils;
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.TagSupport;
26  import org.apache.commons.jelly.XMLOutput;
27  
28  /***
29    * A tag which calls a method in an object instantied by core:new
30    *
31    *
32    * @author Rodney Waldhoff
33    * @version $Revision: 155420 $
34    */
35  public class InvokeTag extends TagSupport implements ArgTagParent {
36  
37      /*** the variable exported */
38      private String var;
39  
40      /*** the variable where the method's exception is exported */
41      private String exceptionVar;
42  
43      /*** the method to invoke */
44      private String methodName;
45  
46      /*** the object to invoke the method on */
47      private Object onInstance;
48  
49      private List paramTypes = new ArrayList();
50      private List paramValues = new ArrayList();
51  
52      public InvokeTag() {
53      }
54  
55      /*** Sets the name of the variable exported by this tag */
56      public void setVar(String var) {
57          this.var = var;
58      }
59  
60      /*** Sets the name of a variable that exports the exception thrown by
61       * the method's invocation (if any)
62       */
63      public void setExceptionVar(String var) {
64          this.exceptionVar = var;
65      }
66  
67      public void setMethod(String method) {
68          this.methodName = method;
69      }
70  
71      public void setOn(Object instance) {
72          this.onInstance = instance;
73      }
74  
75      public void addArgument(Class type, Object value) {
76          paramTypes.add(type);
77          paramValues.add(value);
78      }
79  
80      // Tag interface
81      //-------------------------------------------------------------------------
82      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
83          if ( null == methodName) {
84              throw new MissingAttributeException( "method" );
85          }
86          if ( null == onInstance ) {
87              throw new MissingAttributeException( "on" );
88          }
89  
90          invokeBody(output);
91  
92          Object[] values = paramValues.toArray();
93          Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()]));
94  
95          Object result = null;
96          try {
97              result = MethodUtils.invokeMethod(onInstance,methodName,values,types);
98          }
99          catch (NoSuchMethodException e) {
100             throw new JellyTagException(e);
101         }
102         catch (IllegalAccessException e) {
103             throw new JellyTagException(e);
104         }
105         catch (InvocationTargetException e) {
106             if(null != exceptionVar) {
107                 context.setVariable(exceptionVar,e.getTargetException());
108             } else {
109                 throw new JellyTagException("method " + methodName +
110                     " threw exception: "+ e.getTargetException().getMessage(),
111                     e.getTargetException() );
112             }
113         }
114         finally {
115             paramTypes.clear();
116             paramValues.clear();
117         }
118 
119         ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class));
120         if(null != parentArg) {
121             parentArg.setValue(result);
122         }
123         if(null != var) {
124             context.setVariable(var, result);
125         }
126     }
127 }