@Override
  public ContainerBundleAnalyzer analyze(File bundle) {
    packageExports = null;
    Analyzer analyzer = new Analyzer();
    Jar jar = null;
    try {
      jar = new Jar(bundle);
      Manifest manifest = jar.getManifest();
      String exportHeader = manifest.getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
      if (exportHeader != null) {
        Map<String, Map<String, String>> exported = analyzer.parseHeader(exportHeader);

        packageExports = exported.keySet();
      } else {
        packageExports = new HashSet<>();
      }
    } catch (Exception e) {
      throw new SmartSpacesException(
          String.format("Could not analyze bundle %s", bundle.getAbsolutePath()), e);
    } finally {
      fileSupport.close(analyzer, false);
      fileSupport.close(jar, false);
    }

    return this;
  }
Пример #2
0
  public Manifest getManifest(
      MavenProject project, Map instructions, Properties properties, Jar[] classpath)
      throws IOException, MojoFailureException, MojoExecutionException, Exception {
    Analyzer analyzer = getAnalyzer(project, instructions, properties, classpath);
    boolean hasErrors = reportErrors("Manifest " + project.getArtifact(), analyzer);
    if (hasErrors) {
      String failok = analyzer.getProperty("-failok");
      if (null == failok || "false".equalsIgnoreCase(failok)) {
        throw new MojoFailureException("Error(s) found in manifest configuration");
      }
    }

    Jar jar = analyzer.getJar();

    if (unpackBundle) {
      File outputFile = getOutputDirectory();
      for (Entry<String, Resource> entry : jar.getResources().entrySet()) {
        File entryFile = new File(outputFile, entry.getKey());
        if (!entryFile.exists() || entry.getValue().lastModified() == 0) {
          entryFile.getParentFile().mkdirs();
          OutputStream os = new FileOutputStream(entryFile);
          entry.getValue().write(os);
          os.close();
        }
      }
    }

    Manifest manifest = jar.getManifest();

    // cleanup...
    analyzer.close();

    return manifest;
  }
  private void generateLauncherJar(ProjectLauncher launcher, File folder, MultiStatus status) {
    Jar launcherJar = new Jar("launch");

    // Merge in the classpath JARs
    Collection<String> classpath = launcher.getClasspath();
    for (String classpathEntry : classpath) {
      try {
        Jar classpathJar = new Jar(new File(classpathEntry));
        launcherJar.addAll(classpathJar);
      } catch (IOException e) {
        status.add(
            new Status(
                IStatus.ERROR,
                Plugin.PLUGIN_ID,
                0,
                String.format("Failed to add classpath JAR '%s'.", classpathEntry),
                e));
      }
    }

    // Set the Main-Class
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Main-Class", "launch");
    launcherJar.setManifest(manifest);
    launcherJar.putResource(
        "launch.class",
        new URLResource(ExecutableJarExportWizard.class.getResource("launch.clazz")));

    try {
      launcherJar.write(new File(folder, "launch.jar"));
    } catch (Exception e) {
      status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating launch JAR.", e));
    }
  }
  @Override
  public boolean performFinish() {
    MultiStatus status =
        new MultiStatus(Plugin.PLUGIN_ID, 0, "Failed to install one or more bundles", null);

    List<File> files = fileSelectionPage.getFiles();
    selectedBundles = new LinkedList<Pair<String, String>>();
    for (File file : files) {
      Jar jar = null;
      try {
        jar = new Jar(file);
        jar.setDoNotTouchManifest();

        Attributes mainAttribs = jar.getManifest().getMainAttributes();
        String bsn = BundleUtils.getBundleSymbolicName(mainAttribs);
        String version = mainAttribs.getValue(Constants.BUNDLE_VERSION);
        if (version == null) version = "0";
        selectedBundles.add(Pair.newInstance(bsn, version));
      } catch (IOException e) {
        status.add(
            new Status(
                IStatus.ERROR,
                Plugin.PLUGIN_ID,
                0,
                MessageFormat.format("Failed to analyse JAR: {0}", file.getPath()),
                e));
        continue;
      }

      try {
        File newFile = repository.put(jar);

        RefreshFileJob refreshJob = new RefreshFileJob(newFile);
        if (refreshJob.isFileInWorkspace()) refreshJob.schedule();
      } catch (Exception e) {
        status.add(
            new Status(
                IStatus.ERROR,
                Plugin.PLUGIN_ID,
                0,
                MessageFormat.format("Failed to add JAR to repository: {0}", file.getPath()),
                e));
        continue;
      }
    }

    if (status.isOK()) {
      return true;
    } else {
      ErrorDialog.openError(getShell(), "Error", null, status);
      return false;
    }
  }
