Package org.jibeframework.core.app.method

Source Code of org.jibeframework.core.app.method.MethodHolder

package org.jibeframework.core.app.method;

import groovy.lang.GroovyObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StopWatch;

/**
* This class contains object and method reference required for invocation
*
* @author dhalupa
*
*/
public class MethodHolder {
  private Object bean;
  private Method method;
  private Object[] arguments;
  private String methodId;
  private static final Log logger = LogFactory.getLog(MethodHolder.class);

  public MethodHolder(String methodId, Object serviceBean, Method method, Object[] arguments) {
    super();
    this.bean = serviceBean;
    this.method = method;
    this.arguments = arguments;
    this.methodId = methodId;
  }

  /**
   * Invoke method
   *
   * @param args
   * @return
   */
  public Object invoke() {
    StopWatch sw = null;
    if (logger.isDebugEnabled()) {
      sw = new StopWatch();
      sw.start();
    }
    Object retValue = null;
    if (bean instanceof GroovyObject) {
      retValue = ((GroovyObject) bean).invokeMethod(method.getName(), arguments);
    } else {
      retValue = ReflectionUtils.invokeMethod(method, bean, arguments);
    }

    if (logger.isDebugEnabled()) {
      sw.stop();
      List<String> argsDescr = new ArrayList<String>(arguments.length);
      for (int i = 0; i < arguments.length; i++) {
        if (arguments[i] != null && arguments[i] instanceof String) {
          argsDescr.add((String) arguments[i]);
        } else {
          argsDescr.add(arguments[i] != null ? arguments[i].getClass().getName() : "null");
        }
      }
      logger.debug("Invoking  " + methodId + " with arguments " + argsDescr + ": " + sw.getTotalTimeMillis());

    }
    return retValue;
  }

  public Method getMethod() {
    return method;
  }

  public Double getQuality() {
    double i = 0;
    for (Object arg : arguments) {
      if (arg != null) {
        i++;
      }
    }
    if (arguments.length != 0) {
      return i / arguments.length;
    } else {
      return 1d;
    }
  }

}
TOP

Related Classes of org.jibeframework.core.app.method.MethodHolder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.