Ejemplo n.º 1
0
  public static void testBumpSubBuilders() throws Exception {
    File tmp = new File("tmp-ws");
    if (tmp.exists()) IO.deleteWithException(tmp);
    tmp.mkdir();
    assertTrue(tmp.isDirectory());

    try {
      IO.copy(new File("test/ws"), tmp);
      Workspace ws = Workspace.getWorkspace(tmp);
      Project project = ws.getProject("bump-sub");
      project.setTrace(true);

      assertNull(project.getProperty("Bundle-Version"));

      project.bump("=+0");

      assertNull(project.getProperty("Bundle-Version"));

      for (Builder b : project.getSubBuilders()) {
        assertEquals(new Version(1, 1, 0), new Version(b.getVersion()));
      }
    } finally {
      IO.deleteWithException(tmp);
    }
  }
Ejemplo n.º 2
0
  public static void testBump() throws Exception {
    File tmp = new File("tmp-ws");
    if (tmp.exists()) IO.deleteWithException(tmp);
    tmp.mkdir();
    assertTrue(tmp.isDirectory());

    try {
      IO.copy(new File("test/ws"), tmp);
      Workspace ws = Workspace.getWorkspace(tmp);
      Project project = ws.getProject("p1");
      int size = project.getProperties().size();
      Version old = new Version(project.getProperty("Bundle-Version"));
      System.err.println("Old version " + old);
      project.bump("=+0");
      Version newv = new Version(project.getProperty("Bundle-Version"));
      System.err.println("New version " + newv);
      assertEquals(old.getMajor(), newv.getMajor());
      assertEquals(old.getMinor() + 1, newv.getMinor());
      assertEquals(0, newv.getMicro());
      assertEquals(size, project.getProperties().size());
      assertEquals("sometime", newv.getQualifier());
    } finally {
      IO.deleteWithException(tmp);
    }
  }
Ejemplo n.º 3
0
  public static void testBumpIncludeFile() throws Exception {
    File tmp = new File("tmp-ws");
    if (tmp.exists()) IO.deleteWithException(tmp);
    tmp.mkdir();
    assertTrue(tmp.isDirectory());

    try {
      IO.copy(new File("test/ws"), tmp);
      Workspace ws = Workspace.getWorkspace(tmp);
      Project project = ws.getProject("bump-included");
      project.setTrace(true);
      Version old = new Version(project.getProperty("Bundle-Version"));
      assertEquals(new Version(1, 0, 0), old);
      project.bump("=+0");

      Processor processor = new Processor();
      processor.setProperties(project.getFile("include.txt"));

      Version newv = new Version(processor.getProperty("Bundle-Version"));
      System.err.println("New version " + newv);
      assertEquals(1, newv.getMajor());
      assertEquals(1, newv.getMinor());
      assertEquals(0, newv.getMicro());
    } finally {
      IO.deleteWithException(tmp);
    }
  }
Ejemplo n.º 4
0
 private static void checkPackageInfoFiles(
     Project project, String packageName, boolean expectPackageInfo, boolean expectPackageInfoJava)
     throws Exception {
   File pkgInfo = IO.getFile(project.getSrc(), packageName + "/packageinfo");
   File pkgInfoJava = IO.getFile(project.getSrc(), packageName + "/package-info.java");
   assertEquals(expectPackageInfo, pkgInfo.exists());
   assertEquals(expectPackageInfoJava, pkgInfoJava.exists());
 }
Ejemplo n.º 5
0
  public void deleteCommand(String name) throws Exception {
    CommandData cmd = getCommand(name);
    if (cmd == null) throw new IllegalArgumentException("No such command " + name);

    platform.deleteCommand(cmd);
    File tobedel = new File(commandDir, name);
    IO.deleteWithException(tobedel);
  }