Пример #5
0
  /*
   * Patched version of bnd's Analyzer.calculateExportsFromContents
   */
  public static String calculateExportsFromContents(Jar bundle) {
    String ddel = "";
    StringBuffer sb = new StringBuffer();
    Map<String, Map<String, Resource>> map = bundle.getDirectories();
    for (Iterator<Entry<String, Map<String, Resource>>> i = map.entrySet().iterator();
        i.hasNext(); ) {
      // ----------------------------------------------------
      // should also ignore directories with no resources
      // ----------------------------------------------------
      Entry<String, Map<String, Resource>> entry = i.next();
      if (entry.getValue() == null || entry.getValue().isEmpty()) continue;
      // ----------------------------------------------------
      String directory = entry.getKey();
      if (directory.equals("META-INF") || directory.startsWith("META-INF/")) continue;
      if (directory.equals("OSGI-OPT") || directory.startsWith("OSGI-OPT/")) continue;
      if (directory.equals("/")) continue;

      if (directory.endsWith("/")) directory = directory.substring(0, directory.length() - 1);

      directory = directory.replace('/', '.');
      sb.append(ddel);
      sb.append(directory);
      ddel = ",";
    }
    return sb.toString();
  }
  private IStatus generateJar(String jarPath) {
    MultiStatus status =
        new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred during exporting.", null);
    ProjectLauncher launcher = null;
    try {
      launcher = bndProject.getProjectLauncher();
    } catch (Exception e) {
      status.add(
          new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error getting project launcher.", e));
    }

    try {
      Jar jar = launcher.executable();
      jar.write(jarPath);
    } catch (Exception e) {
      status.add(
          new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating executable JAR.", e));
    }
    return status;
  }
Пример #7
0
  /**
   * We create a JAR with the test.classreferenc.ClassReference class. This class contains a
   * javax.swing.Box.class reference Prior to Java 1.5, this was done in a silly way that is handled
   * specially. After 1.5 it is a normal reference.
   *
   * @throws Exception
   */
  public void testReference() throws Exception {
    Properties properties = new Properties();
    properties.put("-classpath", "compilerversions/compilerversions.jar");
    String[] packages = {
      "sun_1_1",
      "sun_1_6",
      "eclipse_1_1",
      "sun_1_2",
      "sun_1_3",
      "sun_1_4",
      "sun_1_5",
      "sun_jsr14",
      "eclipse_1_5",
      "eclipse_1_6",
      "eclipse_1_2",
      "eclipse_1_3",
      "eclipse_1_4",
      "eclipse_jsr14"
    };
    for (int i = 0; i < packages.length; i++) {
      Builder builder = new Builder();
      properties.put("Export-Package", packages[i]);
      builder.setProperties(properties);
      Jar jar = builder.build();
      System.out.println(builder.getErrors());
      System.out.println(builder.getWarnings());
      assertEquals(0, builder.getErrors().size());
      assertEquals(0, builder.getWarnings().size());

      Manifest manifest = jar.getManifest();
      String imports = manifest.getMainAttributes().getValue("Import-Package");
      assertTrue(
          "Package " + packages[i] + "contains swing ref", imports.indexOf("javax.swing") >= 0);
      assertFalse(
          "Package " + packages[i] + "should not contain ClassRef",
          imports.indexOf("ClassRef") >= 0);
    }
  }
Пример #8
0
  public static void writeManifest(Manifest manifest, File outputFile) throws IOException {
    outputFile.getParentFile().mkdirs();

    FileOutputStream os;
    os = new FileOutputStream(outputFile);
    try {
      Jar.writeManifest(manifest, os);
    } finally {
      try {
        os.close();
      } catch (IOException e) {
        // nothing we can do here
      }
    }
  }