Search in sources :

Example 11 with RevisionRef

use of aQute.service.library.Library.RevisionRef in project bnd by bndtools.

the class Repository method getRevisionRef.

/**
	 * Find a revisionref for a bsn/version
	 * 
	 * @param bsn
	 * @param version
	 * @throws Exception
	 */
private RevisionRef getRevisionRef(String bsn, Version version) throws Exception {
    // Handle when we have a sha reference
    String id = bsn + "-" + version;
    if (notfoundref.contains(id))
        return null;
    if (isSha(bsn) && version.equals(Version.LOWEST)) {
        Revision r = getRevision(new Coordinate(bsn));
        if (r == null)
            return null;
        return new RevisionRef(r);
    }
    logger.debug("Looking for {}-{}", bsn, version);
    for (RevisionRef r : getRevisionRefs(bsn)) {
        Version v = toVersion(r.baseline, r.qualifier);
        if (v.equals(version))
            return r;
    }
    notfoundref.add(id);
    return null;
}
Also used : RevisionRef(aQute.service.library.Library.RevisionRef) Revision(aQute.service.library.Library.Revision) Coordinate(aQute.service.library.Coordinate) Version(aQute.bnd.version.Version)

Example 12 with RevisionRef

use of aQute.service.library.Library.RevisionRef in project bnd by bndtools.

the class JustAnotherPackageManager method what.

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();
        }
    }
    logger.debug("sha {}", Hex.toHexString(sha));
    Revision revision = library.getRevision(sha);
    if (revision == null) {
        return null;
    }
    Justif justif = new Justif(120, 20, 70, 20, 75);
    DateFormat dateFormat = DateFormat.getDateInstance();
    StringBuilder sb = new StringBuilder();
    try (Formatter f = new Formatter(sb)) {
        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();
    }
}
Also used : Justif(aQute.lib.justif.Justif) Matcher(java.util.regex.Matcher) Formatter(java.util.Formatter) URL(java.net.URL) Date(java.util.Date) RevisionRef(aQute.service.library.Library.RevisionRef) Revision(aQute.service.library.Library.Revision) DateFormat(java.text.DateFormat) JarFile(java.util.jar.JarFile) IO.createTempFile(aQute.lib.io.IO.createTempFile) File(java.io.File)

Example 13 with RevisionRef

use of aQute.service.library.Library.RevisionRef in project bnd by bndtools.

the class JPMTest method testAddMirrorURL.

public void testAddMirrorURL() throws Exception {
    // Drop two URIs for an identical resource
    URI uri1 = IO.getFile("testdata/ws/cnf/jar/biz.aQute.bnd.annotation-2.3.0.jar").toURI();
    repo.dropTarget(uri1);
    URI uri2 = IO.getFile("testdata/ws/cnf/jar/biz.aQute.bnd.annotation-2.3.0-duplicate.jar").toURI();
    repo.dropTarget(uri2);
    // Read the index, there should be one resource but two mirror URLs
    List<RevisionRef> refs = new Index(IO.getFile(tmp, "index")).getRevisionRefs();
    assertEquals(1, refs.size());
    assertEquals(2, refs.get(0).urls.size());
    assertTrue(refs.get(0).urls.contains(uri1));
    assertTrue(refs.get(0).urls.contains(uri2));
}
Also used : RevisionRef(aQute.service.library.Library.RevisionRef) URI(java.net.URI)

Example 14 with RevisionRef

use of aQute.service.library.Library.RevisionRef in project bnd by bndtools.

the class Repository method get.

/**
	 * Get a revision.
	 */
@Override
public File get(String bsn, Version version, Map<String, String> attrs, final DownloadListener... listeners) throws Exception {
    init();
    // Check if we're supposed to have this
    RevisionRef resource = index.getRevisionRef(bsn, version);
    if (resource == null)
        return null;
    else
        return getLocal(resource, attrs, listeners);
}
Also used : RevisionRef(aQute.service.library.Library.RevisionRef)

Example 15 with RevisionRef

use of aQute.service.library.Library.RevisionRef in project bnd by bndtools.

the class Repository method analyze.

/**
	 * We have a URI that potentially could be a JAR. We download it and analyze
	 * it. If it looks like a bndle or JAR, we try to guess the different parts
	 * from it and return a ReveisionRef.
	 * 
	 * @param uri the potential URI to a bundle/jar
	 * @return null or a RevisionRef describing the bundle/jar
	 * @throws IOException
	 * @throws IllegalArgumentException
	 */