Ejemplo n.º 6
0
 public void init() throws IOException {
   URL s = getClass().getClassLoader().getResource(SERVICE_JAR_FILE);
   if (s == null)
     if (underTest) return;
     else throw new Error("No " + SERVICE_JAR_FILE + " resource in jar");
   service.getParentFile().mkdirs();
   IO.copy(s, service);
 }
Ejemplo n.º 7
0
 /** The default policy is truncate micro. Check if this is applied to the import. */
 public static void testImportMicroTruncated() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.setProperty("Import-Package", "org.osgi.service.event");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("[1.0,2)", s);
 }
Ejemplo n.º 8
0
 /** Test if we can get the version from the source and apply the default policy. */
 public static void testVersionPolicyImportedExportsDefaultPolicy() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.addClasspath(new File("bin"));
   b.setProperty("Export-Package", "org.osgi.service.event");
   b.setProperty("Private-Package", "test.refer");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("[1.0,2)", s);
 }
Ejemplo n.º 9
0
  public ArtifactData get(byte[] sha) throws Exception {
    String name = Hex.toHexString(sha);
    File data = IO.getFile(repoDir, name + ".json");
    reporter.trace("artifact data file %s", data);
    if (data.isFile()) { // Bin + metadata
      ArtifactData artifact = codec.dec().from(data).get(ArtifactData.class);
      artifact.file = IO.getFile(repoDir, name).getAbsolutePath();
      return artifact;
    }
    File bin = IO.getFile(repoDir, name);
    if (bin.exists()) { // Only bin
      ArtifactData artifact = new ArtifactData();
      artifact.file = bin.getAbsolutePath();
      artifact.sha = sha;
      return artifact;
    }

    return null;
  }
Ejemplo n.º 10
0
 /**
  * See if we a can override the version from the export statement and the version from the source.
  */
 public static void testImportOverridesDiscoveredVersion() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.addClasspath(new File("bin"));
   b.setProperty("Export-Package", "org.osgi.service.event");
   b.setProperty("Private-Package", "test.refer");
   b.setProperty("Import-Package", "org.osgi.service.event;version=2.1.3.q");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("2.1.3.q", s);
 }
Ejemplo n.º 11
0
 /** Check if we can set a specific version on the import that does not use a version policy. */
 public static void testImportMicroNotTruncated() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.setProperty(
       "Import-Package",
       "org.osgi.service.event;version=${@}, org.osgi.service.log;version=\"${range;[==,=+)}\"");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   String l = b.getImports().getByFQN("org.osgi.service.log").get("version");
   assertEquals("1.0.1", s);
   assertEquals("[1.3,1.4)", l);
 }
Ejemplo n.º 12
0
  public void update(UpdateMemo memo) throws Exception {

    ArtifactData target = put(memo.best.urls.iterator().next());

    memo.current.version = new Version(memo.best.version);
    target.sync();
    memo.current.sha = target.sha;
    // memo.current.dependencies = target.dependencies;
    // memo.current.dependencies.add((new File(repoDir,
    // Hex.toHexString(target.sha))).getCanonicalPath());
    // memo.current.runbundles = target.runbundles;
    // memo.current.description = target.description;
    memo.current.time = target.time;

    if (memo.current instanceof ServiceData) {
      Service service = getService((ServiceData) memo.current);
      service.remove();
      createService((ServiceData) memo.current);
      IO.delete(new File(IO.getFile(serviceDir, memo.current.name), "data"));
      storeData(new File(IO.getFile(serviceDir, memo.current.name), "data"), memo.current);
    } else {
      platform.deleteCommand(memo.current);
      createCommand(memo.current, false);
      IO.delete(IO.getFile(commandDir, memo.current.name));
      storeData(IO.getFile(commandDir, memo.current.name), memo.current);
    }
  }
