/**
 * 
 */
package com.trendrr.oss.messaging.channel;

import java.lang.reflect.Method;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.trendrr.oss.DynMap;


/**
 * A request handler that envokes methods on the object.
 * 
 * @author Dustin Norlander
 * @created May 18, 2012
 * 
 */
public class ChannelMethodRequestHandler implements ChannelRequestHandler {

	protected static Log log = LogFactory.getLog(ChannelMethodRequestHandler.class);
	

	Object object;
	HashMap<String, Method> methods = new HashMap<String, Method>();
	
	/**
	 * this object contains all the methods you want to be able to execute. 
	 * 
	 * Be extremely careful with overloaded methods.  if overloading
	 * make sure to use different # of params.
	 * 
	 * @param object
	 */
	public ChannelMethodRequestHandler(Object object) {
		this.object = object;
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.trendrr.topx2.channel.ChannelRequestHandler#handleRequest(java.lang.String, java.lang.Object[])
	 */
	@Override
	public Object handleRequest(String endpoint, Object... inputs)
			throws Exception {
		
		Method m = this.methods.get(endpoint+ "_" + inputs.length);
		if (m == null) {
			//search for the method to add to our map.
			for (Method method : this.object.getClass().getMethods()) {
				if (method.getName().equals(endpoint)) {
					if (method.getParameterTypes().length == inputs.length) {
						this.methods.put(endpoint+ "_" + inputs.length, method);
						m = method;
					}
				}
			}
		}
		if (m == null) {
			throw new Exception("Method: " + endpoint + " with " + inputs.length + " not found!");
		}
		return m.invoke(this.object, inputs);
	}
}

@@ -1141,16 +1141,162 @@
 on %7B%0A%09%09%0A
+%09%09if (this.object == null) %7B%0A%09%09%09throw new Exception(%22Object is null! (%22 + this + %22) enpoint: %22 + endpoint + %22 inputs: %22 + inputs + %22%22);%0A%09%09%7D%0A%09%09%0A%09%09%0A
 %09%09Method

throw better exception if object is somehow null