void put(final URI uri, ArtifactData data) throws Exception {
   reporter.trace("put %s %s", uri, data);
   File tmp = createTempFile(repoDir, "mtp", ".whatever");
   tmp.deleteOnExit();
   try {
     copy(uri.toURL(), tmp);
     byte[] sha = SHA1.digest(tmp).digest();
     reporter.trace("SHA %s %s", uri, Hex.toHexString(sha));
     ArtifactData existing = get(sha);
     if (existing != null) {
       reporter.trace("existing");
       xcopy(existing, data);
       return;
     }
     File meta = new File(repoDir, Hex.toHexString(sha) + ".json");
     File file = new File(repoDir, Hex.toHexString(sha));
     rename(tmp, file);
     reporter.trace("file %s", file);
     data.file = file.getAbsolutePath();
     data.sha = sha;
     data.busy = false;
     CommandData cmddata = parseCommandData(data);
     if (cmddata.bsn != null) {
       data.name = cmddata.bsn + "-" + cmddata.version;
     } else data.name = Strings.display(cmddata.title, cmddata.bsn, cmddata.name, uri);
     codec.enc().to(meta).put(data);
     reporter.trace("TD = " + data);
   } finally {
     tmp.delete();
     reporter.trace("puted %s %s", uri, data);
   }
 }
Exemple #2
0
  public static void main(String[] args) {
    SHA1 sha = new SHA1();

    byte[] dig1 = new byte[20];
    byte[] dig2 = new byte[20];
    byte[] dig3 = new byte[20];

    /*
     * We do not specify a charset name for getBytes(), since we assume that
     * the JVM's default encoder maps the _used_ ASCII characters exactly as
     * getBytes("US-ASCII") would do. (Ah, yes, too lazy to catch the
     * exception that can be thrown by getBytes("US-ASCII")). Note: This has
     * no effect on the SHA-1 implementation, this is just for the following
     * test code.
     */

    sha.update("abc".getBytes());
    sha.digest(dig1);

    sha.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".getBytes());
    sha.digest(dig2);

    for (int i = 0; i < 1000000; i++) sha.update((byte) 'a');
    sha.digest(dig3);

    String dig1_res = toHexString(dig1);
    String dig2_res = toHexString(dig2);
    String dig3_res = toHexString(dig3);

    String dig1_ref = "A9993E364706816ABA3E25717850C26C9CD0D89D";
    String dig2_ref = "84983E441C3BD26EBAAE4AA1F95129E5E54670F1";
    String dig3_ref = "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F";

    if (dig1_res.equals(dig1_ref)) System.out.println("SHA-1 Test 1 OK.");
    else System.out.println("SHA-1 Test 1 FAILED.");

    if (dig2_res.equals(dig2_ref)) System.out.println("SHA-1 Test 2 OK.");
    else System.out.println("SHA-1 Test 2 FAILED.");

    if (dig3_res.equals(dig3_ref)) System.out.println("SHA-1 Test 3 OK.");
    else System.out.println("SHA-1 Test 3 FAILED.");

    if (dig3_res.equals(dig3_ref)) System.out.println("SHA-1 Test 3 OK.");
    else System.out.println("SHA-1 Test 3 FAILED.");
  }
  public String what(String key, boolean oneliner) throws Exception {
    byte[] sha;

    Matcher m = SHA_P.matcher(key);
    if (m.matches()) {
      sha = Hex.toByteArray(key);
    } else {
      m = URL_P.matcher(key);
      if (m.matches()) {
        URL url = new URL(key);
        sha = SHA1.digest(url.openStream()).digest();
      } else {
        File jarfile = new File(key);
        if (!jarfile.exists()) {
          reporter.error("File does not exist: %s", jarfile.getCanonicalPath());
        }
        sha = SHA1.digest(jarfile).digest();
      }
    }
    reporter.trace("sha %s", Hex.toHexString(sha));
    Revision revision = library.getRevision(sha);
    if (revision == null) {
      return null;
    }

    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    Justif justif = new Justif(120, 20, 70, 20, 75);
    DateFormat dateFormat = DateFormat.getDateInstance();

    try {
      if (oneliner) {
        f.format("%20s %s%n", Hex.toHexString(revision._id), createCoord(revision));
      } else {
        f.format("Artifact: %s%n", revision.artifactId);
        if (revision.organization != null && revision.organization.name != null) {
          f.format(" (%s)", revision.organization.name);
        }
        f.format("%n");
        f.format("Coordinates\t0: %s%n", createCoord(revision));
        f.format("Created\t0: %s%n", dateFormat.format(new Date(revision.created)));
        f.format("Size\t0: %d%n", revision.size);
        f.format("Sha\t0: %s%n", Hex.toHexString(revision._id));
        f.format("URL\t0: %s%n", createJpmLink(revision));
        f.format("%n");
        f.format("%s%n", revision.description);
        f.format("%n");
        f.format("Dependencies\t0:%n");
        boolean flag = false;
        Iterable<RevisionRef> closure = library.getClosure(revision._id, true);
        for (RevisionRef dep : closure) {
          f.format(
              " - %s \t2- %s \t3- %s%n",
              dep.name, createCoord(dep), dateFormat.format(new Date(dep.created)));
          flag = true;
        }
        if (!flag) {
          f.format("     None%n");
        }
        f.format("%n");
      }
      f.flush();
      justif.wrap(sb);
      return sb.toString();
    } finally {
      f.close();
    }
  }