Ejemplo n.º 13
0
  /** Test import provide:. */
  public static void testExportProvided() throws Exception {
    Builder a = new Builder();
    a.addClasspath(IO.getFile("jar/osgi.jar"));
    a.addClasspath(new File("bin"));
    a.setProperty("Private-Package", "test.refer");
    a.setProperty("Export-Package", "org.osgi.service.http;provide:=true");
    Jar jar = a.build();
    Map<String, String> event = a.getImports().getByFQN("org.osgi.service.event");
    assertEquals("[1.0,2)", event.get("version"));
    Map<String, String> http = a.getImports().getByFQN("org.osgi.service.http");
    assertEquals("[1.2,1.3)", http.get("version"));

    Manifest m = jar.getManifest();
    String imports = m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);
    assertFalse(imports.contains(Constants.PROVIDE_DIRECTIVE));
  }
Ejemplo n.º 14
0
  private byte[] doSignatureFile(String[] digestNames, MessageDigest[] algorithms, byte[] manbytes)
      throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintWriter ps = IO.writer(out);
    ps.print("Signature-Version: 1.0\r\n");

    for (int a = 0; a < algorithms.length; a++) {
      if (algorithms[a] != null) {
        byte[] digest = algorithms[a].digest(manbytes);
        ps.print(digestNames[a] + "-Digest-Manifest: ");
        ps.print(new Base64(digest));
        ps.print("\r\n");
      }
    }
    return out.toByteArray();
  }
Ejemplo n.º 15
0
  /** hardcoded imports */
  public static void testHardcodedImports() throws Exception {
    Builder b = new Builder();
    b.addClasspath(IO.getFile("jar/osgi.jar"));
    b.setProperty("-versionpolicy", "${range;[==,+)}");
    b.setProperty("Private-Package", "org.objectweb.asm");
    b.setProperty("Import-Package", "org.osgi.framework,org.objectweb.asm,abc;version=2.0.0,*");
    b.build();
    Manifest m = b.getJar().getManifest();
    m.write(System.err);
    String s = b.getImports().getByFQN("org.objectweb.asm").get("version");
    assertNull(s);
    s = b.getImports().getByFQN("abc").get("version");
    assertEquals("2.0.0", s);

    s = b.getImports().getByFQN("org.osgi.framework").get("version");
    assertEquals("[1.3,2)", s);
  }
Ejemplo n.º 16
0
  private String listSupportFiles(List<File> toDelete) throws Exception {
    Formatter f = new Formatter();
    try {
      if (toDelete == null) {
        toDelete = new ArrayList<File>();
      }
      int precount = toDelete.size();
      File confFile = IO.getFile(platform.getConfigFile()).getCanonicalFile();
      if (confFile.exists()) {
        f.format("    * %s \t0 Config file%n", confFile);
        toDelete.add(confFile);
      }

      String result = (toDelete.size() > precount) ? f.toString() : null;
      return result;
    } finally {
      f.close();
    }
  }
Ejemplo n.º 17
0
  /**
   * Constructor
   *
   * @throws IOException
   */
  public JustAnotherPackageManager(Reporter reporter, Platform platform, File homeDir, File binDir)
      throws IOException {
    this.platform = platform;
    this.reporter = reporter;
    this.homeDir = homeDir;
    if (!homeDir.exists() && !homeDir.mkdirs())
      throw new IllegalArgumentException("Could not create directory " + homeDir);

    repoDir = IO.getFile(homeDir, "repo");
    if (!repoDir.exists() && !repoDir.mkdirs())
      throw new IllegalArgumentException("Could not create directory " + repoDir);

    commandDir = new File(homeDir, COMMANDS);
    serviceDir = new File(homeDir, SERVICE);
    commandDir.mkdir();
    serviceDir.mkdir();
    service = new File(repoDir, SERVICE_JAR_FILE);
    if (!service.isFile()) init();

    this.binDir = binDir;
    if (!binDir.exists() && !binDir.mkdirs())
      throw new IllegalArgumentException("Could not create bin directory " + binDir);
  }
