Example #1
0
 private void xmlReadSelectedElements(JarPackageData jarPackage, Element element)
     throws java.io.IOException {
   if (element.getNodeName().equals("selectedElements")) { // $NON-NLS-1$
     jarPackage.setExportClassFiles(
         getBooleanAttribute(element, "exportClassFiles")); // $NON-NLS-1$
     jarPackage.setExportOutputFolders(
         getBooleanAttribute(element, "exportOutputFolder", false)); // $NON-NLS-1$
     jarPackage.setExportJavaFiles(getBooleanAttribute(element, "exportJavaFiles")); // $NON-NLS-1$
     NodeList selectedElements = element.getChildNodes();
     Set<IAdaptable> elementsToExport = new HashSet<IAdaptable>(selectedElements.getLength());
     for (int j = 0; j < selectedElements.getLength(); j++) {
       Node selectedNode = selectedElements.item(j);
       if (selectedNode.getNodeType() != Node.ELEMENT_NODE) continue;
       Element selectedElement = (Element) selectedNode;
       if (selectedElement.getNodeName().equals("file")) // $NON-NLS-1$
       addFile(elementsToExport, selectedElement);
       else if (selectedElement.getNodeName().equals("folder")) // $NON-NLS-1$
       addFolder(elementsToExport, selectedElement);
       else if (selectedElement.getNodeName().equals("project")) // $NON-NLS-1$
       addProject(elementsToExport, selectedElement);
       else if (selectedElement.getNodeName().equals("javaElement")) // $NON-NLS-1$
       addJavaElement(elementsToExport, selectedElement);
       // Note: Other file types are not handled by this writer
     }
     jarPackage.setElements(elementsToExport.toArray());
   }
 }
  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;
  }