예제 #1
0
 /**
  * Prints the warnings if the CMR version saved in the storage does not exists or is different
  * from the current CMR version.
  *
  * @param storageData {@link StorageData}.
  */
 private void printStorageCmrVersionWarn(IStorageData storageData) {
   // inform if the version of the CMR differs or is not available
   if (null == storageData.getCmrVersion()) {
     log.warn(
         "The storage "
             + storageData
             + " does not define the CMR version. The storage might be unstable on the CMR version "
             + cmrVersion
             + ".");
   } else if (!Objects.equals(storageData.getCmrVersion(), cmrVersion)) {
     log.warn(
         "The storage "
             + storageData
             + " has different CMR version ("
             + storageData.getCmrVersion()
             + ") than the current CMR version("
             + cmrVersion
             + "). The storage might be unstable.");
   }
 }
예제 #2
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());
        }
      }
    }
  }
예제 #3
0
  /**
   * Checks for the uploaded files in the storage uploads folder and tries to extract data to the
   * default storage folder.
   *
   * @param packedStorageData Storage data that is packed in the file that needs to be unpacked.
   * @throws IOException IF {@link IOException} occurs during the file tree walk.
   * @throws BusinessException If there is not enough space for the unpacking the storage.
   */
  public void unpackUploadedStorage(final IStorageData packedStorageData)
      throws IOException, BusinessException {
    long storageBytesLeft = getBytesHardDriveOccupancyLeft();
    if (packedStorageData.getDiskSize() > storageBytesLeft) {
      throw new BusinessException(
          "Unpack the uploaded storage " + packedStorageData + ".",
          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 {
      Files.walkFileTree(
          uploadPath,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
              try {
                // skip all other files
                if (!file.toString().endsWith(StorageFileType.ZIP_STORAGE_FILE.getExtension())) {
                  return FileVisitResult.CONTINUE;
                }

                IStorageData storageData = getStorageDataFromZip(file);
                if (!Objects.equals(packedStorageData, storageData)) {
                  // go to next file if the file that we found does not hold the correct
                  // storage to unpack
                  return FileVisitResult.CONTINUE;
                }

                if (null != storageData) {
                  StorageData importedStorageData = new StorageData(storageData);
                  if (existingStoragesSet.add(importedStorageData)) {
                    printStorageCmrVersionWarn(storageData);

                    unzipStorageData(file, getStoragePath(importedStorageData));
                    Path localInformation =
                        getStoragePath(importedStorageData)
                            .resolve(
                                importedStorageData.getId()
                                    + StorageFileType.LOCAL_STORAGE_FILE.getExtension());
                    Files.deleteIfExists(localInformation);
                    writeStorageDataToDisk(importedStorageData);
                  } else {
                    log.info(
                        "Uploaded storage file "
                            + file.toString()
                            + " contains the storage that is already available on the CMR. File will be deleted.");
                  }
                }
                Files.deleteIfExists(file);
              } catch (Exception e) {
                log.warn(
                    "Uploaded storage file "
                        + file.toString()
                        + " is not of correct type and can not be extracted. File will be deleted.",
                    e);
              }
              return FileVisitResult.CONTINUE;
            }
          });
    }
  }
예제 #4
0
 /** {@inheritDoc} */
 public Path getStoragePath(IStorageData storageData) {
   return getDefaultStorageDirPath().resolve(storageData.getStorageFolder());
 }