View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pipeline.stage;
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  
23  import org.apache.commons.pipeline.StageException;
24  
25  /**
26   * Calls a method on the processed object giving it the arguments specified
27   * at the time of object construction.
28   *
29   *
30   * @version $Id: InvokeMethodStage.java 3742 2006-08-28 16:50:23Z kjn $
31   */
32  public class InvokeMethodStage extends BaseStage {
33      //private static final Log log = LogFactory.getLog(InvokeMethodStage.class);
34      
35      private Method method;
36      private Object[] arguments;
37      
38      /**
39       * Creates a new instance of InvokeMethodStage
40       */
41      public InvokeMethodStage(Method method){
42          this.method = method;
43          this.arguments = new Object[] { };
44      }
45      
46      /**
47       * Creates a new instance of InvokeMethodStage
48       */
49      public InvokeMethodStage(Method method, Object... arguments) {
50          this.method = method;
51          this.arguments = arguments;
52      }
53      
54      /**
55       * Creates a new instance of InvokeMethodStage from the class and method names.
56       */
57      public InvokeMethodStage(String className, String methodName, Object... arguments) throws ClassNotFoundException, NoSuchMethodException{
58          Class<?> clazz = InvokeMethodStage.class.getClassLoader().loadClass(className);
59          Class[] argTypes = new Class[arguments.length];
60          for (int i = 0; i < arguments.length; i++) argTypes[i] = arguments[i].getClass();
61          
62          this.method = clazz.getMethod(methodName, argTypes);
63          this.arguments = arguments;
64      }
65      
66      /** Returns the method to be accessed by processing
67       *@return the method
68       */
69      public Method getMethod(){
70          return this.method;
71      }
72      
73      /** Returns the objects being used to invoke this method
74       *@return The objects being used
75       */
76      public Object[] getArguments(){
77          return this.arguments;
78      }
79      
80      /** Calls the specified method on the object being processed and exqueues the result
81       * @param obj The object being processed.
82       */
83      public void process(Object obj) throws org.apache.commons.pipeline.StageException {        
84          try {
85              Object result = method.invoke(obj, arguments);
86              this.emit(result);
87          } catch (IllegalAccessException e){
88              throw new StageException(this, e);
89          } catch (InvocationTargetException e){
90              throw new StageException(this, e);
91          }
92      }
93  }