Пример #1
0
 /**
  * Returns if the storage is closed.
  *
  * @param storageData Storage to check.
  * @return True if storage is closed, in any other situation false.
  */
 public boolean isStorageClosed(StorageData storageData) {
   for (StorageData existing : existingStoragesSet) {
     if (existing.getId().equals(storageData.getId())) {
       return existing.isStorageClosed();
     }
   }
   return false;
 }
Пример #2
0
 /**
  * Returns if the storage is opened, and thus if the write to the storage can be executed.
  *
  * @param storageData Storage to check.
  * @return True if storage is opened, otherwise false.
  */
 public boolean isStorageOpen(StorageData storageData) {
   for (StorageData existing : openedStoragesMap.keySet()) {
     if (existing.getId().equals(storageData.getId())) {
       return true;
     }
   }
   return false;
 }
Пример #3
0
 /**
  * Returns the local cached object that represent the {@link StorageData}.
  *
  * @param storageData Template.
  * @return Local object.
  * @throws BusinessException If local object can not be found.
  */
 private StorageData getLocalStorageDataObject(StorageData storageData) throws BusinessException {
   for (StorageData existing : existingStoragesSet) {
     if (existing.getId().equals(storageData.getId())) {
       return existing;
     }
   }
   throw new BusinessException(
       "Find storage " + storageData + ".", StorageErrorCodeEnum.STORAGE_DOES_NOT_EXIST);
 }
Пример #4
0
 /**
  * Returns the storage data based on the ID. This method can be helpful when the updated version
  * of {@link StorageData} needs to be retrieved.
  *
  * @param id ID of storage.
  * @return {@link StorageData}
  */
 public StorageData getStorageData(String id) {
   for (StorageData storageData : existingStoragesSet) {
     if (storageData.getId().equals(id)) {
       return storageData;
     }
   }
   return null;
 }
Пример #5
0
 /** {@inheritDoc} */
 @MethodLog
 public StorageData removeLabelFromStorage(
     StorageData storageData, AbstractStorageLabel<?> storageLabel) throws StorageException {
   try {
     storageManager.removeLabelFromStorage(storageData, storageLabel);
     return storageManager.getStorageData(storageData.getId());
   } catch (IOException | SerializationException e) {
     throw new StorageException(
         "Exception occurred trying to save storage data changes to disk.", e);
   }
 }
Пример #6
0
 /** {@inheritDoc} */
 @MethodLog
 public StorageData addLabelToStorage(
     StorageData storageData, AbstractStorageLabel<?> storageLabel, boolean doOverwrite)
     throws StorageException {
   try {
     storageManager.addLabelToStorage(storageData, storageLabel, doOverwrite);
     storageLabelDataDao.saveLabel(storageLabel);
     return storageManager.getStorageData(storageData.getId());
   } catch (IOException | SerializationException e) {
     throw new StorageException(
         "Exception occurred trying to save storage data changes to disk.", e);
   }
 }
Пример #7
0
  /**
   * Creates a storage form the uploaded local storage directory.
   *
   * @param localStorageData Local storage information.
   * @throws IOException If {@link IOException} occurs.
   * @throws BusinessException If there is not enough space for the unpacking the storage.
   * @throws SerializationException If serialization fails.
   */
  public void createStorageFromUploadedDir(final IStorageData localStorageData)
      throws IOException, BusinessException, SerializationException {
    long storageBytesLeft = getBytesHardDriveOccupancyLeft();
    if (localStorageData.getDiskSize() > storageBytesLeft) {
      throw new BusinessException(
          "Create the uploaded storage " + localStorageData + ".",
          StorageErrorCodeEnum.LOW_DISK_SPACE);
    }

    Path uploadPath = Paths.get(this.getStorageUploadsFolder());
    if (Files.notExists(uploadPath)) {
      throw new IOException(
          "Can not perform storage unpacking. The main upload path "
              + uploadPath.toString()
              + " does not exist.");
    } else {
      final MutableObject storageUploadPath = new MutableObject();
      final MutableObject uploadedStorageData = new MutableObject();
      final ISerializer serializer = getSerializationManagerProvider().createSerializer();
      Files.walkFileTree(
          uploadPath,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
              // skip all other files, search for the local data
              if (!file.toString()
                  .endsWith(
                      localStorageData.getId()
                          + StorageFileType.LOCAL_STORAGE_FILE.getExtension())) {
                return FileVisitResult.CONTINUE;
              }

              // when found confirm it is the one we wanted to upload
              InputStream inputStream = null;
              Input input = null;
              try {
                inputStream = Files.newInputStream(file, StandardOpenOption.READ);
                input = new Input(inputStream);
                Object deserialized = serializer.deserialize(input);
                if (Objects.equals(deserialized, localStorageData)) {
                  uploadedStorageData.setValue(new StorageData(localStorageData));
                  storageUploadPath.setValue(file.toAbsolutePath().getParent());
                  return FileVisitResult.TERMINATE;
                }
              } catch (SerializationException e) {
                log.warn("Error de-serializing local storage file.", e);
              } finally {
                if (null != input) {
                  input.close();
                }
              }
              return FileVisitResult.CONTINUE;
            }
          });

      // do the rest out of the file walk
      Path parentDir = (Path) storageUploadPath.getValue();
      StorageData storageData = (StorageData) uploadedStorageData.getValue();
      if (null != storageData && null != parentDir) {
        Path storageDir = getStoragePath(storageData);
        if (existingStoragesSet.add(storageData)) {
          if (Files.notExists(storageDir)) {
            printStorageCmrVersionWarn(storageData);

            Files.walkFileTree(parentDir, new CopyMoveFileVisitor(parentDir, storageDir, true));
            Path localInformation =
                getStoragePath(storageData)
                    .resolve(
                        storageData.getId() + StorageFileType.LOCAL_STORAGE_FILE.getExtension());
            Files.deleteIfExists(localInformation);
            writeStorageDataToDisk(storageData);
          } else {
            throw new IOException("Directory to place uploaded storage already exists.");
          }
        } else {
          log.info(
              "Uploaded storage on path "
                  + parentDir.toString()
                  + " contains the storage that is already available on the CMR. Dir will be deleted.");
          Files.walkFileTree(parentDir, new DeleteFileVisitor());
        }
      }
    }
  }