private RevisionRef analyze(File file, URI uri) throws IllegalArgumentException, IOException {
    try {
        try (Jar jar = new Jar(file)) {
            Manifest manifest = jar.getManifest();
            if (manifest == null) {
                logger.debug("Jar {} has no manifest", uri);
                return null;
            }
            Domain domain = Domain.domain(manifest);
            RevisionRef ref = new RevisionRef();
            ref.created = System.currentTimeMillis();
            ref.md5 = MD5.digest(file).digest();
            ref.revision = SHA1.digest(file).digest();
            ref.phase = Library.Phase.MASTER;
            ref.size = file.length();
            ref.urls.add(uri);
            Entry<String, Attrs> bsn = domain.getBundleSymbolicName();
            if (bsn != null) {
                ref.bsn = bsn.getKey();
                ref.name = domain.get(Constants.BUNDLE_SYMBOLICNAME);
                ref.version = domain.getBundleVersion();
                ref.description = domain.get(Constants.BUNDLE_DESCRIPTION);
                ref.groupId = "osgi";
                ref.artifactId = ref.bsn;
            }
            try {
                Map<String, Resource> map = jar.getDirectories().get("META-INF/maven");
                if (map.size() != 1) {
                    return ref;
                }
                ref.groupId = map.keySet().iterator().next();
                map = jar.getDirectories().get("META-INF/maven/" + ref.groupId);
                if (map.size() != 1) {
                    return ref;
                }
                ref.artifactId = map.keySet().iterator().next();
                if (ref.bsn == null) {
                    ref.bsn = ref.groupId + "__" + ref.artifactId;
                }
                Resource r = jar.getResource("META-INF/maven/" + ref.groupId + "/" + ref.artifactId + "/pom.xml");
                if (r != null) {
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(r.openInputStream());
                    XPath xp = xpf.newXPath();
                    if (ref.description == null) {
                        ref.description = xp.evaluate("//description", doc);
                    }
                    if (ref.version == null) {
                        ref.version = xp.evaluate("//version", doc);
                    }
                    if (ref.name == null) {
                        ref.name = xp.evaluate("//name", doc);
                    }
                    ref.packaging = xp.evaluate("//packaging", doc);
                    ref.classifier = xp.evaluate("//classifier", doc);
                }
            } catch (Exception e) {
                logger.debug("parsing maven failed for {}: {}", uri, e);
            }
            if (ref.version == null)
                ref.version = "0";
            if (Verifier.isVersion(ref.version)) {
                Version version = new Version(ref.version);
                ref.baseline = version.getWithoutQualifier().toString();
                ref.qualifier = version.getQualifier();
            }
            if (ref.bsn == null) {
                Pattern JAR_URI_P = Pattern.compile(".*/([^/]+)(?:\\.jar)?", Pattern.CASE_INSENSITIVE);
                Matcher m = JAR_URI_P.matcher(uri.toString());
                if (m.matches()) {
                    ref.bsn = m.group(1);
                } else
                    ref.bsn = "unknown";
            }
            return ref;
        }
    } catch (Exception e) {
        logger.debug("Could not parse JAR {}: {}", uri, e);
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Attrs(aQute.bnd.header.Attrs) Resource(aQute.bnd.osgi.Resource) Manifest(java.util.jar.Manifest) Document(org.w3c.dom.Document) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) SocketException(java.net.SocketException) IOException(java.io.IOException) RevisionRef(aQute.service.library.Library.RevisionRef) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Version(aQute.bnd.version.Version) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain)

Aggregations

RevisionRef (aQute.service.library.Library.RevisionRef)22 Version (aQute.bnd.version.Version)11 Revision (aQute.service.library.Library.Revision)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 SocketException (java.net.SocketException)5 URISyntaxException (java.net.URISyntaxException)5 HashSet (java.util.HashSet)5 Matcher (java.util.regex.Matcher)5 Coordinate (aQute.service.library.Coordinate)4 Library (aQute.service.library.Library)4 ArrayList (java.util.ArrayList)4 Program (aQute.service.library.Library.Program)3 File (java.io.File)3 URI (java.net.URI)3 Formatter (java.util.Formatter)2 LinkedHashMap (java.util.LinkedHashMap)2 Container (aQute.bnd.build.Container)1 Project (aQute.bnd.build.Project)1 Workspace (aQute.bnd.build.Workspace)1