protected void exceptionIfImageSizeGreaterThanAvailableCapacity(String url) {
    if (CoreGlobalProperty.UNIT_TEST_ON) {
      return;
    }

    url = url.trim();
    if (!url.startsWith("http") && !url.startsWith("https")) {
      return;
    }

    String len;
    try {
      HttpHeaders header = restf.getRESTTemplate().headForHeaders(url);
      len = header.getFirst("Content-Length");
    } catch (Exception e) {
      logger.warn(String.format("cannot get image.  The image url : %s.", url));
      return;
    }
    if (len == null) {
      return;
    }

    long size = Long.valueOf(len);
    if (size > self.getAvailableCapacity()) {
      throw new OperationFailureException(
          errf.stringToOperationError(
              String.format(
                  "the backup storage[uuid:%s, name:%s] has not enough capacity to download the image[%s]."
                      + "Required size:%s, available size:%s",
                  self.getUuid(), self.getName(), url, size, self.getAvailableCapacity())));
    }
  }
 protected void updateCapacity(Long totalCapacity, Long availableCapacity) {
   if (totalCapacity != null && availableCapacity != null) {
     self.setTotalCapacity(totalCapacity);
     self.setAvailableCapacity(availableCapacity);
     dbf.update(self);
   }
 }
 protected void checkState(Message msg) {
   if (!stateChecker.isOperationAllowed(msg.getClass().getName(), self.getState().toString())) {
     throw new OperationFailureException(
         errf.stringToOperationError(
             String.format(
                 "backup storage cannot proceed message[%s] because its state is %s",
                 msg.getClass().getName(), self.getState())));
   }
 }
 protected BackupStorageVO updateBackupStorage(APIUpdateBackupStorageMsg msg) {
   boolean update = false;
   if (msg.getName() != null) {
     self.setName(msg.getName());
     update = true;
   }
   if (msg.getDescription() != null) {
     self.setDescription(msg.getDescription());
     update = true;
   }
   return update ? self : null;
 }
  @Transactional
  private void handle(ReturnBackupStorageMsg msg) {
    self =
        dbf.getEntityManager()
            .find(BackupStorageVO.class, self.getUuid(), LockModeType.PESSIMISTIC_WRITE);
    long availSize = self.getAvailableCapacity() + msg.getSize();
    if (availSize > self.getTotalCapacity()) {
      availSize = self.getTotalCapacity();
    }

    self.setAvailableCapacity(availSize);
    dbf.getEntityManager().merge(self);
    bus.reply(msg, new ReturnBackupStorageReply());
  }
  private void handle(BackupStorageDeletionMsg msg) {
    BackupStorageInventory inv = BackupStorageInventory.valueOf(self);
    extpEmitter.beforeDelete(inv);
    deleteHook();
    extpEmitter.afterDelete(inv);

    BackupStorageDeletionReply reply = new BackupStorageDeletionReply();
    tracker.untrackHook(self.getUuid());
    bus.reply(msg, reply);
  }
  protected void changeStatus(BackupStorageStatus status) {
    if (status == self.getStatus()) {
      return;
    }

    BackupStorageStatus oldStatus = self.getStatus();

    self.setStatus(status);
    dbf.update(self);

    BackupStorageStatusChangedData d = new BackupStorageStatusChangedData();
    d.setBackupStorageUuid(self.getUuid());
    d.setNewStatus(status.toString());
    d.setOldStatus(oldStatus.toString());
    d.setInventory(BackupStorageInventory.valueOf(self));
    evtf.fire(BackupStorageCanonicalEvents.BACKUP_STORAGE_STATUS_CHANGED, d);

    logger.debug(
        String.format("change backup storage[uuid:%s] status to %s", self.getUuid(), status));
  }
 private void doScanImages() {
   try {
     List<ImageInventory> images = this.scanImages();
   } catch (Exception e) {
     logger.warn(
         String.format(
             "Unhandled exception happened while scanning backup storage[uuid:%s]",
             self.getUuid()),
         e);
   }
 }
  private void handle(APIReconnectBackupStorageMsg msg) {
    final APIReconnectBackupStorageEvent evt = new APIReconnectBackupStorageEvent(msg.getId());
    ConnectBackupStorageMsg cmsg = new ConnectBackupStorageMsg();
    cmsg.setBackupStorageUuid(self.getUuid());
    bus.makeTargetServiceIdByResourceUuid(cmsg, BackupStorageConstant.SERVICE_ID, self.getUuid());
    bus.send(
        cmsg,
        new CloudBusCallBack(msg) {
          @Override
          public void run(MessageReply reply) {
            if (!reply.isSuccess()) {
              evt.setErrorCode(reply.getError());
            } else {
              self = dbf.reload(self);
              evt.setInventory(getSelfInventory());
            }

            bus.publish(evt);
          }
        });
  }
  protected void handle(APIChangeBackupStorageStateMsg msg) {
    APIChangeBackupStorageStateEvent evt = new APIChangeBackupStorageStateEvent(msg.getId());

    BackupStorageState currState = self.getState();
    BackupStorageStateEvent event = BackupStorageStateEvent.valueOf(msg.getStateEvent());
    BackupStorageState nextState = AbstractBackupStorage.getNextState(currState, event);

    try {
      extpEmitter.preChange(self, event);
    } catch (BackupStorageException e) {
      evt.setErrorCode(
          errf.instantiateErrorCode(SysErrors.CHANGE_RESOURCE_STATE_ERROR, e.getMessage()));
      bus.publish(evt);
      return;
    }

    extpEmitter.beforeChange(self, event);
    changeStateHook(event, nextState);
    self.setState(nextState);
    self = dbf.updateAndRefresh(self);
    extpEmitter.afterChange(self, event, currState);
    evt.setInventory(getSelfInventory());
    bus.publish(evt);
  }