/* (non-Javadoc)
   * @see org.jboss.remoting.ServerInvocationHandler#invoke(org.jboss.remoting.InvocationRequest)
   */
  public Object invoke(InvocationRequest invocation) throws Throwable {
    Serializable key = (Serializable) invocation.getRequestPayload().get(OID);
    Remotable remotable = remotables.get(key);
    if (remotable == null)
      throw new IllegalArgumentException("Can't find remotable " + key + " in " + remotables);

    Object parameters[] = (Object[]) invocation.getParameter();
    SerializableMethod method = (SerializableMethod) parameters[0];
    Object args[] = (Object[]) parameters[1];
    ClassLoader loader = remotable.getClassLoader();
    Method realMethod = method.toMethod(loader);
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(loader);
      return realMethod.invoke(remotable.getTarget(), args);
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException(
          "unable to invoke "
              + realMethod
              + " on "
              + remotable.getTarget()
              + " with "
              + Arrays.toString(args),
          e);
    } catch (InvocationTargetException e) {
      throw e.getCause();
    } finally {
      Thread.currentThread().setContextClassLoader(oldLoader);
    }
  }