Ejemplo n.º 18
0
  public void deinit(Appendable out, boolean force) throws Exception {
    Settings settings = new Settings(platform.getConfigFile());

    if (!force) {
      Justif justify = new Justif(80, 40);
      StringBuilder sb = new StringBuilder();
      Formatter f = new Formatter(sb);

      try {
        String list = listFiles(platform.getGlobal());
        if (list != null) {
          f.format("In global default environment:%n");
          f.format(list);
        }

        list = listFiles(platform.getLocal());
        if (list != null) {
          f.format("In local default environment:%n");
          f.format(list);
        }

        if (settings.containsKey(JPM_CACHE_GLOBAL)) {
          list = listFiles(IO.getFile(settings.get(JPM_CACHE_GLOBAL)));
          if (list != null) {
            f.format("In global configured environment:%n");
            f.format(list);
          }
        }

        if (settings.containsKey(JPM_CACHE_LOCAL)) {
          list = listFiles(IO.getFile(settings.get(JPM_CACHE_LOCAL)));
          if (list != null) {
            f.format("In local configured environment:%n");
            f.format(list);
          }
        }

        list = listSupportFiles();
        if (list != null) {
          f.format("jpm support files:%n");
          f.format(list);
        }

        f.format("%n%n");

        f.format(
            "All files listed above will be deleted if deinit is run with the force flag set"
                + " (\"jpm deinit -f\" or \"jpm deinit --force\"%n%n");
        f.flush();

        justify.wrap(sb);
        out.append(sb.toString());
      } finally {
        f.close();
      }
    } else { // i.e. if(force)
      int count = 0;
      File[] caches = {platform.getGlobal(), platform.getLocal(), null, null};
      if (settings.containsKey(JPM_CACHE_LOCAL)) {
        caches[2] = IO.getFile(settings.get(JPM_CACHE_LOCAL));
      }
      if (settings.containsKey(JPM_CACHE_GLOBAL)) {
        caches[3] = IO.getFile(settings.get(JPM_CACHE_GLOBAL));
      }
      ArrayList<File> toDelete = new ArrayList<File>();

      for (File cache : caches) {
        if (cache == null || !cache.exists()) {
          continue;
        }
        listFiles(cache, toDelete);
        if (toDelete.size() > count) {
          count = toDelete.size();
          if (!cache.canWrite()) {
            reporter.error(PERMISSION_ERROR + " (" + cache + ")");
            return;
          }
          toDelete.add(cache);
        }
      }
      listSupportFiles(toDelete);

      for (File f : toDelete) {
        if (f.exists() && !f.canWrite()) {
          reporter.error(PERMISSION_ERROR + " (" + f + ")");
        }
      }
      if (reporter.getErrors().size() > 0) {
        return;
      }

      for (File f : toDelete) {
        if (f.exists()) {
          IO.deleteWithException(f);
        }
      }
    }
  }
