Esempio n. 1
0
  /**
   * ************* Packs an application folder into a zip file.
   *
   * @param application the application object as read from the application file.
   * @param applicationDir the directory where the application was read from.
   * @param additionalServiceFiles additional files that should be packaged into each service
   *     directory.
   * @return the packaged zip file.
   * @throws IOException .
   * @throws PackagingException .
   */
  public static File packApplication(
      final Application application, final File applicationDir, final File[] additionalServiceFiles)
      throws IOException, PackagingException {

    boolean hasExtendedServices = false;
    for (final Service service : application.getServices()) {
      if (!service.getExtendedServicesPaths().isEmpty()) {
        hasExtendedServices = true;
        break;
      }
    }
    File applicationFolderToPack = applicationDir;
    // If there are no extended service we don't need to prepare an application folder to pack with
    // all the
    // extended services content.
    if (hasExtendedServices) {
      final File destApplicationFolder = createCopyDirectory(applicationFolderToPack);

      for (final Service service : application.getServices()) {
        final File extFolder = new File(destApplicationFolder + "/" + service.getName());
        final File recipeFile =
            DSLReader.findDefaultDSLFile(
                DSLReader.SERVICE_DSL_FILE_NAME_SUFFIX,
                new File(applicationDir + "/" + service.getName()));
        copyExtendedServiceFiles(service, recipeFile, extFolder);
      }
      // Pack the prepared folder instead of the original application folder.
      applicationFolderToPack = destApplicationFolder;
    }

    if ((additionalServiceFiles != null) && (additionalServiceFiles.length > 0)) {
      // if a copy directory was already created, use the existing one, otherwise
      // create a new one.
      if (applicationFolderToPack == applicationDir) {
        applicationFolderToPack = createCopyDirectory(applicationFolderToPack);
      }
      List<Service> services = application.getServices();
      for (Service service : services) {
        File serviceDir = new File(applicationFolderToPack, service.getName());
        if (!serviceDir.exists()) {
          throw new PackagingException("Could not find service folder at: " + serviceDir);
        }
        if (!serviceDir.isDirectory()) {
          throw new PackagingException("Was expecting a directory at: " + serviceDir);
        }

        for (File fileToCopy : additionalServiceFiles) {
          FileUtils.copyFileToDirectory(fileToCopy, serviceDir);
        }
      }
    }
    // zip the application folder.
    final File zipFile = File.createTempFile("application", ".zip");
    zipFile.deleteOnExit();
    ZipUtils.zip(applicationFolderToPack, zipFile);
    return zipFile;
  }
Esempio n. 2
0
  private static File createZippedPu(
      final Service service, final File puFolderToZip, final File recipeFile)
      throws IOException, PackagingException {
    logger.finer("trying to zip " + puFolderToZip.getAbsolutePath());
    final String serviceName =
        service.getName() != null ? service.getName() : recipeFile.getParentFile().getName();

    // create a temp dir under the system temp dir
    final File tmpFile = File.createTempFile("ServicePackage", null);
    tmpFile.delete();
    tmpFile.mkdir();

    final File zipFile = new File(tmpFile, serviceName + ".zip");

    // files will be deleted in reverse order
    tmpFile.deleteOnExit();
    zipFile.deleteOnExit();

    ServiceReader.validateFolderSize(puFolderToZip, service.getMaxJarSize());
    ZipUtils.zip(puFolderToZip, zipFile);
    logger.finer("zipped folder successfully to " + zipFile.getAbsolutePath());
    return zipFile;
  }