/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an
 * "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.powertac.util;

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

import org.apache.log4j.Logger;

/**
 * Static resource for dispatching messages. Helps avoid visitor or 
 * double-dispatch intrusions into domain types.
 * @author John Collins
 */
public class MessageDispatcher
{
  /**
   * Dispatches a call to methodName inside target based on the type of message.
   * Allows polymorphic method dispatch without the use of visitor or double
   * dispatch schemes, which produce nasty couplings with domain types.
   */
  static public Object dispatch (Object target, 
                                 String methodName, 
                                 Object... args)
  {
    Object result = null;
    Object message = args[0];
    Logger log = Logger.getLogger(target.getClass().getName());
    try {
      Class[] classes = new Class[args.length];
      for (int index = 0; index < args.length; index++) {
        classes[index] = (args[index].getClass());
      }
      Method method = target.getClass().getDeclaredMethod(methodName,
                                                          classes);
      log.debug("found method " + method);
      result = method.invoke(target, args);
    }
    catch (NoSuchMethodException nsm) {
      log.error("Could not find processor for incoming message of type " +
                              message.getClass().getName());
    }
    catch (Exception ex) {
      log.error("Exception calling message processor: " + ex.toString());
    }
    return result;
  }
}

@@ -1147,16 +1147,339 @@
  types.%0A
+   * %3Cp%3E%0A   * Note that this scheme finds only exact matches between types of arguments%0A   * and declared types of formal parameters for declared or inherited methods.%0A   * So it will not call a method with formal parameter types of %0A   * (Transaction, List) if the actual arguments are (Transaction, ArrayList).%0A   * %3C/p%3E%0A
    */%0A  
@@ -1631,16 +1631,80 @@
 gs)%0A  %7B%0A
+    Logger log = Logger.getLogger(target.getClass().getName());%0A
     Obje
@@ -1759,44 +1759,95 @@
     
-Logger log = Logger.getLogger(tar
+log.debug(%22Dispatch: %22 + args.length + %0A              %22 args, message class=%22 + messa
 ge
-t
 .get
@@ -1887,16 +1887,19 @@
    Class
+%3C?%3E
 %5B%5D class
@@ -1981,24 +1981,93 @@
  index++) %7B%0A
+        log.debug(%22arg class: %22 + args%5Bindex%5D.getClass().getName());%0A
         clas
@@ -2109,24 +2109,72 @@
 ));%0A      %7D%0A
+      // see if we can find the method directly%0A
       Method
@@ -2208,16 +2208,8 @@
 .get
-Declared
 Meth
@@ -2226,66 +2226,8 @@
 ame,
-%0A                                                         
  cla
@@ -2376,21 +2376,21 @@
     log.
-error
+debug
 (%22Could 
@@ -2402,107 +2402,37 @@
 ind 
-processor for incoming message of type %22 +%0A                              message.getClass().getName
+exact match: %22 + nsm.toString
 ());

Clarify capabilities