Exemplo n.º 1
0
 /** @return Returns the custom file mapping properties or null if they weren't overwritten */
 private Properties getCustomFileMappings(String ampFileLocation) {
   File file =
       new File(
           ampFileLocation + "/" + FILE_MAPPING_PROPERTIES,
           ModuleManagementTool.DETECTOR_AMP_AND_WAR);
   if (!file.exists()) {
     // Nothing there
     return null;
   }
   Properties mappingProperties = new Properties();
   InputStream is = null;
   try {
     is = new BufferedInputStream(new FileInputStream(file));
     mappingProperties.load(is);
   } catch (IOException exception) {
     throw new ModuleManagementToolException(
         "Unable to load default extension file mapping properties.", exception);
   } finally {
     if (is != null) {
       try {
         is.close();
       } catch (Throwable e) {
       }
     }
   }
   return mappingProperties;
 }
Exemplo n.º 2
0
 @NotNull
 @Override
 public Value getLastUpdate() {
   return valueTableDirectory.exists()
       ? DateTimeType.get().valueOf(new Date(valueTableDirectory.lastModified()))
       : DateTimeType.get().nullValue();
 }
Exemplo n.º 3
0
  /**
   * Lists all the currently installed modules in the WAR
   *
   * @param warLocation the war location
   */
  public void listModules(String warLocation) {
    ModuleDetails moduleDetails = null;
    boolean previous = this.verbose;
    this.verbose = true;
    try {
      File moduleDir = new File(warLocation + MODULE_DIR, DETECTOR_AMP_AND_WAR);
      if (moduleDir.exists() == false) {
        outputMessage("No modules are installed in this WAR file");
      }

      java.io.File[] dirs = moduleDir.listFiles();
      if (dirs != null && dirs.length != 0) {
        for (java.io.File dir : dirs) {
          if (dir.isDirectory() == true) {
            File moduleProperties =
                new File(dir.getPath() + "/module.properties", DETECTOR_AMP_AND_WAR);
            if (moduleProperties.exists() == true) {
              try {
                InputStream is = new FileInputStream(moduleProperties);
                moduleDetails = ModuleDetailsHelper.createModuleDetailsFromPropertiesStream(is);
              } catch (IOException exception) {
                throw new ModuleManagementToolException(
                    "Unable to open module properties file '" + moduleProperties.getPath() + "'",
                    exception);
              }

              outputMessage(
                  "Module '" + moduleDetails.getId() + "' installed in '" + warLocation + "'");
              outputMessage("   Title:        " + moduleDetails.getTitle(), true);
              outputMessage("   Version:      " + moduleDetails.getVersion(), true);
              outputMessage("   Install Date: " + moduleDetails.getInstallDate(), true);
              outputMessage("   Description:   " + moduleDetails.getDescription(), true);
            }
          }
        }
      } else {
        outputMessage("No modules are installed in this WAR file");
      }
    } finally {
      this.verbose = previous;
    }
  }
Exemplo n.º 4
0
 /**
  * Removes a file from the given location in the war file.
  *
  * @param warLocation the war file location
  * @param filePath the path to the file that is to be deleted
  * @param preview indicates whether this is a preview install
  */
 private void removeFile(String warLocation, String filePath, boolean preview) {
   File removeFile = new File(warLocation + filePath, DETECTOR_AMP_AND_WAR);
   if (removeFile.exists() == true) {
     outputMessage("Removing file '" + filePath + "' from war", true);
     if (preview == false) {
       removeFile.delete();
     }
   } else {
     outputMessage(
         "The file '" + filePath + "' was expected for removal but was not present in the war",
         true);
   }
 }
Exemplo n.º 5
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);
          }
        }
      }
    }
  }