Ejemplo n.º 19
0
  public static void testSetPackageVersion() throws Exception {
    File tmp = new File("tmp-ws");
    if (tmp.exists()) IO.deleteWithException(tmp);
    tmp.mkdir();
    assertTrue(tmp.isDirectory());

    try {
      IO.copy(new File("test/ws"), tmp);
      Workspace ws = Workspace.getWorkspace(tmp);
      Project project = ws.getProject("p5");
      project.setTrace(true);

      Version newVersion = new Version(2, 0, 0);

      // Package with no package info
      project.setPackageInfo("pkg1", newVersion);
      Version version = project.getPackageInfo("pkg1");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg1", true, false);

      // Package with package-info.java containing @Version("1.0.0")
      project.setPackageInfo("pkg2", newVersion);
      version = project.getPackageInfo("pkg2");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg2", false, true);

      // Package with package-info.java containing @aQute.bnd.annotations.Version("1.0.0")
      project.setPackageInfo("pkg3", newVersion);
      version = project.getPackageInfo("pkg3");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg3", false, true);

      // Package with package-info.java containing @aQute.bnd.annotations.Version(value="1.0.0")
      project.setPackageInfo("pkg4", newVersion);
      version = project.getPackageInfo("pkg4");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg4", false, true);

      // Package with package-info.java containing version + packageinfo
      project.setPackageInfo("pkg5", newVersion);
      version = project.getPackageInfo("pkg5");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg5", true, true);

      // Package with package-info.java NOT containing version + packageinfo
      project.setPackageInfo("pkg6", newVersion);
      version = project.getPackageInfo("pkg6");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg6", true, true);

      // Package with package-info.java NOT containing version
      project.setPackageInfo("pkg7", newVersion);
      version = project.getPackageInfo("pkg7");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg7", true, true);

      newVersion = new Version(2, 2, 0);

      // Update packageinfo file
      project.setPackageInfo("pkg1", newVersion);
      version = project.getPackageInfo("pkg1");
      assertEquals(newVersion, version);
      checkPackageInfoFiles(project, "pkg1", true, false);

    } finally {
      IO.deleteWithException(tmp);
    }
  }
Ejemplo n.º 20
0
  public void signJar(Jar jar) {
    if (digestNames == null || digestNames.length == 0)
      error("Need at least one digest algorithm name, none are specified");

    if (keystoreFile == null || !keystoreFile.getAbsoluteFile().exists()) {
      error("No such keystore file: " + keystoreFile);
      return;
    }

    if (alias == null) {
      error("Private key alias not set for signing");
      return;
    }

    MessageDigest digestAlgorithms[] = new MessageDigest[digestNames.length];

    getAlgorithms(digestNames, digestAlgorithms);

    try {
      Manifest manifest = jar.getManifest();
      manifest.getMainAttributes().putValue("Signed-By", "Bnd");

      // Create a new manifest that contains the
      // Name parts with the specified digests

      ByteArrayOutputStream o = new ByteArrayOutputStream();
      manifest.write(o);
      doManifest(jar, digestNames, digestAlgorithms, o);
      o.flush();
      byte newManifestBytes[] = o.toByteArray();
      jar.putResource("META-INF/MANIFEST.MF", new EmbeddedResource(newManifestBytes, 0));

      // Use the bytes from the new manifest to create
      // a signature file

      byte[] signatureFileBytes = doSignatureFile(digestNames, digestAlgorithms, newManifestBytes);
      jar.putResource("META-INF/BND.SF", new EmbeddedResource(signatureFileBytes, 0));

      // Now we must create an RSA signature
      // this requires the private key from the keystore

      KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

      KeyStore.PrivateKeyEntry privateKeyEntry = null;

      java.io.FileInputStream keystoreInputStream = null;
      try {
        keystoreInputStream = new java.io.FileInputStream(keystoreFile);
        char[] pw = password == null ? new char[0] : password.toCharArray();

        keystore.load(keystoreInputStream, pw);
        keystoreInputStream.close();
        privateKeyEntry =
            (PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(pw));
      } catch (Exception e) {
        error(
            "No able to load the private key from the give keystore("
                + keystoreFile.getAbsolutePath()
                + ") with alias "
                + alias
                + " : "
                + e);
        return;
      } finally {
        IO.close(keystoreInputStream);
      }
      PrivateKey privateKey = privateKeyEntry.getPrivateKey();

      Signature signature = Signature.getInstance("MD5withRSA");
      signature.initSign(privateKey);

      signature.update(signatureFileBytes);

      signature.sign();

      // TODO, place the SF in a PCKS#7 structure ...
      // no standard class for this? The following
      // is an idea but we will to have do ASN.1 BER
      // encoding ...

      ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
      jar.putResource("META-INF/BND.RSA", new EmbeddedResource(tmpStream.toByteArray(), 0));
    } catch (Exception e) {
      error("During signing: " + e);
    }
  }