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 void copyExtendedServiceFiles(
      final Service service, final File recipeFile, final File extFolder)
      throws FileNotFoundException, PackagingException, IOException {
    final LinkedList<String> extendedServicesPaths = service.getExtendedServicesPaths();

    File extendingScriptFile = new File(extFolder + "/" + recipeFile.getName());
    File currentExtendedServiceContext = recipeFile;

    for (final String extendedServicePath : extendedServicesPaths) {
      // Locate the extended service file in the destination path
      final File extendedServiceFile =
          locateServiceFile(currentExtendedServiceContext, extendedServicePath);
      // If the extended service exists in my directory, no need to copy or change anything
      // This can happen if we have extension of services inside application since the client
      // will prepare the extending service directory already and then it will be prepared fully at
      // the server
      if (extendedServiceFile.getParentFile().equals(recipeFile.getParentFile())) {
        continue;
      }
      // Copy it to local dir with new name if needed
      final File localExtendedServiceFile =
          copyExtendedServiceFileAndRename(extendedServiceFile, extFolder);
      logger.finer(
          "copying locally extended script "
              + extendedServiceFile
              + " to "
              + localExtendedServiceFile);
      // Update the extending script extend property with the location of the new extended service
      // script
      updateExtendingScriptFileWithNewExtendedScriptLocation(
          extendingScriptFile, localExtendedServiceFile);
      // Copy remote resources locally
      final File rootScriptDir = extendedServiceFile.getParentFile();
      FileUtils.copyDirectory(
          rootScriptDir,
          extFolder,
          new FileFilter() {

            @Override
            public boolean accept(final File pathname) {
              if (!SVNFileFilter.getFilter().accept(pathname)) {
                return false;
              }
              if (pathname.equals(extendedServiceFile)) {
                return false;
              }
              if (pathname.isDirectory()) {
                return true;
              }
              final String relativePath = pathname.getPath().replace(rootScriptDir.getPath(), "");
              final boolean accept = !new File(extFolder.getPath() + "/" + relativePath).exists();
              if (accept && logger.isLoggable(Level.FINEST)) {
                logger.finest("copying extended script resource [" + pathname + "] locally");
              }
              return accept;
            }
          });
      // Replace context extending script file for multiple level extension
      extendingScriptFile = localExtendedServiceFile;
      currentExtendedServiceContext = extendedServiceFile;
    }
  }