예제 #1
0
 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);
   }
 }
예제 #2
0
 /**
  * Turn the shas into a readable form
  *
  * @param dependencies
  * @return
  * @throws Exception
  */
 public List<?> toString(List<byte[]> dependencies) throws Exception {
   List<String> out = new ArrayList<String>();
   for (byte[] dependency : dependencies) {
     ArtifactData data = get(dependency);
     if (data == null) out.add(Hex.toHexString(dependency));
     else {
       out.add(Strings.display(data.name, Hex.toHexString(dependency)));
     }
   }
   return out;
 }
예제 #3
0
  public ArtifactData getCandidateAsync(String arg) throws Exception {
    reporter.trace("coordinate %s", arg);
    if (isUrl(arg))
      try {
        ArtifactData data = putAsync(new URI(arg));
        data.local = true;
        return data;
      } catch (Exception e) {
        reporter.trace("hmm, not a valid url %s, will try the server", arg);
      }

    Coordinate c = new Coordinate(arg);

    if (c.isSha()) {
      ArtifactData r = get(c.getSha());
      if (r != null) return r;
    }

    Revision revision = library.getRevisionByCoordinate(c);
    if (revision == null) return null;

    reporter.trace("revision %s", Hex.toHexString(revision._id));

    ArtifactData ad = get(revision._id);
    if (ad != null) {
      reporter.trace("found in cache");
      return ad;
    }

    URI url = revision.urls.iterator().next();
    ArtifactData artifactData = putAsync(url);
    artifactData.coordinate = c;
    return artifactData;
  }
예제 #4
0
 /** Delete a cache entry */
 public boolean deleteCache(byte[] id) throws Exception {
   File dir = IO.getFile(cache, Hex.toHexString(id));
   if (dir.isDirectory()) {
     IO.delete(dir);
     return true;
   }
   return false;
 }
예제 #5
0
  void download0(URI url, File path, byte[] sha) throws Exception {
    path.getParentFile().mkdirs();
    File tmp = IO.createTempFile(path.getParentFile(), "tmp", ".jar");
    URL u = url.toURL();

    URLConnection conn = u.openConnection();
    InputStream in;
    if (conn instanceof HttpURLConnection) {
      HttpURLConnection http = (HttpURLConnection) conn;
      http.setRequestProperty("Accept-Encoding", "deflate");
      http.setInstanceFollowRedirects(true);

      connector.handle(conn);

      int result = http.getResponseCode();
      if (result / 100 != 2) {
        String s = "";
        InputStream err = http.getErrorStream();
        try {
          if (err != null) s = IO.collect(err);
          if (result == 404) {
            reporter.trace("not found ");
            throw new FileNotFoundException("Cannot find " + url + " : " + s);
          }
          throw new IOException(
              "Failed request " + result + ":" + http.getResponseMessage() + " " + s);
        } finally {
          if (err != null) err.close();
        }
      }

      String deflate = http.getHeaderField("Content-Encoding");
      in = http.getInputStream();
      if (deflate != null && deflate.toLowerCase().contains("deflate")) {
        in = new InflaterInputStream(in);
        reporter.trace("inflate");
      }
    } else {
      connector.handle(conn);
      in = conn.getInputStream();
    }

    IO.copy(in, tmp);

    byte[] digest = SHA1.digest(tmp).digest();
    if (Arrays.equals(digest, sha)) {
      IO.rename(tmp, path);
    } else {
      reporter.trace(
          "sha's did not match %s, expected %s, got %s", tmp, Hex.toHexString(sha), digest);
      throw new IllegalArgumentException("Invalid sha downloaded");
    }
  }
예제 #6
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;
  }
예제 #7
0
  @Override
  void encode(Encoder app, Object object, Map<Object, Type> visited) throws IOException, Exception {

    // Byte arrays should not be treated as arrays. We treat them
    // as hex strings

    if (object instanceof byte[]) {
      StringHandler.string(app, Hex.toHexString((byte[]) object));
      return;
    }
    app.append("[");
    app.indent();
    String del = "";
    int l = Array.getLength(object);
    for (int i = 0; i < l; i++) {
      app.append(del);
      app.encode(Array.get(object, i), componentType, visited);
      del = ",";
    }
    app.undent();
    app.append("]");
  }
예제 #8
0
  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();
    }
  }
예제 #9
0
 private String createJpmLink(Revision rev) {
   return String.format(
       "http://jpm4j.org/#!/p/sha/%s//%s", Hex.toHexString(rev._id), rev.baseline);
 }
예제 #10
0
  /** Get the file belonging to a Resource Descriptor */
  public File getResource(byte[] rd, final RepositoryPlugin.DownloadListener... blockers)
      throws Exception {
    final ResourceDescriptorImpl rds = getResourceDescriptor(rd);

    // No such descriptor?

    if (rds == null) {
      reporter.trace("no such descriptor %s", Hex.toHexString(rd));
      return null;
    }

    //
    // Construct a path
    //
    final File path =
        IO.getFile(cache, Hex.toHexString(rds.id) + "/" + rds.bsn + "-" + rds.version + ".jar");

    if (path.isFile()) {
      //
      // Ok, it is there, just report
      //
      ok(blockers, path);
      return path;
    }

    //
    // Check if we had an earlier failure
    //

    synchronized (failures) {
      Long l = failures.get(rds.url);
      if (l != null && (System.currentTimeMillis() - l) < THRESHOLD) {
        reporter.trace("descriptor %s, had earlier failure not retrying", Hex.toHexString(rd));
        return null;
      }
    }

    //
    // Check if we need to download directly, no blockers
    //
    if (blockers == null || blockers.length == 0) {
      reporter.trace("descriptor %s, not found, immediate download", Hex.toHexString(rd));
      download(rds, path);
      return path;
    }

    //
    // We have blockers so we can download in the background.
    //

    reporter.trace("descriptor %s, not found, background download", Hex.toHexString(rd));

    //
    // With download listeners we need to be careful to queue them
    // appropriately. Don't want to download n times because
    // requests arrive during downloads.
    //

    synchronized (queues) {
      List<DownloadListener> list = queues.get(path);
      boolean first = list == null || list.isEmpty();
      for (DownloadListener b : blockers) {
        queues.add(path, b);
      }

      if (!first) {
        // return, file is being downloaded by another and that
        // other will signal the download listener.
        reporter.trace("someone else is downloading our file " + queues.get(path));
        return path;
      }
    }
    limitDownloads.acquire();

    executor.execute(
        new Runnable() {
          public void run() {
            try {
              download(rds, path);
              synchronized (queues) {
                ok(queues.get(path).toArray(EMPTY_LISTENER), path);
              }
            } catch (Exception e) {
              synchronized (queues) {
                fail(e, queues.get(path).toArray(EMPTY_LISTENER), path);
              }
            } finally {
              synchronized (queues) {
                queues.remove(path);
              }
              limitDownloads.release();
            }
          }
        });
    return path;
  }