예제 #1
0
  /**
   * Cleans the WAR file of all files relating to the currently installed version of the the AMP.
   *
   * @param warFileLocatio the war file location
   * @param moduleId the module id
   * @param preview indicates whether this is a preview installation
   */
  private void cleanWAR(String warFileLocation, String moduleId, boolean preview) {
    InstalledFiles installedFiles = new InstalledFiles(warFileLocation, moduleId);
    installedFiles.load();

    for (String add : installedFiles.getAdds()) {
      // Remove file
      removeFile(warFileLocation, add, preview);
    }
    for (String mkdir : installedFiles.getMkdirs()) {
      // Remove folder
      removeFile(warFileLocation, mkdir, preview);
    }
    for (Map.Entry<String, String> update : installedFiles.getUpdates().entrySet()) {
      if (preview == false) {
        // Recover updated file and delete backups
        File modified = new File(warFileLocation + update.getKey(), DETECTOR_AMP_AND_WAR);
        File backup = new File(warFileLocation + update.getValue(), DETECTOR_AMP_AND_WAR);
        modified.copyFrom(backup);
        backup.delete();
      }

      outputMessage(
          "Recovering file '" + update.getKey() + "' from backup '" + update.getValue() + "'",
          true);
    }
    // Now remove the installed files list
    String installedFilesPathInWar = installedFiles.getFilePathInWar();
    removeFile(warFileLocation, installedFilesPathInWar, preview);
    // Remove the module properties
    String modulePropertiesFileLocationInWar =
        ModuleDetailsHelper.getModulePropertiesFilePathInWar(moduleId);
    removeFile(warFileLocation, modulePropertiesFileLocationInWar, preview);
  }
예제 #2
0
  /**
   * Copies a file from the AMP location to the correct location in the WAR, interating on
   * directories where appropraite.
   *
   * @param ampFileLocation the AMP file location
   * @param warFileLocation the WAR file location
   * @param sourceDir the directory in the AMP to copy from. It must start with "/".
   * @param destinationDir the directory in the WAR to copy to. It must start with "/".
   * @param installedFiles a list of the currently installed files
   * @param preview indicates whether this is a preview install or not
   * @throws IOException throws any IOExpceptions thar are raised
   */
  private void copyToWar(
      String ampFileLocation,
      String warFileLocation,
      String sourceDir,
      String destinationDir,
      InstalledFiles installedFiles,
      boolean preview)
      throws IOException {
    if (sourceDir.length() == 0 || !sourceDir.startsWith("/")) {
      throw new IllegalArgumentException("sourceDir must start with '/'");
    }
    if (destinationDir.length() == 0 || !destinationDir.startsWith("/")) {
      throw new IllegalArgumentException("destinationDir must start with '/'");
    }

    // Handle source and destination if they are just the root '/'
    if (sourceDir.equals("/")) {
      sourceDir = "";
    }
    if (destinationDir.equals("/")) {
      destinationDir = "";
    }

    String sourceLocation = ampFileLocation + sourceDir;
    File ampConfig = new File(sourceLocation, DETECTOR_AMP_AND_WAR);

    java.io.File[] files = ampConfig.listFiles();
    if (files != null) {
      for (java.io.File sourceChild : files) {
        String destinationFileLocation =
            warFileLocation + destinationDir + "/" + sourceChild.getName();
        File destinationChild = new File(destinationFileLocation, DETECTOR_AMP_AND_WAR);
        if (sourceChild.isFile() == true) {
          String backupLocation = null;
          boolean createFile = false;
          if (destinationChild.exists() == false) {
            createFile = true;
          } else {
            // Backup file about to be updated
            backupLocation = BACKUP_DIR + "/" + generateGuid() + ".bin";
            if (preview == false) {
              File backupFile = new File(warFileLocation + backupLocation, DETECTOR_AMP_AND_WAR);
              backupFile.copyFrom(destinationChild);
            }
          }

          if (createFile == true) {
            installedFiles.addAdd(destinationDir + "/" + sourceChild.getName());
            this.outputMessage(
                "File '" + destinationDir + "/" + sourceChild.getName() + "' added to war from amp",
                true);
          } else {
            installedFiles.addUpdate(destinationDir + "/" + sourceChild.getName(), backupLocation);
            this.outputMessage(
                "WARNING: The file '"
                    + destinationDir
                    + "/"
                    + sourceChild.getName()
                    + "' is being updated by this module and has been backed-up to '"
                    + backupLocation
                    + "'",
                true);
          }
        } else {
          boolean mkdir = false;
          if (destinationChild.exists() == false) {
            mkdir = true;
          }

          copyToWar(
              ampFileLocation,
              warFileLocation,
              sourceDir + "/" + sourceChild.getName(),
              destinationDir + "/" + sourceChild.getName(),
              installedFiles,
              preview);
          if (mkdir == true) {
            installedFiles.addMkdir(destinationDir + "/" + sourceChild.getName());
            this.outputMessage(
                "Directory '" + destinationDir + "/" + sourceChild.getName() + "' added to war",
                true);
          }
        }
      }
    }
  }