protected JarPackageData getJarPackageData(
      IPackageFragmentRoot[] roots, IType mainType, IProgressMonitor monitor) throws CoreException {

    String filePath = getTempJarPath(appModule.getLocalModule());

    if (filePath == null) {
      handleApplicationDeploymentFailure();
    }

    IPath location = new Path(filePath);

    // Note that if no jar builder is specified in the package data
    // then a default one is used internally by the data that does NOT
    // package any jar dependencies.
    JarPackageData packageData = new JarPackageData();

    packageData.setJarLocation(location);

    // Don't create a manifest. A repackager should determine if a generated
    // manifest is necessary
    // or use a user-defined manifest.
    packageData.setGenerateManifest(false);

    // Since user manifest is not used, do not save to manifest (save to
    // manifest saves to user defined manifest)
    packageData.setSaveManifest(false);

    packageData.setManifestMainClass(mainType);
    packageData.setElements(roots);
    return packageData;
  }
Example #2
0
 private void xmlReadManifest(JarPackageData jarPackage, Element element)
     throws java.io.IOException {
   if (element.getNodeName().equals("manifest")) { // $NON-NLS-1$
     jarPackage.setManifestVersion(element.getAttribute("manifestVersion")); // $NON-NLS-1$
     jarPackage.setUsesManifest(getBooleanAttribute(element, "usesManifest")); // $NON-NLS-1$
     jarPackage.setReuseManifest(getBooleanAttribute(element, "reuseManifest")); // $NON-NLS-1$
     jarPackage.setSaveManifest(getBooleanAttribute(element, "saveManifest")); // $NON-NLS-1$
     jarPackage.setGenerateManifest(
         getBooleanAttribute(element, "generateManifest")); // $NON-NLS-1$
     jarPackage.setManifestLocation(
         Path.fromPortableString(element.getAttribute("manifestLocation"))); // $NON-NLS-1$
     jarPackage.setManifestMainClass(getMainClass(element));
     xmlReadSealingInfo(jarPackage, element);
   }
 }
  public ApplicationArchive getApplicationArchive(IProgressMonitor monitor) throws CoreException {

    if (!initialized) {
      // Seems like initialize() wasn't invoked prior to this call
      throw CloudErrorUtil.toCoreException(
          Messages.JavaCloudFoundryArchiver_ERROR_ARCHIVER_NOT_INITIALIZED);
    }

    ApplicationArchive archive =
        JavaWebApplicationDelegate.getArchiveFromManifest(appModule, cloudServer);

    if (archive == null) {

      File packagedFile = null;

      IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(appModule);

      if (javaProject == null) {
        handleApplicationDeploymentFailure(
            Messages.JavaCloudFoundryArchiver_ERROR_NO_JAVA_PROJ_RESOLVED);
      }

      JavaPackageFragmentRootHandler rootResolver =
          getPackageFragmentRootHandler(javaProject, monitor);

      IType mainType = rootResolver.getMainType(monitor);

      final IPackageFragmentRoot[] roots = rootResolver.getPackageFragmentRoots(monitor);

      if (roots == null || roots.length == 0) {
        handleApplicationDeploymentFailure(
            Messages.JavaCloudFoundryArchiver_ERROR_NO_PACKAGE_FRAG_ROOTS);
      }

      JarPackageData jarPackageData = getJarPackageData(roots, mainType, monitor);

      boolean isBoot = CloudFoundryProjectUtil.isSpringBoot(appModule);

      // Search for existing MANIFEST.MF
      IFile metaFile = getManifest(roots, javaProject);

      // Only use existing manifest files for non-Spring boot, as Spring
      // boot repackager will
      // generate it own manifest file.
      if (!isBoot && metaFile != null) {
        // If it is not a boot project, use a standard library jar
        // builder
        jarPackageData.setJarBuilder(getDefaultLibJarBuilder());

        jarPackageData.setManifestLocation(metaFile.getFullPath());
        jarPackageData.setSaveManifest(false);
        jarPackageData.setGenerateManifest(false);
        // Check manifest accessibility through the jar package data
        // API
        // to verify the packaging won't fail
        if (!jarPackageData.isManifestAccessible()) {
          handleApplicationDeploymentFailure(
              NLS.bind(
                  Messages.JavaCloudFoundryArchiver_ERROR_MANIFEST_NOT_ACCESSIBLE,
                  metaFile.getLocation().toString()));
        }

        InputStream inputStream = null;
        try {

          inputStream = new FileInputStream(metaFile.getLocation().toFile());
          Manifest manifest = new Manifest(inputStream);
          Attributes att = manifest.getMainAttributes();
          if (att.getValue("Main-Class") == null) { // $NON-NLS-1$
            handleApplicationDeploymentFailure(
                Messages.JavaCloudFoundryArchiver_ERROR_NO_MAIN_CLASS_IN_MANIFEST);
          }
        } catch (FileNotFoundException e) {
          handleApplicationDeploymentFailure(
              NLS.bind(
                  Messages.JavaCloudFoundryArchiver_ERROR_FAILED_READ_MANIFEST,
                  e.getLocalizedMessage()));

        } catch (IOException e) {
          handleApplicationDeploymentFailure(
              NLS.bind(
                  Messages.JavaCloudFoundryArchiver_ERROR_FAILED_READ_MANIFEST,
                  e.getLocalizedMessage()));

        } finally {

          if (inputStream != null) {
            try {
              inputStream.close();

            } catch (IOException io) {
              // Ignore
            }
          }
        }

      } else {
        // Otherwise generate a manifest file. Note that manifest files
        // are only generated in the temporary jar meant only for
        // deployment.
        // The associated Java project is no modified.
        jarPackageData.setGenerateManifest(true);

        // This ensures that folders in output folders appear at root
        // level
        // Example: src/main/resources, which is in the project's
        // classpath, contains non-Java templates folder and
        // has output folder target/classes. If not exporting output
        // folder,
        // templates will be packaged in the jar using this path:
        // resources/templates
        // This may cause problems with the application's dependencies
        // if they are looking for just /templates at top level of the
        // jar
        // If exporting output folders, templates folder will be
        // packaged at top level in the jar.
        jarPackageData.setExportOutputFolders(true);
      }

      try {
        packagedFile = packageApplication(jarPackageData, monitor);
      } catch (CoreException e) {
        handleApplicationDeploymentFailure(
            NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_JAVA_APP_PACKAGE, e.getMessage()));
      }

      if (packagedFile == null || !packagedFile.exists()) {
        handleApplicationDeploymentFailure(
            Messages.JavaCloudFoundryArchiver_ERROR_NO_PACKAGED_FILE_CREATED);
      }

      if (isBoot) {
        bootRepackage(roots, packagedFile);
      }

      // At this stage a packaged file should have been created or found
      try {
        archive = new CloudZipApplicationArchive(new ZipFile(packagedFile));
      } catch (IOException ioe) {
        handleApplicationDeploymentFailure(
            NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_CREATE_CF_ARCHIVE, ioe.getMessage()));
      }
    }

    return archive;
  }