Exemplo n.º 6
0
  /**
   * Installs a given AMP file into a given WAR file.
   *
   * @param ampFileLocation the location of the AMP file to be installed
   * @param warFileLocation the location of the WAR file into which the AMP file is to be installed.
   * @param preview indicates whether this should be a preview install. This means that the process
   *     of installation will be followed and reported, but the WAR file will not be modified.
   * @param forceInstall indicates whether the installed files will be replaces reguarless of the
   *     currently installed version of the AMP. Generally used during development of the AMP.
   * @param backupWAR indicates whether we should backup the war we are modifying or not
   */
  public void installModule(
      String ampFileLocation,
      String warFileLocation,
      boolean preview,
      boolean forceInstall,
      boolean backupWAR) {
    try {
      outputMessage("Installing AMP '" + ampFileLocation + "' into WAR '" + warFileLocation + "'");

      if (preview == false) {
        // Make sure the module and backup directory exisits in the WAR file
        File moduleDir = new File(warFileLocation + MODULE_DIR, DETECTOR_AMP_AND_WAR);
        if (moduleDir.exists() == false) {
          moduleDir.mkdir();
        }
        File backUpDir = new File(warFileLocation + BACKUP_DIR, DETECTOR_AMP_AND_WAR);
        if (backUpDir.exists() == false) {
          backUpDir.mkdir();
        }

        // Make a backup of the war we are going to modify
        if (backupWAR == true) {
          java.io.File warFile = new java.io.File(warFileLocation);
          if (warFile.exists() == false) {
            throw new ModuleManagementToolException(
                "The war file '" + warFileLocation + "' does not exist.");
          }
          String backupLocation = warFileLocation + "-" + System.currentTimeMillis() + ".bak";
          java.io.File backup = new java.io.File(backupLocation);
          copyFile(warFile, backup);

          outputMessage("WAR has been backed up to '" + backupLocation + "'");
        }
      }

      // Get the details of the installing module
      String propertiesLocation = ampFileLocation + "/module.properties";
      ModuleDetails installingModuleDetails =
          ModuleDetailsHelper.createModuleDetailsFromPropertyLocation(propertiesLocation);
      if (installingModuleDetails == null) {
        throw new ModuleManagementToolException(
            "No module.properties file has been found in the installing .amp file '"
                + ampFileLocation
                + "'");
      }
      String installingId = installingModuleDetails.getId();
      VersionNumber installingVersion = installingModuleDetails.getVersion();

      // Check that the target war has the necessary dependencies for this install
      List<ModuleDependency> installingModuleDependencies =
          installingModuleDetails.getDependencies();
      List<ModuleDependency> missingDependencies = new ArrayList<ModuleDependency>(0);
      for (ModuleDependency dependency : installingModuleDependencies) {
        String dependencyId = dependency.getDependencyId();
        ModuleDetails dependencyModuleDetails =
            ModuleDetailsHelper.createModuleDetailsFromWarAndId(warFileLocation, dependencyId);
        // Check the dependency.  The API specifies that a null returns false, so no null check is
        // required
        if (!dependency.isValidDependency(dependencyModuleDetails)) {
          missingDependencies.add(dependency);
          continue;
        }
      }
      if (missingDependencies.size() > 0) {
        throw new ModuleManagementToolException(
            "The following modules must first be installed: " + missingDependencies);
      }

      // Try to find an installed module by the ID
      ModuleDetails installedModuleDetails =
          ModuleDetailsHelper.createModuleDetailsFromWarAndId(warFileLocation, installingId);
      if (installedModuleDetails == null) {
        // It might be there as one of the aliases
        List<String> installingAliases = installingModuleDetails.getAliases();
        for (String installingAlias : installingAliases) {
          ModuleDetails installedAliasModuleDetails =
              ModuleDetailsHelper.createModuleDetailsFromWarAndId(warFileLocation, installingAlias);
          if (installedAliasModuleDetails == null) {
            // There is nothing by that alias
            continue;
          }
          // We found an alias and will treat it as the same module
          installedModuleDetails = installedAliasModuleDetails;
          outputMessage(
              "Module '"
                  + installingAlias
                  + "' is installed and is an alias of '"
                  + installingId
                  + "'");
          break;
        }
      }

      // Now clean up the old instance
      if (installedModuleDetails != null) {
        String installedId = installedModuleDetails.getId();
        VersionNumber installedVersion = installedModuleDetails.getVersion();

        int compareValue = installedVersion.compareTo(installingVersion);
        if (forceInstall == true || compareValue == -1) {
          if (forceInstall == true) {
            // Warn of forced install
            outputMessage(
                "WARNING: The installation of this module is being forced.  All files will be removed and replaced regardless of exiting versions present.");
          }

          // Trying to update the extension, old files need to cleaned before we proceed
          outputMessage(
              "Clearing out files relating to version '"
                  + installedVersion
                  + "' of module '"
                  + installedId
                  + "'");
          cleanWAR(warFileLocation, installedId, preview);
        } else if (compareValue == 0) {
          // Trying to install the same extension version again
          outputMessage(
              "WARNING: This version of this module is already installed in the WAR. Installation skipped.");
          return;
        } else if (compareValue == 1) {
          // Trying to install an earlier version of the extension
          outputMessage(
              "WARNING: A later version of this module is already installed in the WAR. Installation skipped.");
          return;
        }
      }

      // Check if a custom mapping file has been defined
      Properties fileMappingProperties = null;
      Properties customFileMappingProperties = getCustomFileMappings(ampFileLocation);
      if (customFileMappingProperties == null) {
        fileMappingProperties = defaultFileMappingProperties;
      } else {
        fileMappingProperties = new Properties();
        // A custom mapping file was present.  Check if it must inherit the default mappings.
        String inheritDefaultStr =
            customFileMappingProperties.getProperty(PROP_INHERIT_DEFAULT, "true");
        if (inheritDefaultStr.equalsIgnoreCase("true")) {
          fileMappingProperties.putAll(defaultFileMappingProperties);
        }
        fileMappingProperties.putAll(customFileMappingProperties);
        fileMappingProperties.remove(PROP_INHERIT_DEFAULT);
      }

      // Copy the files from the AMP file into the WAR file
      outputMessage(
          "Adding files relating to version '"
              + installingVersion
              + "' of module '"
              + installingId
              + "'");
      InstalledFiles installedFiles = new InstalledFiles(warFileLocation, installingId);
      for (Map.Entry<Object, Object> entry : fileMappingProperties.entrySet()) {
        // The file mappings are expected to start with "/"
        String mappingSource = (String) entry.getKey();
        if (mappingSource.length() == 0 || !mappingSource.startsWith("/")) {
          throw new AlfrescoRuntimeException(
              "File mapping sources must start with '/', but was: " + mappingSource);
        }
        String mappingTarget = (String) entry.getValue();
        if (mappingTarget.length() == 0 || !mappingTarget.startsWith("/")) {
          throw new AlfrescoRuntimeException(
              "File mapping targets must start with '/' but was '" + mappingTarget + "'");
        }

        // Run throught the files one by one figuring out what we are going to do during the copy
        copyToWar(
            ampFileLocation,
            warFileLocation,
            mappingSource,
            mappingTarget,
            installedFiles,
            preview);

        if (preview == false) {
          // Get a reference to the source folder (if it isn't present don't do anything)
          File source = new File(ampFileLocation + "/" + mappingSource, DETECTOR_AMP_AND_WAR);
          if (source != null && source.list() != null) {
            // Get a reference to the destination folder
            File destination =
                new File(warFileLocation + "/" + mappingTarget, DETECTOR_AMP_AND_WAR);
            if (destination == null) {
              throw new ModuleManagementToolException(
                  "The destination folder '"
                      + mappingTarget
                      + "' as specified in mapping properties does not exist in the war");
            }
            // Do the bulk copy since this is quicker than copying files one by one
            destination.copyAllFrom(source);
          }
        }
      }

      if (preview == false) {
        // Save the installed file list
        installedFiles.save();

        // Update the installed module details
        installingModuleDetails.setInstallState(ModuleInstallState.INSTALLED);
        installingModuleDetails.setInstallDate(new Date());
        ModuleDetailsHelper.saveModuleDetails(warFileLocation, installingModuleDetails);

        // Update the zip files
        File.update();

        // Set the modified date
        java.io.File warFile = new java.io.File(warFileLocation);
        if (warFile.exists()) {
          warFile.setLastModified(System.currentTimeMillis());
        }
      }
    } catch (ZipWarningException ignore) {
      // Only instances of the class ZipWarningException exist in the chain of
      // exceptions. We choose to ignore this.
    } catch (ZipControllerException exception) {
      // At least one exception occured which is not just a ZipWarningException.
      // This is a severe situation that needs to be handled.
      throw new ModuleManagementToolException(
          "A Zip error was encountered during deployment of the AEP into the WAR", exception);
    } catch (IOException exception) {
      throw new ModuleManagementToolException(
          "An IO error was encountered during deployment of the AEP into the WAR", exception);
    }
  }