/* (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);
    }
  }
 public void removeRemotable(Remotable remotable) {
   log.debug("removeRemotable " + remotable);
   Remotable previous = remotables.remove(remotable.getId());
   assert previous == remotable
       : "there was a different remotable under "
           + remotable.getId()
           + " ("
           + previous
           + " != "
           + remotable
           + ")";
 }
 public void addRemotable(Remotable remotable) {
   log.debug("addRemotable " + remotable);
   Remotable previous = remotables.put(remotable.getId(), remotable);
   assert previous == null : "there was a previous remotable under " + remotable.getId();
 }