View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.uif.component;
17  
18  import java.io.Serializable;
19  import java.lang.reflect.Method;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.core.api.exception.RiceRuntimeException;
23  import org.kuali.rice.krad.datadictionary.Copyable;
24  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
25  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
26  import org.springframework.util.MethodInvoker;
27  import org.springframework.util.ReflectionUtils;
28  
29  /**
30   * Extends <code>MethodInvoker</code> to add properties for specifying
31   * a method for invocation within the UIF
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  @BeanTag(name = "methodInvokerConfig-bean", parent = "Uif-MethodInvokerConfig")
36  public class MethodInvokerConfig extends MethodInvoker implements Serializable, Copyable {
37      private static final long serialVersionUID = 6626790175367500081L;
38  
39      private String staticMethod;
40      private Class[] argumentTypes;
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      public void prepare() throws ClassNotFoundException, NoSuchMethodException {
47          if ((getTargetObject() == null) && (getTargetClass() != null)) {
48              try {
49                  setTargetObject(getTargetClass().newInstance());
50              } catch (Exception e) {
51                  throw new RiceRuntimeException("Unable to create new intance of target class", e);
52              }
53          }
54  
55          super.prepare();
56      }
57  
58      /**
59       * Set a fully qualified static method name to invoke,
60       * e.g. "example.MyExampleClass.myExampleMethod".
61       * Convenient alternative to specifying targetClass and targetMethod.
62       *
63       * @return static method to invoke
64       */
65      @BeanTagAttribute(name="staticMethod")
66      public String getStaticMethod() {
67          return staticMethod;
68      }
69  
70      /**
71       * Override to catch a set staticMethod since super does
72       * not contain a getter
73       *
74       * @param staticMethod static method to invoke
75       */
76      @Override
77      public void setStaticMethod(String staticMethod) {
78          super.setStaticMethod(staticMethod);
79          this.staticMethod = staticMethod;
80      }
81  
82      /**
83       * Declared argument types for the method to be invoked, if not set the types will
84       * be retrieved based on the target class and target name
85       *
86       * @return method argument types
87       */
88      @BeanTagAttribute(name="argumentTypes",type= BeanTagAttribute.AttributeType.LISTBEAN)
89      public Class[] getArgumentTypes() {
90          if (argumentTypes == null) {
91              return getMethodArgumentTypes();
92          }
93  
94          return argumentTypes;
95      }
96  
97      /**
98       * Setter for the method argument types that should be invoked
99       *
100      * @param argumentTypes
101      */
102     public void setArgumentTypes(Class[] argumentTypes) {
103         this.argumentTypes = argumentTypes;
104     }
105 
106     /**
107      * Finds the method on the target class that matches the target name and
108      * returns the declared parameter types
109      *
110      * @return method parameter types
111      */
112     protected Class[] getMethodArgumentTypes() {
113         if (StringUtils.isNotBlank(staticMethod)) {
114             int lastDotIndex = this.staticMethod.lastIndexOf('.');
115             if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
116                 throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " +
117                         "e.g. 'example.MyExampleClass.myExampleMethod'");
118             }
119             String className = this.staticMethod.substring(0, lastDotIndex);
120             String methodName = this.staticMethod.substring(lastDotIndex + 1);
121             try {
122                 setTargetClass(resolveClassName(className));
123             } catch (ClassNotFoundException e) {
124                 throw new RuntimeException("Unable to get class for name: " + className);
125             }
126             setTargetMethod(methodName);
127         }
128 
129         Method matchingCandidate = findMatchingMethod();
130         if (matchingCandidate != null) {
131             return matchingCandidate.getParameterTypes();
132         }
133 
134         Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
135         for (Method candidate : candidates) {
136             if (candidate.getName().equals(getTargetMethod())) {
137                 return candidate.getParameterTypes();
138             }
139         }
140 
141         return null;
142     }
143 
144     /**
145      * @see Copyable#clone()
146      */
147     @Override
148     public MethodInvokerConfig clone() throws CloneNotSupportedException {
149         return (MethodInvokerConfig) super.clone();
150     }
151 
152 }