コード例 #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
  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");
    }
  }
コード例 #3
0
  public void store(File file, String path) throws Exception {

    int n = 0;
    URL url = new URL(base + path);
    SHA1 sha1 = SHA1.digest(file);
    MD5 md5 = MD5.digest(file);

    TaggedData go = client.build().put().upload(file).updateTag().asTag().go(url);

    switch (go.getState()) {
      case NOT_FOUND:
      case OTHER:
        throw new IOException("Could not store " + path + " from " + file + " with " + go);

      case UNMODIFIED:
      case UPDATED:
      default:
        break;
    }
    client.build().put().upload(sha1.asHex()).asTag().go(new URL(base + path + ".sha1"));
    client.build().put().upload(md5.asHex()).asTag().go(new URL(base + path + ".md5"));
  }
コード例 #4
0
  public TaggedData fetch(String path, File file) throws Exception {
    URL url = new URL(base + path);
    int n = 0;
    while (true)
      try {
        reporter.trace("Fetching %s", path);

        TaggedData tag =
            client
                .build()
                .headers("User-Agent", "Bnd")
                .useCache(file, DEFAULT_MAX_STALE)
                .asTag()
                .go(url);
        reporter.trace("Fetched %s", tag);
        if (tag.getState() == State.UPDATED) {

          // https://issues.sonatype.org/browse/NEXUS-4900

          if (!path.endsWith("/maven-metadata.xml")) {
            URL shaUrl = new URL(base + path + ".sha1");
            URL md5Url = new URL(base + path + ".md5");
            String sha = client.build().asString().timeout(15000).go(shaUrl);
            if (sha != null) {
              String fileSha = SHA1.digest(file).asHex();
              checkDigest(fileSha, sha, file);
            } else {
              String md5 = client.build().asString().timeout(15000).go(md5Url);
              if (md5 != null) {
                String fileMD5 = MD5.digest(file).asHex();
                checkDigest(fileMD5, md5, file);
              }
            }
          }
        }
        return tag;
      } catch (Exception e) {
        n++;
        if (n > 3) throw e;
        Thread.sleep(1000 * n);
      }
  }
コード例 #5
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();
    }
  }
コード例 #6
0
ファイル: ProjectLauncherImpl.java プロジェクト: hemantgp/bnd
  /**
   * Create a standalone executable. All entries on the runpath are rolled out into the JAR and the
   * runbundles are copied to a directory in the jar. The launcher will see that it starts in
   * embedded mode and will automatically detect that it should load the bundles from inside. This
   * is drive by the launcher.embedded flag.
   *
   * @throws Exception
   */
  @Override
  public Jar executable() throws Exception {

    // TODO use constants in the future
    Parameters packageHeader = OSGiHeader.parseHeader(project.getProperty("-package"));
    boolean useShas = packageHeader.containsKey("jpm");
    project.trace("Useshas %s %s", useShas, packageHeader);

    Jar jar = new Jar(project.getName());

    Builder b = new Builder();
    project.addClose(b);

    if (!project.getIncludeResource().isEmpty()) {
      b.setIncludeResource(project.getIncludeResource().toString());
      b.setProperty(Constants.RESOURCEONLY, "true");
      b.build();
      if (b.isOk()) {
        jar.addAll(b.getJar());
      }
      project.getInfo(b);
    }

    List<String> runpath = getRunpath();

    Set<String> runpathShas = new LinkedHashSet<String>();
    Set<String> runbundleShas = new LinkedHashSet<String>();
    List<String> classpath = new ArrayList<String>();

    for (String path : runpath) {
      project.trace("embedding runpath %s", path);
      File file = new File(path);
      if (file.isFile()) {
        if (useShas) {
          String sha = SHA1.digest(file).asHex();
          runpathShas.add(sha + ";name=\"" + file.getName() + "\"");
        } else {
          String newPath = "jar/" + file.getName();
          jar.putResource(newPath, new FileResource(file));
          classpath.add(newPath);
        }
      }
    }

    // Copy the bundles to the JAR

    List<String> runbundles = (List<String>) getRunBundles();
    List<String> actualPaths = new ArrayList<String>();

    for (String path : runbundles) {
      project.trace("embedding run bundles %s", path);
      File file = new File(path);
      if (!file.isFile()) project.error("Invalid entry in -runbundles %s", file);
      else {
        if (useShas) {
          String sha = SHA1.digest(file).asHex();
          runbundleShas.add(sha + ";name=\"" + file.getName() + "\"");
          actualPaths.add("${JPMREPO}/" + sha);
        } else {
          String newPath = "jar/" + file.getName();
          jar.putResource(newPath, new FileResource(file));
          actualPaths.add(newPath);
        }
      }
    }

    LauncherConstants lc = getConstants(actualPaths, true);
    lc.embedded = !useShas;
    lc.storageDir = null; // cannot use local info

    final Properties p = lc.getProperties(new UTF8Properties());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    p.store(bout, "");
    jar.putResource(
        LauncherConstants.DEFAULT_LAUNCHER_PROPERTIES,
        new EmbeddedResource(bout.toByteArray(), 0L));

    Manifest m = new Manifest();
    Attributes main = m.getMainAttributes();

    for (Entry<Object, Object> e : project.getFlattenedProperties().entrySet()) {
      String key = (String) e.getKey();
      if (key.length() > 0 && Character.isUpperCase(key.charAt(0)))
        main.putValue(key, (String) e.getValue());
    }

    Instructions instructions = new Instructions(project.getProperty(Constants.REMOVEHEADERS));
    Collection<Object> result = instructions.select(main.keySet(), false);
    main.keySet().removeAll(result);

    if (useShas) {
      project.trace("Use JPM launcher");
      m.getMainAttributes().putValue("Main-Class", JPM_LAUNCHER_FQN);
      m.getMainAttributes().putValue("JPM-Classpath", Processor.join(runpathShas));
      m.getMainAttributes().putValue("JPM-Runbundles", Processor.join(runbundleShas));
      URLResource jpmLauncher = new URLResource(this.getClass().getResource("/" + JPM_LAUNCHER));
      jar.putResource(JPM_LAUNCHER, jpmLauncher);
      doStart(jar, JPM_LAUNCHER_FQN);
    } else {
      project.trace("Use Embedded launcher");
      m.getMainAttributes().putValue("Main-Class", EMBEDDED_LAUNCHER_FQN);
      m.getMainAttributes().putValue(EmbeddedLauncher.EMBEDDED_RUNPATH, Processor.join(classpath));
      URLResource embeddedLauncher =
          new URLResource(this.getClass().getResource("/" + EMBEDDED_LAUNCHER));
      jar.putResource(EMBEDDED_LAUNCHER, embeddedLauncher);
      doStart(jar, EMBEDDED_LAUNCHER_FQN);
    }
    if (project.getProperty(Constants.DIGESTS) != null)
      jar.setDigestAlgorithms(project.getProperty(Constants.DIGESTS).trim().split("\\s*,\\s*"));
    else jar.setDigestAlgorithms(new String[] {"SHA-1", "MD-5"});
    jar.setManifest(m);
    return jar;
  }