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   * Runs a static method with the object (or array) being processed. The returned object 
27   * will be exqueued on the main pipeline if it is not null. If the returned
28   * object is null, this stage will attempt to place the original object on the 
29   * branch specified by {@link #setNullResultBranchTag(String)}.
30   */
31  public class InvokeStaticMethodStage extends BaseStage {
32      
33      // Branch upon which the original objects will be enqueued if the defined
34      // Method returned a null result.
35      private String nullResultBranchKey;
36  
37      // Method used to process objects in the queue
38      private Method method;
39      
40      /**
41       * Creates a new instance of InvokeStaticMethodStage
42       */
43      public InvokeStaticMethodStage(Method method) {
44          super();
45          this.method = method;
46      }
47      
48      /** 
49       * Convenience method to create the new stage with String description of className, methodName and argumentType
50       *
51       * @param className The fully qualified class name, such as "java.lang.String" of the class in which the method resides
52       * @param methodName The name of the method
53       * @param argumentType The argument type of the method (Sorry, this doesn't support multiple argument methods)
54       */
55      public InvokeStaticMethodStage(String className, String methodName, String... argumentTypeNames) throws ClassNotFoundException, NoSuchMethodException {
56          Class clazz = InvokeStaticMethodStage.class.getClassLoader().loadClass(className);
57          Class[] argTypes = new Class[argumentTypeNames.length];
58          for (int i = 0; i < argumentTypeNames.length; i++) {
59              argTypes[i] = Class.forName(argumentTypeNames[i]);
60          }
61          
62          this.method = clazz.getMethod(methodName, argTypes);
63      }
64      
65      /** 
66       * Returns the Method object for the method that will be used to process
67       * objects in the queue.
68       */
69      public Method getMethod(){
70          return this.method;
71      }
72      
73      /** 
74       * <p>Calls the defined static method and exqueues the returned object if it is 
75       * not null, otherwise placing the original object on the branch specified
76       * by the nullResultBranchKey property if nullResultBranchKey is not null.</p>
77       *
78       * @param obj The object to be processed.
79       */
80      public void process(Object obj) throws StageException {
81          try {
82              Object result = this.method.invoke(null, obj);
83              if (result != null){
84                  this.emit(result);
85              } else if (nullResultBranchKey != null) {
86                  this.context.getBranchFeeder(nullResultBranchKey).feed(obj);
87              }
88          } catch (IllegalAccessException e){
89              throw new StageException(this, e);
90          } catch (InvocationTargetException e){
91              throw new StageException(this, e);
92          }       
93      }
94  
95      /**
96       * Getter for property nullResultBranchKey.
97       *
98       * @return Value of property nullResultBranchKey.
99       */
100     public String getNullResultBranchKey() {
101         return this.nullResultBranchKey;
102     }
103 
104     /**
105      * Setter for property nullResultBranchKey. If set to null (default) 
106      * then objects generating null results are simply dropped from the stream.
107      *
108      * @param nullResultBranchKey New value of property nullResultBranchKey.
109      */
110     public void setNullResultBranchKey(String nullResultBranchKey) {
111         this.nullResultBranchKey = nullResultBranchKey;
112     }
113 }