public static Throwable findOriginalThrowable(Throwable throwable)
  {
    //just to make sure we don't go to infinite loop. Most exception is less than 100 level deep.
    int depth = 0;
    Throwable original = throwable;
    while (original.getCause() != null && depth < 100)
    {
      original = original.getCause();
      depth++;
    }
    return original;
  }
@Test
  public void testFindOriginalThrowable()
  {
    ConnectException connectException = new ConnectException("Foo");
    RemoteInvocationException e = new RemoteInvocationException("Failed to get connect to a server", connectException);
    Throwable throwable = LoadBalancerUtil.findOriginalThrowable(e);

    Assert.assertEquals(throwable, connectException);

    //we only go as far as 100 level deep for finding exception
    Exception npe = new NullPointerException();
    Exception temp = npe;
    for (int i = 0; i < 100; i++)
    {
      e = new RemoteInvocationException(temp);
      temp = e;
    }

    throwable = LoadBalancerUtil.findOriginalThrowable(e);
    Assert.assertEquals(throwable, npe);

    //we add the 101th exception then we lost the reference to NullPointerException
    e = new RemoteInvocationException(temp);
    throwable = LoadBalancerUtil.findOriginalThrowable(e);
    Assert.assertFalse(throwable instanceof NullPointerException);
  }