@Override
  public synchronized T fetchEntity()
      throws EntityNotFoundException, EntityVersionMismatchException {
    EntityClientEndpoint endpoint = null;
    try {
      ClientInstanceID clientInstanceID =
          new ClientInstanceID(this.nextClientInstanceID.getAndIncrement());
      EntityDescriptor entityDescriptor =
          new EntityDescriptor(getEntityID(), clientInstanceID, this.version);
      endpoint =
          this.entityManager.fetchEntity(
              entityDescriptor, entityClientService.getMessageCodec(), null);
    } catch (EntityException e) {
      // In this case, we want to close the endpoint but still throw back the exception.
      // Note that we must externally only present the specific exception types we were expecting.
      // Thus, we need to check
      // that this is one of those supported types, asserting that there was an unexpected wire
      // inconsistency, otherwise.
      if (e instanceof EntityNotFoundException) {
        throw (EntityNotFoundException) e;
      } else if (e instanceof EntityVersionMismatchException) {
        throw (EntityVersionMismatchException) e;
      } else {
        throw Assert.failure("Unsupported exception type returned to fetch", e);
      }
    } catch (final Throwable t) {
      Util.printLogAndRethrowError(t, logger);
    }

    // Note that a failure to resolve the endpoint would have thrown so this can't be null.
    if (endpoint == null) {
      Assert.assertNotNull(endpoint);
    }
    return (T) entityClientService.create(endpoint);
  }
 @Override
 public C reconfigure(C configuration) throws EntityException {
   EntityID entityID = getEntityID();
   try {
     return entityClientService.deserializeConfiguration(
         this.entityManager
             .reconfigureEntity(
                 entityID, this.version, entityClientService.serializeConfiguration(configuration))
             .get());
   } catch (EntityException e) {
     throw ExceptionUtils.addLocalStackTraceToEntityException(e);
   } catch (InterruptedException e) {
     // We don't expect an interruption here.
     throw new RuntimeException(e);
   }
 }
 @Override
 public void create(C configuration)
     throws EntityNotProvidedException, EntityAlreadyExistsException,
         EntityVersionMismatchException {
   EntityID entityID = getEntityID();
   try {
     this.entityManager
         .createEntity(
             entityID, this.version, entityClientService.serializeConfiguration(configuration))
         .get();
   } catch (EntityException e) {
     // Note that we must externally only present the specific exception types we were expecting.
     // Thus, we need to check
     // that this is one of those supported types, asserting that there was an unexpected wire
     // inconsistency, otherwise.
     e = ExceptionUtils.addLocalStackTraceToEntityException(e);
     if (e instanceof EntityNotProvidedException) {
       throw (EntityNotProvidedException) e;
     } else if (e instanceof EntityAlreadyExistsException) {
       throw (EntityAlreadyExistsException) e;
     } else if (e instanceof EntityVersionMismatchException) {
       throw (EntityVersionMismatchException) e;
     } else {
       // WARNING:  Assert.failure returns an exception, instead of throwing one.
       throw Assert.failure("Unsupported exception type returned to create", e);
     }
   } catch (InterruptedException e) {
     // We don't expect an interruption here.
     throw new RuntimeException(e);
   }
 }