001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.pipeline.stage;
019
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022
023 import org.apache.commons.pipeline.StageException;
024
025 /**
026 * Calls a method on the processed object giving it the arguments specified
027 * at the time of object construction.
028 *
029 *
030 * @version $Id: InvokeMethodStage.java 3742 2006-08-28 16:50:23Z kjn $
031 */
032 public class InvokeMethodStage extends BaseStage {
033 //private static final Log log = LogFactory.getLog(InvokeMethodStage.class);
034
035 private Method method;
036 private Object[] arguments;
037
038 /**
039 * Creates a new instance of InvokeMethodStage
040 */
041 public InvokeMethodStage(Method method){
042 this.method = method;
043 this.arguments = new Object[] { };
044 }
045
046 /**
047 * Creates a new instance of InvokeMethodStage
048 */
049 public InvokeMethodStage(Method method, Object... arguments) {
050 this.method = method;
051 this.arguments = arguments;
052 }
053
054 /**
055 * Creates a new instance of InvokeMethodStage from the class and method names.
056 */
057 public InvokeMethodStage(String className, String methodName, Object... arguments) throws ClassNotFoundException, NoSuchMethodException{
058 Class<?> clazz = InvokeMethodStage.class.getClassLoader().loadClass(className);
059 Class[] argTypes = new Class[arguments.length];
060 for (int i = 0; i < arguments.length; i++) argTypes[i] = arguments[i].getClass();
061
062 this.method = clazz.getMethod(methodName, argTypes);
063 this.arguments = arguments;
064 }
065
066 /** Returns the method to be accessed by processing
067 *@return the method
068 */
069 public Method getMethod(){
070 return this.method;
071 }
072
073 /** Returns the objects being used to invoke this method
074 *@return The objects being used
075 */
076 public Object[] getArguments(){
077 return this.arguments;
078 }
079
080 /** Calls the specified method on the object being processed and exqueues the result
081 * @param obj The object being processed.
082 */
083 public void process(Object obj) throws org.apache.commons.pipeline.StageException {
084 try {
085 Object result = method.invoke(obj, arguments);
086 this.emit(result);
087 } catch (IllegalAccessException e){
088 throw new StageException(this, e);
089 } catch (InvocationTargetException e){
090 throw new StageException(this, e);
091 }
092 }
093 }